C++提高编程 vector容器
```C++ #include <vector> void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector<int> v1; //无参构造 for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector<int> v2(v1.begin(), v1.end()); printVector(v2); vector<int> v3(10, 100); printVector(v3); vector<int> v4(v3); printVector(v4); } int main() { test01(); system("pause"); return 0; } ``` **总结:**vector的多种构造方式没有可比性,灵活使用即可
```C++ #include <vector> void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; } //赋值操作 void test01() { vector<int> v1; //无参构造 for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector<int>v2; v2 = v1; printVector(v2); vector<int>v3; v3.assign(v1.begin(), v1.end()); printVector(v3); vector<int>v4; v4.assign(10, 100); printVector(v4); } int main() { test01(); system("pause"); return 0; } ``` 总结: vector赋值方式比较简单,使用operator=,或者assign都可以
#include <vector> void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); if (v1.empty()) { cout << "v1为空" << endl; } else { cout << "v1不为空" << endl; cout << "v1的容量 = " << v1.capacity() << endl; cout << "v1的大小 = " << v1.size() << endl; } //resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充 v1.resize(15,10); printVector(v1); //resize 重新指定大小 ,若指定的更小,超出部分元素被删除 v1.resize(5); printVector(v1); } int main() { test01(); system("pause"); return 0; }
#include <vector> void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; } //插入和删除 void test01() { vector<int> v1; //尾插 v1.push_back(10); v1.push_back(20); v1.push_back(30); v1.push_back(40); v1.push_back(50); printVector(v1); //尾删 v1.pop_back(); printVector(v1); //插入 v1.insert(v1.begin(), 100); printVector(v1); v1.insert(v1.begin(), 2, 1000); printVector(v1); //删除 v1.erase(v1.begin()); printVector(v1); //清空 v1.erase(v1.begin(), v1.end()); v1.clear(); printVector(v1); } int main() { test01(); system("pause"); return 0; }
#include <vector> void test01() { vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } for (int i = 0; i < v1.size(); i++) { cout << v1[i] << " "; } cout << endl; for (int i = 0; i < v1.size(); i++) { cout << v1.at(i) << " "; } cout << endl; cout << "v1的第一个元素为: " << v1.front() << endl; cout << "v1的最后一个元素为: " << v1.back() << endl; } int main() { test01(); system("pause"); return 0; } ``` 总结: * 除了用迭代器获取vector容器中元素,[ ]和at也可以 * front返回容器第一个元素 * back返回容器最后一个元素
#include <vector> void printVector(vector<int>& v) { for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector<int>v2; for (int i = 10; i > 0; i--) { v2.push_back(i); } printVector(v2); //互换容器 cout << "互换后" << endl; v1.swap(v2); printVector(v1); printVector(v2); } void test02() { vector<int> v; for (int i = 0; i < 100000; i++) { v.push_back(i); } cout << "v的容量为:" << v.capacity() << endl; cout << "v的大小为:" << v.size() << endl; v.resize(3); cout << "v的容量为:" << v.capacity() << endl; cout << "v的大小为:" << v.size() << endl; //收缩内存 vector<int>(v).swap(v); //匿名对象
//匿名对象的特点,当前匿名对象执行完成后,编译器发现是匿名对象,立即把内存空间释放了
cout << "v的容量为:" << v.capacity() << endl; cout << "v的大小为:" << v.size() << endl; } int main() { test01(); test02(); system("pause"); return 0; }
#include <vector> void test01() { vector<int> v; //预留空间 v.reserve(100000); int num = 0; int* p = NULL; for (int i = 0; i < 100000; i++) { v.push_back(i); if (p != &v[0]) { p = &v[0]; num++; } } cout << "num:" << num << endl; } int main() { test01(); system("pause"); return 0; }