STL_List使用

梦里梦外; 2022-09-23 03:52 278阅读 0赞

list构造:

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// constructing lists
  2. #include <iostream>
  3. #include <list>
  4. int main()
  5. {
  6. std::list<int> first;// 空链表
  7. std::list<int> second(4, 100); //用4个值,全为100
  8. std::list<int> third(second.begin(), second.end()); //以第二个进行构造
  9. std::list<int> fourth(third);//以第三个构造
  10. int myints[] = { 16, 2, 77, 29 };//以数组进行构造
  11. std::list<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
  12. std::cout << "The contents of fifth are: ";
  13. for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
  14. std::cout << *it << ' ';
  15. std::cout << '\n';
  16. system("pause");
  17. return 0;
  18. }</span>

Center

list赋值运算符重载:

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// assignment operator with lists
  2. #include <iostream>
  3. #include <list>
  4. int main()
  5. {
  6. std::list<int> first(3); // list 用3个0
  7. std::list<int> second(5); // list 用5个0
  8. second = first;//以first赋值给second,second变为3个0
  9. first = std::list<int>(); //再以空链表赋值给first
  10. std::cout << "Size of first: " << int(first.size()) << '\n';
  11. std::cout << "Size of second: " << int(second.size()) << '\n';
  12. system("pause");
  13. return 0;
  14. }</span>

list::begin() :指向迭代器起始位置

list::end() :指向最后一个元素的下一个位置

list::rbegin()/list::rend(): 逆向迭代器。

list::cbegin(): C++11标准, const类型迭代器

list::cend(): C++11标准,const类型迭代器

list::size():求元素个数

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// list::size
  2. #include <iostream>
  3. #include <list>
  4. int main()
  5. {
  6. std::list<int> l;
  7. std::cout << "0. size: " << l.size() << '\n';
  8. for (int i = 0; i<10; i++)
  9. l.push_back(i);//插入10个元素,分别为0-9
  10. std::cout << "1. size: " << l.size() << '\n';
  11. l.insert(l.begin(), 10, 100);//从头开始插入,插入10个元素,全为100
  12. std::cout << "2. size: " << l.size() << '\n';
  13. l.pop_back();//尾删一个元素
  14. std::cout << "3. size: " << l.size() << '\n';//剩余19个元素
  15. system("pause");
  16. return 0;
  17. }
  18. </span>

list::front():

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">std::list<int> l;
  2. l.push_back(77);
  3. l.push_back(22);
  4. //l.front()指向第一个元素
  5. l.front() -= l.back();//77-=22-->55
  6. std::cout << "l.front() is now " << l.front() << '\n';</span>

list::back():指向链表中最后一个元素

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">std::list<int> l;
  2. l.push_back(10);
  3. while (l.back() != 0)
  4. {
  5. l.push_back(l.back() - 1);//l.back()指向最后一个元素,每次减一
  6. }</span>

list::assign():重新分配空间

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">int myints[] = { 1776, 7, 4 };
  2. first.assign(myints, myints + 3); //以数组重新分配空间</span>

list::push_front(): 在头结点前插入

list::push_front():从头结点删除

list::push_back():尾插

list::pop_back():尾删

list::insert():插入

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// inserting into a list
  2. #include <iostream>
  3. #include <list>
  4. #include <vector>
  5. int main()
  6. {
  7. std::list<int> mylist;
  8. std::list<int>::iterator it;
  9. // set some initial values:
  10. for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5
  11. it = mylist.begin();
  12. ++it; // it 指向第二个 ^
  13. mylist.insert(it, 10); // 1 10 2 3 4 5
  14. // it仍然指向数字2,插入2个20,所以2 3 4 5向后挪动,然后插入2个20 ^
  15. mylist.insert(it, 2, 20); // 1 10 20 20 2 3 4 5
  16. --it; // 减减it,it指向第二个20
  17. std::vector<int> myvector(2, 30);
  18. mylist.insert(it, myvector.begin(), myvector.end());
  19. //it指向第二个20,插入2个30,所以第二个20后面的元素向后移动,然后插入2个30
  20. // 1 10 20 30 30 20 2 3 4 5
  21. // ^
  22. std::cout << "mylist contains:";
  23. for (it = mylist.begin(); it != mylist.end(); ++it)
  24. std::cout << ' ' << *it;
  25. std::cout << '\n';
  26. system("pause");
  27. return 0;
  28. }</span>

list::erase():删除

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// erasing from list
  2. #include <iostream>
  3. #include <list>
  4. int main()
  5. {
  6. std::list<int> mylist;
  7. std::list<int>::iterator it1, it2;
  8. // set some values:
  9. for (int i = 1; i<10; ++i)
  10. mylist.push_back(i * 10);// 10 20 30 40 50 60 70 80 90
  11. it1 = it2 = mylist.begin();
  12. advance(it2, 6); //迭代器it2向前移动6个位置。
  13. ++it1; //it1后移一位---指向20
  14. //记得要用迭代器接收
  15. it1 = mylist.erase(it1); //it1指向20,erase(it1),返回下一个位置给it1
  16. it2 = mylist.erase(it2); //it2指向70,erase(it2),返回下一个位置80
  17. ++it1; //指向40
  18. --it2; //指向60
  19. //10 30 40 50 60 80 90
  20. mylist.erase(it1, it2); // 10 30 60 80 90
  21. //删除40 50 (60并不删除,因为是左闭右开区间)
  22. std::cout << "mylist contains:";
  23. for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
  24. std::cout << ' ' << *it1;
  25. std::cout << '\n';
  26. system("pause");
  27. return 0;
  28. }</span>

list::remove():移除

list::unique():去重

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">// list::unique
  2. #include <iostream>
  3. #include <cmath>
  4. #include <list>
  5. // a binary predicate implemented as a function:
  6. bool same_integral_part(double first, double second)
  7. {
  8. return (int(first) == int(second));
  9. }
  10. // a binary predicate implemented as a class:
  11. struct is_near {
  12. bool operator() (double first, double second)
  13. {
  14. return (fabs(first - second)<5.0);
  15. }
  16. };
  17. int main()
  18. {
  19. double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
  20. 12.77, 73.35, 72.25, 15.3, 72.25 };
  21. std::list<double> mylist(mydoubles, mydoubles + 10);
  22. mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
  23. // 15.3, 72.25, 72.25, 73.0, 73.35
  24. mylist.unique(); //去重 // 2.72, 3.14, 12.15, 12.77
  25. // 15.3, 72.25, 73.0, 73.35
  26. mylist.unique(same_integral_part);
  27. //整数部分相同的去掉一个,保留第一个,去掉后面的
  28. // 2.72, 3.14, 12.15 15.3, 72.25, 73.0
  29. mylist.unique(is_near());//绝对值小于5的删除后一个 // 2.72, 12.15, 72.25
  30. std::cout << "mylist contains:";
  31. for (std::list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  32. std::cout << ' ' << *it;
  33. std::cout << '\n';
  34. system("pause");
  35. return 0;
  36. }</span>

list::merge():合并

list::sort():排序

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jmeter使用(jmeter使用流程)

    使用jmeter做性能测试脚本怎么写 如果只是简单的接口类的脚本,完全可以百度一下,然后照着别人的流程走,主要是注意各种配置和参数的意义就行了,这个其实没多大难度,比lo

    相关 SVN使用使用教程

    SVN使用使用教程 前言 本人在公司孤儿式开发,是用不到svn这种工具的,但是怕突然某一天电脑坏掉,代码消失所以还是使用上代码管理工具。 之前使用过svn工具,但

    相关 idea使用svn(日常使用)

    本文记录了svn的日常使用!!!非常详细!!!持续更新… 更新svn的项目到本地(一般是先更新再进行提交) ![在这里插入图片描述][watermark_type_