理论知识
² vector.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
² vector.insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
² vector.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值
简单案例
vector<int> vecA;
vector<int> vecB;
vecA.push_back(1);
vecA.push_back(3);
vecA.push_back(5);
vecA.push_back(7);
vecA.push_back(9);
vecB.push_back(2);
vecB.push_back(4);
vecB.push_back(6);
vecB.push_back(8);
vecA.insert(vecA.begin(), 11); //{11, 1, 3, 5, 7, 9}
vecA.insert(vecA.begin()+1,2,33); //{11,33,33,1,3,5,7,9}
vecA.insert(vecA.begin() , vecB.begin() , vecB.end() ); //{2,4,6,8,11,33,33,1,3,5,7,9}
测试代码:
[cpp] view plain copy
#include <iostream> using namespace std; #include "vector" void printV(vector<int> &v) { for (int i = 0; i < v.size();i++) { cout <<v[i]<< " "; } cout << endl; } //插入操作 void main1() { vector<int> v1; v1.insert(v1.begin(),1);//在开始位置插入元素1 printV(v1); v1.insert(v1.begin(),2,33);//在开始位置插入两个33 printV(v1); v1.insert(v1.begin(),v1.begin(),v1.end());//在开始位置插入整个区间元素 printV(v1); } int main() { cout << "插入操作:" << endl; main1(); return 0; }
理论知识
² vector.clear(); //移除容器的所有数据
² vec.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
² vec.erase(pos); //删除pos位置的数据,返回下一个数据的位置。
简单案例:
删除区间内的元素
vecInt是用vector<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。
vector<int>::iterator itBegin=vecInt.begin()+1;
vector<int>::iterator itEnd=vecInt.begin()+2;
vecInt.erase(itBegin,itEnd);
//此时容器vecInt包含按顺序的1,6,9三个元素。
假设 vecInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素
for(vector<int>::iterator it=vecInt.being(); it!=vecInt.end(); ) //小括号里不需写 ++it
{
if(*it == 3)
{
it = vecInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
//此时,不执行 ++it;
}
else
{
++it;
}
}
//删除vecInt的所有元素
vecInt.clear(); //容器为空
测试代码:
[cpp] view plain copy
#include <iostream> using namespace std; #include "vector" void printV(vector<int> &v) { for (int i = 0; i < v.size();i++) { cout <<v[i]<< " "; } cout << endl; } //删除操作 void main2() { vector<int> v1(10); for (int i = 0; i < v1.size(); i++) { v1[i] = i + 1; } printV(v1); v1.erase(v1.begin(),v1.begin()+4);//区间删除 printV(v1); v1.erase(v1.begin());//头部删除 printV(v1); v1[0] = 23; v1[1] = 22; printV(v1); for (vector<int>::iterator it = v1.begin(); it != v1.end();)//删除容器里等于8的元素 { if (*it == 8) { it = v1.erase(it); } else it++; } printV(v1); } int main() { cout << "删除操作:" << endl; main2(); return 0; }
实战c++中的vector系列--C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
一蓑烟雨1989 2015-12-19 18:07:00 6934 收藏 3 分类专栏: C++ 实战c++中的vector系列 版权 前面提到的emplace_back就是C++11所增加的内容。
所以这篇博客就是想罗列一下C++11对vector容器的扩充。
std::vector::cbegin和std::vector::cend 这两个方法是与std::vector::begin和std::vector::end相对应的,从字面就能看出来,多了一个’c’,顾名思义就是const的意思。 所以: std::vector::cbegin: Returns a const_iterator pointing to the first element in the container. std::vector::cend: Returns a const_iterator pointing to the past-the-end element in the container.
#include <iostream> #include <vector> int main () { std::vector<int> myvector = {10,20,30,40,50}; std::cout << "myvector contains:"; for (auto it = myvector.cbegin(); it != myvector.cend(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 40 50std::vector::crbegin和std::vector::crend 这两个方法就不解释了,与上面的相比就是多了个’r’, reverse的缩写,反转迭代器,代码就省略了。
std::vector::emplace 之前已经对emplace_back进行了讨论,其实还有一个方法叫emplace。 我想说的就是,emplace之于emplace_back就像insert之于push_back。 看英文描述就直观:
emplace:Construct and insert element emplace_back:Construct and insert element at the end
如何使用:
#include <iostream> #include <vector> int main () { std::vector<int> myvector = {10,20,30}; auto it = myvector.emplace ( myvector.begin()+1, 100 ); myvector.emplace ( it, 200 ); myvector.emplace ( myvector.end(), 300 ); std::cout << "myvector contains:"; for (auto& x: myvector) std::cout << ' ' << x; std::cout << '\n'; return 0; }Output: myvector contains: 10 200 100 20 30 300 std::vector::data Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
#include <iostream> #include <vector> int main () { std::vector<int> myvector (5); int* p = myvector.data(); *p = 10; ++p; *p = 20; p[2] = 100; std::cout << "myvector contains:"; for (unsigned i=0; i<myvector.size(); ++i) std::cout << ' ' << myvector[i]; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 0 100 0std::vector::shrink_to_fit Requests the container to reduce its capacity to fit its size. 就是减少空间
#include <iostream> #include <vector> int main () { std::vector<int> myvector (100); std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n'; std::cout << "1. size of myvector: " << myvector.size() << '\n'; myvector.resize(10); std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n'; std::cout << "2. size of myvector: " << myvector.size() << '\n'; myvector.shrink_to_fit(); std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n'; std::cout << "3. size of myvector: " << myvector.size() << '\n'; return 0; } //输出 1. capacity of myvector: 100 1. size of myvector: 100 2. capacity of myvector: 100 2. size of myvector: 10 3. capacity of myvector: 10 3. size of myvector: 10#include <iostream> #include <string> #include <vector> using namespace std; struct Foo{ public: Foo () = default; Foo(int n,double x):id(n),price(x){} void print()const { cout << "id is:" << id; cout << ";price is:" << price << "."; } private: int id = 110; double price = 1.123; }; typedef vector<Foo> VECTORFOO; void print(const VECTORFOO &); int main() { VECTORFOO v; v.emplace(v.begin(),42,3.1416); v.emplace(v.begin(),Foo(43,3.1417)); // v.insert(v.begin(),{44,3.1418}); v.emplace_back(Foo(44,3.1418)); v.emplace_back(Foo(45,3.1419)); v.emplace(v.end(),Foo(46,3.1420)); print(v); return 0; } void print(const VECTORFOO & vec) { int i = 1; for(VECTORFOO::const_iterator it = vec.begin() ; it != vec.end(); ++it) { cout << "Elem " << i <<" is:"; (*it).print(); cout << endl; ++i; } } r@r-Sys:~/9/delete_and_add_element$ ./123 Elem 1 is:id is:43;price is:3.1417. Elem 2 is:id is:42;price is:3.1416. Elem 3 is:id is:44;price is:3.1418. Elem 4 is:id is:45;price is:3.1419. Elem 5 is:id is:46;price is:3.142.