代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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();return 0;
}
测试结果:
总结:
vector的多种构造方式没有可比性,灵活使用即可。
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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();return 0;
}
测试结果:
总结:
vector赋值方式比较简单,使用operator=,或者assign都可以。
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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 capacity = " << v1.capacity() << endl;cout << "v1 size = " << v1.size() << endl;}v1.resize(15, 10);printVector(v1);v1.resize(5);printVector(v1);
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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);}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 front elem = " << v1.front() << endl;cout << "v1 final elem = " << v1.back() << endl;}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;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 capacity = " << v.capacity() << endl;cout << "v size = " << v.size() << endl;v.resize(3);cout << "v capacity = " << v.capacity() << endl;cout << "v size = " << v.size() << endl;//收缩内存vector<int>(v).swap(v);cout << "v capacity = " << v.capacity() << endl;cout << "v size = " << v.size() << endl;}int main()
{test01();test02();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;void printVector(vector<int >&v)
{for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{vector<int>v;/*v.reserve(100000);*/int num = 0;int *p = nullptr;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();return 0;
}
测试结果:
代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;void printVector(vector<int >&v)
{for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{vector<int>v;v.reserve(100000);int num = 0;int *p = nullptr;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();return 0;
}
测试结果:
总结: