map
#include <bits/stdc++.h>
using namespace std;
int main ( ) {
pair<string, int> student ("Alice",95);
cout << "键:" << student.first << ",值:" << student.second << endl;
map<string, int> scores;
scores["***"] = 87;
scores["**"] = 92;
scores["****"] = 91;
scores["**c"] = 120;
cout << "\n范围for循环:" << endl;
for (const auto& entry : scores) {
cout << entry.first << ":" << entry.second << endl;
}
auto result1 = scores.insert(pair<string,int>("wangshuo",119));
cout << "插入102:" << boolalpha << result1.second << endl;
map<string, double> prices = {
{"apple", 2.5},
{"banana", 1.8},
{"orange", 3.2}
};
cout << "苹果价格:" << prices["apple"] << endl;
cout << "不存在的梨价格:" << prices["pear"] << endl;
cout << "\n范围for循环:" << endl;
auto it = prices.find("banana");
if (it != prices.end()) {
cout << "找到香蕉:" << it -> second << endl;
} else {
cout << "香蕉不存在" << endl;
}
if (prices.count("apple") > 0) {
cout << "苹果存在," << prices["apple"] << endl;
}
map<int, string> data = {
{1, "one"}, {2,"two"}, {3, "three"},
{4, "four"}, {5,"five"}, {6, "six"}
};
int count = data.erase(2);
cout << "\n删除键2:" << (count > 0 ? "成功" : "失败") << endl;
data.clear();
data.empty();
data.size();
return 0;
}

浙公网安备 33010602011771号