• C++之queue及stack容器方法总结


    q.front():引用队列第一个元素
    q.back():引用队列最后一个元素
    q.push():入队
    q.pop():出队
    q.empty():队列判空,空返回1,不为空返回0
    q.emplace():指定位置插入元素,但由于这里是队列,所以效果与push一样
    q.size():获取队列容量
    q.swap():交换两个队列中同类型的元素

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. queue q;
    8. queue p;
    9. p.push("good");
    10. cout<empty()<
    11. //入队
    12. q.push("hello");
    13. q.push("queue");
    14. q.push("nine");
    15. q.push("twelve");
    16. //出队
    17. q.pop(); //队列q中现有内容:queue nine twelve
    18. //引用队列第一个元素
    19. cout<front()<
    20. //引用队列最后一个元素
    21. cout<back()<
    22. //判断队列是否为空,为空返回1,不为空返回0
    23. cout<empty()<
    24. //由于队列是FIFO,不能随意插入,所以这里第一个参数默认为队尾
    25. q.emplace("last");
    26. //获得队列大小
    27. cout<size()<
    28. //交换两个队列中的同类型的内容
    29. q.swap(p);
    30. cout<<"交换后队列p中内容:"<
    31. //由于for循环里面在出队,导致队列p的大小在减小,所以for循环里面不能用p.size作为判断条件
    32. int j=p.size();
    33. for(int i=0;i
    34. cout<front()<
    35. p.pop();
    36. }
    37. cout<<"交换后队列q中内容:"<
    38. cout<front()<
    39. cout<back()<
    40. return 0;
    41. }
    1. 1
    2. queue
    3. twelve
    4. 0
    5. 4
    6. 交换后队列p中内容:
    7. queue
    8. nine
    9. twelve
    10. last
    11. 交换后队列q中内容:
    12. good
    13. good

     s.push():压栈
    s.pop():弹栈
    s.empty():栈判空,空返回1,不为空返回0
    s.emplace():指定位置插入元素,但由于这里是栈,所以效果与push一样
    s.size():获取栈容量
    s.swap():交换两个栈中同类型的元素
    s.top():获取栈顶元素

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. stack s;
    7. //压栈
    8. s.push("hello");
    9. s.push("stack");
    10. s.push("nine");
    11. s.push("twelve");
    12. //弹栈
    13. s.pop();
    14. //判空
    15. s.empty();
    16. //获取栈的大小
    17. cout<size()<
    18. //指定位置插入元素,由于是栈,所以默认在栈顶插入
    19. s.emplace("emplace");
    20. //获取栈顶元素
    21. cout<top()<
    22. stack s1;
    23. s.swap(s1);
    24. int flag_s = s.size();
    25. cout<<"栈s中的内容:"<
    26. for(int i=0;i
    27. cout<top()<
    28. s.pop();
    29. }
    30. int flag_s1 = s1.size();
    31. cout<<"栈s中的内容:"<
    32. for(int i=0;i
    33. cout<top()<
    34. s1.pop();
    35. }
    36. return 0;
    37. }
    1. 3
    2. emplace
    3. 栈s中的内容:
    4. 栈s中的内容:
    5. emplace
    6. nine
    7. stack
    8. hello
  • 相关阅读:
    后端面经学习自测(二)
    python 中文字符转换unicode及Unicode 编码转换为中文
    jQuery_显式迭代each()
    MySQL、SQL Server、Hive对时间格式化
    每天一道C语言编程:排队买票
    关于Mybaits缓存....
    Qt day4
    数据链路层——MAC帧、ARP协议详解
    基于ESP8266和微型舵机的远程灯控制系统
    soh估计:Data-driven prediction of battery cycle life before capacity degradation
  • 原文地址:https://blog.csdn.net/hold_the_key/article/details/126814257