可以简单的将vector想象成一个可扩容的线性表
vector的初始化
int main()
{
int ar[] = { 12,23,34 };
vector<int>ar1;
vector<int>ar2 = {12,23,34,45,56};
vector<int>ar3(10, 23);
vector<int>ar4({ 12,23,34,45,56 });
vector<int>ar5(ar1);
vector<int>ar6(std::move(ar2));
ar1 = { 12,23,34,45 };
ar1 = std::move(ar3);
return 0;
}
int main()
{
vector<Object> objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
return 0;
}
先构建4个对象,然后在vector里面拷贝构造
通过迭代器对vector里面的数据进行访问
#include<vector>
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << "Object" << endl; }
Object(const Object& obj) :value(obj.value) { cout << "Object &" << endl; }
Object(Object&& obj) :value(obj.value) { cout << "move Object&&" << endl; }
Object& operator=(const Object& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
Object& operator=(const Object&& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
~Object()
{
cout << "~Object" << endl;
}
int& Value()
{
return value;
}
};
int main()
{
vector<Object> objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
for (int i = 0; i < objvec.size(); ++i)
{
cout << objvec[i].Value() << endl;
}
for (int i = 0; i < objvec.capacity(); ++i)
{
cout << objvec.at(i).Value() << endl;
}
vector<Object>::iterator it = objvec.begin();
for (; it != objvec.end(); ++it)
{
cout << it->Value() << " " << (*it).Value() << endl;
}
it = objvec.begin();
for (; it != objvec.end(); ++it)
{
cout << it->Value() << " " << endl;
it->Value() += 100;
cout << (*it).Value() << " "<< endl;
}
vector<Object>::const_iterator cit = objvec.begin();
for (; cit != objvec.end(); ++cit)
{
}
return 0;
}
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
for (auto x : objvec)
{
cout << x.Value() << endl;
}
return 0;
}
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
for (const auto &x : objvec)
{
cout << x.Value() << endl;
}
return 0;
}
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
Object* op = objvec.data();
cout << op->Value() << endl;
op->Value() += 10;
op->~Object();
delete[]op;
}
reserve
#include<vector>
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << "Object" << endl; }
Object(const Object& obj) :value(obj.value) { cout << "Object &" << endl; }
Object(Object&& obj) :value(obj.value) { cout << "move Object&&" << endl; }
Object& operator=(const Object& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
Object& operator=(const Object&& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
~Object()
{
cout << "~Object" << endl;
}
int& Value()
{
return value;
}
};
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.reserve(10);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
扩容导致迭代器失效
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
vector<Object>::iterator it = objvec.begin();
Object& obj = objvec.back();
objvec.reserve(10);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
resize
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.resize(10);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.resize(10);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.resize(2);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
initializer_list<Object>arr = { Object(10),Object(20) };
int a{ 10 };
int* p{ nullptr };
int ar[]{ 12,23,34 };
vector<int>veci{ 12,23,34 };
list<int>ilist{ 12,23,34,45 };
objvec.resize(10);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
列表方式初始化
#include<initializer_list>
template<class T>
class SeqList
{
T* data;
int maxsize;
int cursize;
public:
SeqList(int sz = 10) :data(T new[sz], maxsize(sz), cursize(sz)) {}
SeqList(std::initializer_list<T>init)
{
maxsize = init.size();
cursize = maxsize;
data = (T*)malloc(sizeof(T) * maxsize);
}
};
int main()
{
SeqList<int>seqlist{ 12,23,34,45 };
return 0;
}
assign
注意这几种扩容均可能导致迭代器失效
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
for (auto& x : objvec)
{
cout << x.Value() << endl;
}
objvec.assign(10, Object(100));
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
for (auto& x : objvec)
{
cout << x.Value() << endl;
}
}
reserve(1000)空间太大可用空间过多如何处理
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.reserve(100000);
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
{
vector<Object>tmp(objvec);
tmp.swap(objvec);
}
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
}
插入函数
push_back
push_back(100);是隐士构造,先创建无名对象object在对其拷贝构造,注意这里只有在单参构造函数里面才可以,如果你想拒绝隐士构建在但参构造函数前加explicit
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
vector<Object>::iterator it = objvec.begin();
cout << it->Value() << endl;
objvec.push_back(Object(100));
cout << it->Value() << endl;
}
插入函数可能会导致迭代器失效
insert函数
int main()
{
vector<Object>objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
vector<Object>::iterator it = objvec.begin();
objvec.insert(it,Object(20));
return 0;
}
emplace_back()原位构建
int main()
{
vector<Object>objvec;
objvec.reserve(10);
objvec = { Object(10),Object(20),Object(30),Object(40) };
cout << objvec.size() << endl;
cout << objvec.capacity() << endl;
objvec.emplace_back(100);
}
template<class T,class ...Arg>
void Make(T* p, Arg... arg)
{
new(p) T(arg...);
}
int main()
{
Object* p = (Object*)malloc(sizeof(Object));
Object obj(12);
Make(p, 12);
Make(p, 12, 23);
Make(p, std::move(obj));
}
|