• C++学习笔记(二十八)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十八篇博客。

    本篇博客介绍了C++的map容器,并实现了员工分组。

    本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

    目录

    map容器

    map基本概念

    map构造和赋值

    map大小和交换

    map插入和删除

    map查找和统计

    map排序

    员工分组


    map容器

    map基本概念

    map里面所有元素都是pair。pair的第一个元素为键,起到索引作用,第二个元素为值。所有元素会根据键值自动排序。

    mapmultimap属于关联式容器,底层结构用二叉树实现。map不允许键值重复,而multimap允许键值重复。使用map或multimap都要包含头文件map

    map构造和赋值

    map mp创建一个map容器,键的类型为T1,值得类型为T2。

    map(const map &mp)是拷贝构造函数。

    map& operator=(const map &mp)重载等号操作符。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. map<int, int> m1;
    7. m1.insert(pair<int, int>(25, 75));
    8. m1.insert(pair<int, int>(20, 20));
    9. m1.insert(pair<int, int>(10, 15));
    10. m1.insert(pair<int, int>(5, 20));
    11. m1.insert(pair<int, int>(15, 25));
    12. for (map<int, int>::iterator it = m1.begin(); it != m1.end(); ++it) {
    13. cout << it->first << " " << it->second << endl;
    14. }
    15. cout << endl;
    16. map<int, int>m2(m1);
    17. for (map<int, int>::iterator it = m2.begin(); it != m2.end(); ++it) {
    18. cout << it->first << " " << it->second << endl;
    19. }
    20. cout << endl;
    21. map<int, int>m3;
    22. m3 = m2;
    23. for (map<int, int>::iterator it = m3.begin(); it != m3.end(); ++it) {
    24. cout << it->first << " " << it->second << endl;
    25. }
    26. cout << endl;
    27. return 0;
    28. }

    程序的输出是:

    5   20
    10   15
    15   25
    20   20
    25   75

    5   20
    10   15
    15   25
    20   20
    25   75

    5   20
    10   15
    15   25
    20   20
    25   75
     

    map大小和交换

    size()返回容器中元素个数。

    empty()判断容器是否为空。

    swap(mp)交换两个容器。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. map<int, int>m1;
    7. m1.insert(pair<int, int>(10, 100));
    8. m1.insert(pair<int, int>(20, 200));
    9. m1.insert(pair<int, int>(30, 300));
    10. m1.insert(pair<int, int>(40, 400));
    11. map<int, int>m2;
    12. m2.insert(pair<int, int>(60, 600));
    13. m2.insert(pair<int, int>(70, 700));
    14. m2.insert(pair<int, int>(80, 800));
    15. m2.insert(pair<int, int>(90, 900));
    16. cout << m1.empty() << endl;
    17. cout << m1.size() << endl;
    18. for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
    19. cout << it->first << " " << it->second << endl;
    20. }
    21. cout << endl;
    22. for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
    23. cout << it->first << " " << it->second << endl;
    24. }
    25. cout << endl;
    26. m1.swap(m2);
    27. for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
    28. cout << it->first << " " << it->second << endl;
    29. }
    30. cout << endl;
    31. for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
    32. cout << it->first << " " << it->second << endl;
    33. }
    34. cout << endl;
    35. return 0;
    36. }

    程序的输出是:

    0
    4
    10   100
    20   200
    30   300
    40   400

    60   600
    70   700
    80   800
    90   900

    60   600
    70   700
    80   800
    90   900

    10   100
    20   200
    30   300
    40   400
     

    map插入和删除

    insert(elem)将elem插入容器中。

    clear()删除所有元素。

    erase(pos)删除pos迭代器所指元素。

    erase(beg,end)删除beg到end的所有元素,包括beg不包括end。

    erase(key)删除键值为key的元素。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. map<int, int>m;
    7. m.insert(pair<int, int>(10, 100));
    8. m.insert(pair<int, int>(20, 200));
    9. m.insert(make_pair(30, 300));
    10. m.insert(make_pair(40, 400));
    11. for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    12. cout << it->first << " " << it->second << endl;
    13. }
    14. cout << endl;
    15. m.erase(30);
    16. for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    17. cout << it->first << " " << it->second << endl;
    18. }
    19. cout << endl;
    20. return 0;
    21. }

    程序的输出是:

    10   100
    20   200
    30   300
    40   400

    10   100
    20   200
    40   400
     

    map查找和统计

    find(key)查找key为键的是否存在,存在则返回指向的迭代器,否则返回.end。

    count(key)返回key为键的元素个数。

    map不能插入键重复的元素,否则以第一次为准。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. map<int, int>m;
    7. m.insert(pair<int, int>(10, 100));
    8. m.insert(pair<int, int>(70, 700));
    9. m.insert(make_pair(60, 600));
    10. m.insert(make_pair(30, 300));
    11. map<int, int>::iterator it;
    12. it = m.find(50);
    13. if (it == m.end()) {
    14. cout << "not found" << endl;
    15. }
    16. else {
    17. cout << "found" << endl;
    18. }
    19. it = m.find(60);
    20. if (it == m.end()) {
    21. cout << "not found" << endl;
    22. }
    23. else {
    24. cout << "found" << endl;
    25. }
    26. cout << m.count(30) << endl;
    27. cout << m.count(20) << endl;
    28. return 0;
    29. }

    程序的输出是:

    not found
    found
    1
    0

    map排序

    对于自定义类型,要用仿函数指定排序规则。也可以用仿函数改变默认排序规则。

    1. #include
    2. #include
    3. using namespace std;
    4. class compare
    5. {
    6. public:
    7. bool operator() (int a, int b) const
    8. {
    9. return a > b;
    10. }
    11. };
    12. int main(void)
    13. {
    14. map<int, int, compare> m;
    15. m.insert(pair <int, int>(30, 50));
    16. m.insert(pair <int, int>(40, 60));
    17. m.insert(make_pair(10, 20));
    18. m.insert(make_pair(25, 75));
    19. for (map<int, int, compare>::iterator it = m.begin(); it != m.end(); it++) {
    20. cout << it->first << " " << it->second << endl;
    21. }
    22. return 0;
    23. }

    程序中用仿函数将排序规则改为降序。程序的输出是:

    40   60
    30   50
    25   75
    10   20


    员工分组

    公司招聘了十二个员工,进入公司后要指派在哪个部门工作。员工信息包括姓名和工资。部门有三个部分(程序中用ABC代替)。随机分配给员工部门和工资。

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. class worker
    7. {
    8. public:
    9. string name;
    10. int salary;
    11. };
    12. int main(void)
    13. {
    14. srand((unsigned int)time(NULL));
    15. vector v;
    16. worker w1;
    17. w1.name = "Linda";
    18. w1.salary = rand() % 5000 + 5000 + 1;
    19. worker w2;
    20. w2.name = "Larry";
    21. w2.salary = rand() % 5000 + 5000 + 1;
    22. worker w3;
    23. w3.name = "Lester";
    24. w3.salary = rand() % 5000 + 5000 + 1;
    25. worker w4;
    26. w4.name = "Lisa";
    27. w4.salary = rand() % 5000 + 5000 + 1;
    28. worker w5;
    29. w5.name = "Lidia";
    30. w5.salary = rand() % 5000 + 5000 + 1;
    31. worker w6;
    32. w6.name = "Lee";
    33. w6.salary = rand() % 5000 + 5000 + 1;
    34. worker w7;
    35. w7.name = "Lane";
    36. w7.salary = rand() % 5000 + 5000 + 1;
    37. worker w8;
    38. w8.name = "Leslie";
    39. w8.salary = rand() % 5000 + 5000 + 1;
    40. worker w9;
    41. w9.name = "Lorena";
    42. w9.salary = rand() % 5000 + 5000 + 1;
    43. worker w10;
    44. w10.name = "Lorenzo";
    45. w10.salary = rand() % 5000 + 5000 + 1;
    46. worker w11;
    47. w11.name = "Lowell";
    48. w11.salary = rand() % 5000 + 5000 + 1;
    49. worker w12;
    50. w12.name = "Laura";
    51. w12.salary = rand() % 5000 + 5000 + 1;
    52. v.push_back(w1);
    53. v.push_back(w2);
    54. v.push_back(w3);
    55. v.push_back(w4);
    56. v.push_back(w5);
    57. v.push_back(w6);
    58. v.push_back(w7);
    59. v.push_back(w8);
    60. v.push_back(w9);
    61. v.push_back(w10);
    62. v.push_back(w11);
    63. v.push_back(w12);
    64. multimap<int, worker> m;
    65. for (vector::iterator it = v.begin(); it != v.end(); ++it) {
    66. int work;
    67. work = rand() % 3;
    68. m.insert(make_pair(work, *it));
    69. }
    70. for (multimap<int, worker>::iterator it = m.begin(); it != m.end(); ++it) {
    71. if (it->first == 0) {
    72. cout << "A:" << " ";
    73. }
    74. else if (it->first == 1) {
    75. cout << "B:" << " ";
    76. }
    77. else if (it->first == 2) {
    78. cout << "C:" << " ";
    79. }
    80. cout << "the name is " << it->second.name << " " << "the salary is " << it->second.salary << endl;
    81. }
    82. return 0;
    83. }

    程序创建了worker类,其中name表示姓名,salary表示工资。随后创建了十二个worker类对象,并指定了姓名和工资。这里姓名是手动输入的,所以代码长了。工资是随机生成的,控制使得生成随机数的值是5001-10000。随后加入vector容器v。然后创建一个multimap容器m,键表示部门(0是A,1是B,2是C),值是worker类对象。然后遍历v,对于每个类对象,都将其作为值,将生成的0-2的随机数作为键加入m中。最后遍历容器m进行输出。

    下面是程序的一次运行结果:

    A:   the name is Lester   the salary is 8744
    A:   the name is Lisa   the salary is 9206
    A:   the name is Lidia   the salary is 8034
    A:   the name is Lorenzo   the salary is 9951
    A:   the name is Lowell   the salary is 6045
    B:   the name is Lee   the salary is 6127
    B:   the name is Leslie   the salary is 6978
    B:   the name is Laura   the salary is 5622
    C:   the name is Linda   the salary is 8658
    C:   the name is Larry   the salary is 7003
    C:   the name is Lane   the salary is 9832
    C:   the name is Lorena   the salary is 8370

  • 相关阅读:
    8+NAM+分型+单细胞生信思路
    vue transition淡入淡出效果
    《30天吃掉那只 TensorFlow2.0》 2-3 自动微分机制
    【服务器学习】 iomanager IO协程调度模块
    PI3K α/β 靶向抑制剂协同 BCL-2 阻断过程可用于治疗 DLBCL
    基于Microsoft SemanticKernel和GPT4实现一个智能翻译服务
    [ERROR Swap]: running with swap on is not supported. Please disable swap
    2023年中国婴儿床发展历程及趋势分析:未来婴儿床行业市场规模将继续成长[图]
    nrf52832 PWM配置
    干货!小程序,爆红的三大规律
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126430767