vector<int>v; //空容器 执行默认初始化
vector<int>v2(v1); //v2包含v1的所有副本
vector<int>v2 = v1; //等价于vectorv2(v1);
vector<int>v3(n,val); //v3包含n个val值
vector<int>v4(n); //包含了n个执行了值初始化的对象(int默认0)
vector<vector<int>>v5; //组成vector的元素也可以是vector
vector<int>v6{1,2,3}; //初始化赋值
vector<int>v6 = {1,2,3}; //等价于vectorv6{1,2,3};
vector<int>v7(v6.begin(),v6.end()); //传递两个迭代器,构造一个初始值为[begin,end)区间元素的Vector(注:半开区间).
v1 == v2
v1 != v2
v1 <= v2
v1 >= v2
v1 < v2
v1 > v2
v[]
//C++ Vectors能够使用标准运算符: ==, !=, <=, >=, <, 和 >. 要访问vector中的某特定位置的元素可以使用 [] 操作符.
//两个vectors被认为是相等的,如果: 1.它们具有相同的容量 2.所有相同位置的元素相等.
//vectors之间大小的比较是按照词典规则.
void assign( input_iterator start, input_iterator end );//将区间[start, end)的元素赋到当前vector
void assign( size_type num, const TYPE &val );//赋num个值为val的元素到vector中
//assign()函数将会清除掉为vector赋值以前的内容.
vector<int> v( 5, 1 );
for( int i = 0; i < 10; i++ ) {
cout << "Element " << i << " is " << v[i] << endl;
}
Element 0 is 1
Element 1 is 1
Element 2 is 1
Element 3 is 1
Element 4 is 1
Element 5 is 1279870543
Element 6 is 1161250782
Element 7 is 10038
Element 8 is 13575144
Element 9 is 13566144
Process returned 0 (0x0) execution time : 0.083 s
Press any key to continue.
for( int i = 0; i < 10; i++ ) {
cout << "Element " << i << " is " << v.at(i) << endl;
}
Element 0 is 1
Element 1 is 1
Element 2 is 1
Element 3 is 1
Element 4 is 1
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Process returned 3 (0x3) execution time : 0.756 s
Press any key to continue.
//at() 函数 返回当前Vector指定位置loc的元素的引用.
//at() 函数 比 [] 运算符更加安全, 因为它不会让你去访问到Vector内越界的元素.
//back() 函数返回当前vector最末一个元素的引用.
//front() 函数返回当前vector起始元素的引用
vector<int> v;
for( int i = 0; i < 5; i++ ) {
v.push_back(i);
}
cout << "The first element is " << v.front()
<< " and the last element is " << v.back() << endl;
The first element is 0 and the last element is 4
Process returned 0 (0x0) execution time : 0.071 s
Press any key to continue.
iterator begin();
//begin() 函数返回一个指向当前vector起始元素的迭代器.
iterator end();
//end() 函数返回一个指向当前vector末尾元素的下一位置的迭代器.
vector<int> v1( 5, 789 );
vector<int>::iterator it;
for( it = v1.begin(); it != v1.end(); it++ )
cout << *it << endl;
size_type capacity();
//capacity() 函数 返回当前vector在重新进行内存分配以前所能容纳的元素数量.
vector<int> v{1,2,3,4,5};
cout<<v.capacity(); // 5
void clear();
//clear() 函数删除当前vector中的所有元素.
bool empty();
//如果当前vector没有容纳任何元素,则empty()函数返回true,否则返回false.
size_type size();
//size() 函数返回当前vector所容纳元素的数目
//和capacity不同的是,capacity是容量,而size是数量
iterator erase( iterator loc );
//删作指定位置loc的元素
iterator erase( iterator start, iterator end );
//删除区间[start, end)的所有元素.
//erase() 函数返回值是指向删除的最后一个元素的下一位置的迭代器.
vector<int> v{1,2,3,4,5,6,7,8,9,0};
vector<int>::iterator it = v.erase(v.begin());
cout<<*it<<endl; // 2
allocator_type get_allocator();
//get_allocator() 函数返回当前vector的内存分配器.
insert() 函数有以下三种用法:
iterator insert( iterator loc, const TYPE &val );
//在指定位置loc前插入值为val的元素,返回指向这个元素的迭代器
void insert( iterator loc, size_type num, const TYPE &val );
//在指定位置loc前插入num个值为val的元素
void insert( iterator loc, input_iterator start, input_iterator end );
//在指定位置loc前插入区间[start, end)的所有元素 .
用法1
vector<int> v{1,2,3};
vector<int>::iterator it = insert(v.begin(),9999);
cout<<*it<<endl;
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
9999
9999 1 2 3
Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.
用法2
vector<int> v{1,2,3};
v.insert(v.end(),5,21);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 2 3 21 21 21 21 21
Process returned 0 (0x0) execution time : 0.059 s
Press any key to continue.
用法3
vector<int>v3(3,100);
vector<int> v{1,2,3};
v.insert(v.begin(),v3.begin(),v3.end());
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
100 100 100 1 2 3
Process returned 0 (0x0) execution time : 0.019 s
Press any key to continue.
size_type max_size();
//max_size() 函数返回当前vector所能容纳元素数量的最大值(译注:包括可重新分配内存).
--------------------------------------------------------------------------------
vector<int>v;
cout<<v.max_size()<<endl; //1073741823
void pop_back();
//pop_back() 函数删除当前vector最末的一个元素
vector<int> v{1,2,3,99};
v.pop_back();
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 2 3
Process returned 0 (0x0) execution time : 0.017 s
Press any key to continue.
void push_back( const TYPE &val );
//push_back() 添加值为val的元素到当前vector末尾
vector<int> v{1,2,3};
v.push_back(7);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 2 3 7
Process returned 0 (0x0) execution time : 0.049 s
Press any key to continue.
reverse_iterator rbegin();
//rbegin() 函数返回指向当前vector末尾的逆迭代器.
reverse_iterator rend();
//rend() 函数返回指向当前vector起始位置的逆迭代器.
void reserve( size_type size );
//reserve() 函数为当前vector预留至少共容纳size个元素的空间.(译注:实际空间可能大于size)
void resize( size_type size, TYPE val );
//resize() 函数改变当前vector的大小为size,且对新创建的元素赋值val,无val则为0
vector<int> v{1,2,3,4,5,6,7,8,9,0};
v.resize(15);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 2 3 4 5 6 7 8 9 0 0 0 0 0 0
Process returned 0 (0x0) execution time : 0.021 s
Press any key to continue.
vector<int> v{1,2,3,4,5,6,7,8,9,0};
v.resize(5);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 2 3 4 5
Process returned 0 (0x0) execution time : 0.018 s
Press any key to continue.
void swap( vector &from );
//swap() 函数交换当前vector与vector from的元素
vector<int>v3(3,100);
vector<int> v{1,2,3,4,5,6,7,8,9,0};
v.swap(v3);
for(vector<int>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
100 100 100
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
data();
// 返回指向内存中数组第一个元素的指针
vector<int> v{2,4,6};
cout<<*(v.data())<<endl; // 2
cout<<*(v.data()+2)<<endl; // 6
shrink_to_fit();
//请求移除未使用的容量。
vector<int> v(100,10);
cout<<v.capacity()<<endl; //100
v.clear();
cout<<v.capacity()<<endl; //100
v.shrink_to_fit();
cout<<v.capacity()<<endl; // 0
flip();
// 翻转所有位
vector<bool> v{0,1,0,1,0,1,1,0,100};
v.flip();
for(vector<bool>::iterator it = v.begin();it!=v.end();it++)
cout<<*it<<" ";
1 0 1 0 1 0 0 1 0
Process returned 0 (0x0) execution time : 0.076 s
Press any key to continue.
static void swap(reference x, reference y);
//交换 x 与 y 的内容
std::vector<bool> vb1{ 1,0 };
for (auto e : vb1) { std::cout << e << " "; }
std::cout << '\n';
vb1.swap(vb1[0], vb1[1]);
for (auto e : vb1) { std::cout << e << " "; }