#include #include //所有元素都是pair //第一个元素是key(索引) //第二个元素是value(实值) //map不可重复,multimap可以重复 #include #include using namespace std; int main() { map stu; srand(time(0)); cout << "按照key排序" << endl; stu.insert(pair (1, rand() % 20 + 1)); stu.insert(pair (3, rand() % 20 + 1)); stu.insert(pair (2, rand() % 20 + 1)); stu.insert(pair (4, rand() % 20 + 1)); for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout << "拷贝构造" << endl; map stu1(stu); for (auto a : stu1) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout << "赋值" << endl; map stu2=stu; for (auto a : stu2) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } }
#include #include //所有元素都是pair //第一个元素是key(索引) //第二个元素是value(实值) //map不可重复,multimap可以重复 #include #include using namespace std; int main() { map stu; map stu1; srand(time(0)); cout << "按照key排序" << endl; stu.empty() ? cout << "为空" << endl : cout << "不为空" << endl; cout << "大小是:" << stu.size() << endl; stu.insert(pair (1, rand() % 20 + 1)); stu.insert(pair (3, rand() % 20 + 1)); stu.insert(pair (2, rand() % 20 + 1)); stu.insert(pair (4, rand() % 20 + 1)); stu.empty() ? cout << "为空"<(5, rand() % 20 + 1)); stu1.insert(pair (6, rand() % 20 + 1)); stu1.insert(pair (7, rand() % 20 + 1)); stu1.insert(pair (8, rand() % 20 + 1)); for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout << endl; for (auto a : stu1) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout < cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout << endl; for (auto a : stu1) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } }
#include #include //所有元素都是pair //第一个元素是key(索引) //第二个元素是value(实值) //map不可重复,multimap可以重复 #include #include using namespace std; int main() { map stu; map stu1; srand(time(0)); cout << "按照key排序" << endl; stu.insert(pair (1, rand() % 20 + 1)); stu.insert(make_pair(3, rand() % 20 + 1)); stu[2]= rand() % 20 + 1;//不建议 stu.insert(pair (4, rand() % 20 + 1)); for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } cout << endl << stu[5] << endl; stu.erase(1); cout << endl << "删除key值1" << endl; for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } stu.clear(); cout << "为空" << endl; for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } }
#include #include //所有元素都是pair //第一个元素是key(索引) //第二个元素是value(实值) //map不可重复,multimap可以重复 #include #include using namespace std; int main() { map stu; map stu1; srand(time(0)); cout << "按照key排序" << endl; stu.insert(pair (1, rand() % 20 + 1)); stu.insert(make_pair(3, rand() % 20 + 1)); stu[2]= rand() % 20 + 1;//不建议 stu.insert(pair (4, rand() % 20 + 1)); for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } map ::iterator pos; pos=stu.find(1); if (pos != stu.end()) cout << "存在这个元素,位置是->" <<(*pos).first << ",元素值是->" << (*pos).second; else cout << "不存在这个元素"; cout << endl; int num = stu.count(3); cout << "3" << "的key为->" << num << endl; num = stu.count(5); cout << "5" << "的key为->" << num << endl; multimap aa; aa.insert(make_pair(1, 1)); aa.insert(make_pair(1, 2)); aa.insert(make_pair(2, 3)); num = aa.count(1); cout << "1" << "的key为->" << num << endl; }
#include #include //所有元素都是pair //第一个元素是key(索引) //第二个元素是value(实值) //map不可重复,multimap可以重复 #include #include using namespace std; class paixu { public: bool operator()(int b1, int b2)const { return b1 > b2; } }; int main() { map stu; cout << "仿函数operator" << endl; cout << "改变排序规则(创建时就要改变)" << endl; srand(time(0)); cout << "按照key排序" << endl; stu.insert(make_pair(1, rand() % 20 + 1)); stu.insert(make_pair(3, rand() % 20 + 1)); stu[2]= rand() % 20 + 1;//不建议 stu.insert(make_pair(4, rand() % 20 + 1)); for (auto a : stu) { cout << "编号:" << a.first << ",年龄:" << a.second << endl; } }
上一篇 【Mybatis】单表多表查询,动态SQL使用
下一篇 【CVE-2021-35042】django order
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号