C++学习记录(十)容器vector、string

这是第十天的学习

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4 #include <deque>
  5 using namespace std;
  6 
  7 template <class T>
  8 bool cmp(const T &a, const T &b)
  9 {
 10     return a > b;
 11 }
 12 
 13 void test()
 14 {
 15     // 动态数组容器,vector和array几何一样
 16     vector<int> _vector;
 17     // 对STL容器vector对象插入数据操作
 18     _vector.push_back(1);
 19     _vector.push_back(5);
 20     _vector.push_back(3);
 21     _vector.push_back(9);
 22     _vector.push_back(7);
 23     // 遍历操作
 24     vector<int>::iterator it;
 25     for(it=_vector.begin(); it!=_vector.end(); it++)
 26     {
 27         cout << *it << endl;
 28     }
 29     cout << "---***---" << endl;
 30     // 排序,从小到大
 31     sort(_vector.begin(),_vector.end());
 32     for(it=_vector.begin(); it!=_vector.end(); it++)
 33     {
 34         cout << *it << endl;
 35     }
 36     cout << "---***---" << endl;
 37     // 排序,从大到小
 38     sort(_vector.begin(),_vector.end(),cmp<int>);
 39     for(it=_vector.begin(); it!=_vector.end(); it++)
 40     {
 41         cout << *it << endl;
 42     }
 43 }
 44 
 45 void test1()
 46 {
 47     string str("hello");
 48     cout << str << endl;
 49     string str1 = str;
 50     cout << str1 << endl;
 51     string str2(3,'w');
 52     cout << str2 << endl;
 53     string str3 = str1 + str2;
 54     cout << str3 << endl;
 55     string str4 = "hello world";
 56     cout << str4 << endl;
 57     // 字符串元素操作
 58     cout << str4.at(2) << endl;
 59     str4 += str3;
 60     cout << str4 << endl;
 61     for(string::iterator it = str.begin(); it!=str.end(); it++)
 62     {
 63         cout << *it << endl;
 64     }
 65     for(char s: str)            // 枚举for循环
 66     {
 67         cout << s << endl;
 68     }
 69 }
 70 
 71 void show(int value)
 72 {
 73     cout << value << endl;
 74 }
 75 class Show
 76 {
 77 public:
 78     void operator()(int& value)
 79     {
 80         cout << value << endl;
 81     }
 82 };
 83 
 84 void test2()
 85 {
 86     vector<int> _vector;
 87     for(int i = 0; i<30; i++)
 88     {
 89         _vector.push_back(i);
 90         cout << "Vector capacity:" << _vector.capacity() << " , real size: " << _vector.size() << endl;
 91         // 开辟空间的策略按2的倍数开辟,根据数据大小自动扩容
 92     }
 93 
 94     int arr[3] = {1,4,7};
 95     //vector<int> vec(arr, arr+2);
 96     vector<int> vec(arr, arr + sizeof (arr)/sizeof (int));
 97     // -1-
 98     for(int k : vec)
 99     {
100         cout << k << endl;
101     }
102     cout << "---***---" << endl;
103     // -2-
104     for(auto it = vec.begin(); it!=vec.end(); it++)
105     {
106         cout << *it << endl;
107     }
108     cout << "---***---" << endl;
109     // -3- 使用algorithm中的for_each函数模板,使用函数指针作为策略
110     for_each(vec.begin(),vec.end(),&show);
111     cout << "---***---" << endl;
112     // -4- 使用函数对象(仿函数来实现策略)
113     // 仿函数:函数对象
114     for_each(vec.begin(),vec.end(),Show());
115     cout << "---***---" << endl;
116     // -5- 使用匿名函数Lambda表达式
117     // []()->返回值{//匿名函数体};[]用于标识函数体内捕捉外部变量的方式,1拷贝=,2引用&
118     // ()匿名函数的参数列表,->返回值,没有返回值可以省略
119     for_each(vec.begin(),vec.end(),[&](int& value){
120         cout << value << endl;
121     });
122 }
123 
124 int main()
125 {
126     //test();
127     //test1();
128     test2();
129 
130     cout << "Hello World!" << endl;
131     return 0;
132 }

 

posted @ 2022-05-14 21:07  Z_He  阅读(28)  评论(0编辑  收藏  举报