vector学习记录
1 #include <vector> 2 #include <iostream> 3 #include <algorithm> 4 #include <string> 5 using namespace std; 6 7 int main() { 8 //创建 9 /* 10 //方法1 11 vector<char> v1(20, 'a'); //(元素个数 = 0, 默认值 = 0) 12 cout << "record的大小为:" << v1.size() << endl; 13 cout << "record的容量为" << v1.capacity() << endl; 14 for (int i = 0; i < v1.size(); i++) 15 cout << v1[i] << " "; 16 cout << endl; 17 //方法2 18 vector<char> v2{ 'a', 'b', 'c' }; 19 cout << "v2的大小为:" << v2.size() << endl; 20 cout << "v2的容量为" << v2.capacity() << endl; 21 for (int i = 0; i < v2.size(); i++) 22 cout << v2[i] << " "; 23 cout << endl; 24 */ 25 //容量和大小 26 /* 27 vector<char> v(10, 'a'); 28 cout << "容器的元素为:"; 29 for (auto i : v) 30 cout << i << " "; 31 cout << endl; 32 cout << "容器的容量为:" << v.capacity() << endl; 33 cout << "容器的大小为:" << v.size() << endl; 34 v.resize(5); 35 v.reserve(12); 36 cout << "容器大小和容量重定义后" << endl; 37 cout << "容器的元素为:"; 38 for (auto i : v) 39 cout << i << " "; 40 cout << endl; 41 cout << "容器的容量为:" << v.capacity() << endl; 42 cout << "容器的大小为:" << v.size() << endl; 43 */ 44 //访问 45 /* 46 vector<int> v{ 1, 2 }; 47 //cout << v[100] ;//(没有安全检查) 48 //cout<< v.at(100);//有安全检查 49 cout << "第一个元素为:" << v.front() << endl; //front()返回第一个元素的引用 50 cout << "最后一个元素为:" << v.back() << endl;//back()返回最后一个元素的引用 51 auto a = v.data(); //返回vector中动态数组的引用 52 for (int i = 0; i < 2; i++) 53 cout << a[i] << " "; 54 */ 55 //迭代器 56 /* 57 vector<int> v{ 0, 1, 2, 3, 4, 5}; 58 v.reserve(19); 59 //正向迭代器 60 cout << "使用正向迭代器" << endl; 61 for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) { 62 cout << *i << " "; 63 *i *= 2; 64 } 65 cout << endl; 66 //反向迭代器 67 cout << "使用反向迭代器" << endl; 68 for (vector<int>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i) 69 cout << *i << " "; 70 cout << endl; 71 //vector::iterator为随机访问迭代器,功能最为强大,强大的表现为: 72 //1.迭代器可以比较大小(位置关系比较) 73 //2.迭代器可以向前向后多移动几位 74 //...... 75 cout << "随机访问迭代器" << endl; 76 for (vector<int>::iterator i = v.begin(); i < v.end(); i += 2) 77 cout << *i << " "; 78 cout << endl; 79 //辅助函数(advance, iter_swap, distance) 80 vector<int>::iterator i = v.begin(); 81 cout << "迭代器i一开始指向的元素为" << *i << ", "; 82 advance(i, 2); 83 cout << "向前移动2个位置后指向的元素为" << *i << endl; 84 cout << "第一个元素和最后一个元素相差的元素个数为" << distance(v.begin(), --v.end()) << endl; 85 cout << "交换第一个元素和最后一个元素的情况:" << endl; 86 cout << "交换前:"; 87 for (auto i : v) 88 cout << i << " "; 89 cout << endl; 90 cout << "交换后:"; 91 iter_swap(v.begin(), --v.end()); 92 for (auto i : v) 93 cout << i << " "; 94 cout << endl; 95 //const + 迭代器 96 const auto it = v.begin(); //相当于指针常量 97 //it++错误 98 auto cit = v.cbegin(); //相当于指向常量的指针 99 //*cit = 5错误 100 */ 101 //添加 102 /* 103 vector<string> v; 104 v.push_back("123"); //临时变量,需要调用构造函数和转移构造函数 105 v.emplace_back("abc");//直接内部创建元素时作为构造函数参数,效率更高 106 for (auto i : v) 107 cout << i << " "; 108 */ 109 //插入(插入的位置在迭代器参数的前面 110 /* 111 vector<string> v = { "one", "three", "four" }; 112 //emplace 113 v.emplace(++begin(v), "two"); //std::begin(容器对象)返回容器指向第一个元素的迭代器 114 for (auto i : v) 115 cout << i << " "; 116 cout << endl; 117 //insert 单个元素 118 v.insert(v.end(), "five"); 119 for (auto i : v) 120 cout << i << " "; 121 cout << endl; 122 //insert 多个相同元素 123 v.insert(v.begin(), 5, "zero"); 124 for (auto i : v) 125 cout << i << " "; 126 cout << endl; 127 //insert 多个元素 128 v.insert(v.end(), { "six", "seven", "eight" }); //初始式方法 129 string s[] = { "night", "ten", "elevent" }; 130 v.insert(v.end(), begin(s), end(s)); 131 for (auto i : v) 132 cout << i << " "; 133 */ 134 //删除 135 /* 136 vector<int> v = { 0, 1, 2, 3, 3, 3, 4, 5 }; 137 //erase 删除单个位置元素并返回下一个元素位置(begin~end, 但不包括end) 138 auto i = v.begin(); 139 v.erase(i); 140 //++i 报错(原因:迭代器失效) 141 for (auto i = v.begin(); i != v.end();) { 142 if (*i == 3) 143 i = v.erase(i); //返回元素的作用 144 else 145 ++i; 146 } 147 for (auto i : v) 148 cout << i << " "; 149 cout << endl; 150 //erase 删除一个范围元素并返回下一个元素位置 151 v.erase(v.begin()+1, v.end()-1); 152 for (auto i : v) 153 cout << i << " "; 154 cout << endl; 155 //clear() 删除所有元素 156 v.clear(); 157 for (auto i : v) 158 cout << i << " "; 159 cout << endl; 160 */ 161 //查找 162 vector<char> v = { 'A', 'B', 'C'}; 163 auto it = find(v.begin(), v.end(), 'A'); 164 if (it != v.end()) 165 cout << "成功找到A"; 166 cout << endl; 167 system("pause"); 168 return 0; 169 }
成为更好的自己。