vector

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template<class T>
  5. void PrintVector(T s, T e) {
  6. for(; s != e; ++s)
  7. cout << *s << endl " ";
  8. cout << endl;
  9. }
  10. void main() {
  11. int a[5] = {1, 2, 3, 4, 5};
  12. vector<int> v(a, a+5); // 将a中的元素给v
  13. // 两个随机迭代器相减,输出 1) 5
  14. cout << "1) " << v.end() - v.begin();
  15. // 2) 1 2 3 4 5
  16. cout << "2) "; PrintVector(v.begin(), v.end());
  17. v.insert(v.begin() + 2, 13); // 在数组中插入元素13
  18. // 3) 1 2 13 3 4 5
  19. cout << "3) "; PrintVector(v.begin(), v.end());
  20. v.erase(v.begin() + 2); // 删除位于 begin() + 2的元素
  21. // 4) 1 2 3 4 5
  22. cout << "4) "; PrintVector(v.begin(), v.end());
  23. vector<int> v2(4, 100); // v2 有4个100
  24. // 将v的一段插入v2的开头
  25. v2.insert(v2.begin(), v.begin() + 1, v.begin() + 3);
  26. // 5) v2: 2 3 100 100 100 100
  27. cout << "5) "; PrintVector(v2.begin(), v2.end());
  28. // 删除 v 上的一个区间,即 2,3
  29. v.erase(v.begin() + 1, b.begin() + 3);
  30. // 6) 1 4 5
  31. cout << "6) "; PrintVector(v.begin(), v.end());
  32. }

vector实现二维数组

  • vector<vector<int> > 这里要把最后两个”>”分开,不然有的编译器会认为是右移运算符

    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4. void main() {
    5. vector<vector<int> > v(3);
    6. for(int i = 0; i < v.size(); ++i)
    7. for(int j = 0; j < 4; ++j)
    8. v[i].push_back(j);
    9. for(int i = 0; i < v.size(); ++i) {
    10. for(int j = 0; j < v[i].size(); ++j)
    11. cout << v[i][j] << " ";
    12. cout << endl;
    13. }
    14. }

    deque

    image.png

    双向链表list

    image.png
    image.png

    1. #include <list>
    2. #include <iostream>
    3. #include <algotirhm>
    4. using namespace std;
    5. class A {
    6. private:
    7. int n;
    8. public:
    9. A (int n_) { n = n_;}
    10. friend bool operator<(const A & a1, cosnt A & a2);
    11. friend bool operator==(const A & a1, const A & a2);
    12. friend ostream & operator << (ostream & o, const A & a);
    13. };
    14. bool operator< (const A & a1, const A & a2) {
    15. return a1.n < a2.n;
    16. }
    17. bool operator == (const A & a1, const A & a2) {
    18. return a1.n == a2.n;
    19. }
    20. ostream & operator << (ostream & o, const A & a) {
    21. o << a.n;
    22. return o;
    23. }
    24. template <class T>
    25. void PrintList(const list<T> & lst) {
    26. // 不推荐的写法,还是两个迭代器为参数比较好
    27. int tmp = lst.size();
    28. if(tmp > 0) {
    29. typename list<T>::const_iterator i;
    30. i = lst.begin();
    31. for(; i != lst.end(); ++i)
    32. cout << *i << ",";
    33. cout << endl;
    34. }
    35. }
    36. void main() {
    37. list<A> lst1, lst2;
    38. lst1.push_back(1); lst1.push_back(2);
    39. lst1.push_back(3); lst1.push_back(4);
    40. lst1.push_back(2);
    41. lst2.push_back(10); lst2.push_back(20);
    42. lst2.push_back(30); lst2.push_back(30);
    43. lst2.push_back(30); lst2.push_back(40);
    44. lst2.push_back(40);
    45. // 1) 1,3,2,4,2
    46. cout << "1) "; PrintList(lst1);
    47. // 2) 40,20,10,30,30,30,40
    48. cout << "2) "; PrintList(lst2);
    49. lst2.sort();
    50. // 3) 10,20,30,30,30,40,40
    51. cout << "3) "; PrintList(lst2);
    52. lst2.pop_front();
    53. // 4) 20,30,30,30,40,40
    54. cout << "4) "; PrintList(lst2);
    55. lst1.remove(2); // 删除所有和A(2)相等的元素
    56. // 5) 1,3,4
    57. cout << "5) "; PrintList(lst1);
    58. lst2.unique(); // 删除所有和前一个元素相等的元素
    59. // 6) 20,30,40
    60. cout << "6) "; PrintList(lst2);
    61. lst1.merge(lst2); // 合并lst2到lst1并清空lst2
    62. // 7) 1,3,4,20,30,40
    63. cout << "7) "; PrintList(lst1);
    64. // 8)
    65. cout << "8) "; PrintList(lst2);
    66. lst1.reverse();
    67. // 9) 40,30,20,4,3,1
    68. cout << "9) "; PrintList(lst1);
    69. lst2.push_back(100); lst2.push_back(200);
    70. lst2.push_back(300); lst2.push_back(400);
    71. list<A>::iterator p1, p2, p3;
    72. p1 = find(lst1.begin(), lst1.end(), 3);
    73. p2 = find(lst2.begin(), lst2.end(), 200);
    74. p3 = find(lst2.begin(), lst2.end(), 400);
    75. // 将[p2, p3)插入p1之前,并从lst2中删除[p2, p3)
    76. lst1.splice(p1, lst2, p2, p3);
    77. // 10) 40,30,20,4,200,300,3,1
    78. cout << "10) "; PrintList(lst1);
    79. // 11) 100,400
    80. cout << "11) "; PrintList(lst2);
    81. }
  • 模板作为参数的写法

  • dev中在模板参数类型前面要加typename
  • splice的用法