# Construct Vector

vector的初始化函数有6种，分别是 default、fill、range、copy、move、initializer list。

1 vector<int> fill_(5, 100);
2 vector<int> copy_(fill_);
3 vector<int> range_(fill_.begin(), fill_.end());
4 // move貌似是右值引用，没举例子
5 int myints[] = {1, 2, 3, 4};
6 vector<int> init_list(myints, myints + sizeof(myints) / sizeof(int));

# member function

## 1. assign

vector<int> firsrt;
vector<int> second;
vector<int> third;

first.assign(4, 100);
second.assign(first.begin(), first.end());

int myints[] = {1, 2, 3};
third.assign(myints, myints + sizeof(myints) / sizeof(int));

## 2. at

vector<int> first(4, 100);
first.at(2) = 200;

## 3. back

1 vector<int> first(4, 100);
2 first.back() = 200;

## 4. begin && end

for(vector<int>::iterator i = v.begin(); i != v.end(); ++i) {}

## 5. capacity && max_size && size

vector<int> first;
for (int i=0; i<100; ++i) first.push_back(i);
cout << first.size() << endl;        //100
cout << first.capacity() << endl;   //141
cout << first.max_size() << endl; //1073741823

## 6. cbegin && cend（C++11）

for(auto i = first.cbegin(); i != first.cend(); ++i) {}

## 7. clear

first.clear();

## 8. rbegin && rend

for(auto i = first.rbegin(); i != rend(); ++i) {};

crbegin和crend就不介绍了。

## 9. data（C++11）

int *p = first.data();
cout << *(++p) << endl;

## 10. emplace(C++11)

1 // 最后只在第一个成功插入43，其他的没有
2 first.emplace(first.begin(), 200, 1, 2, 3, 43);

## 11. emplace_back(C++11)

first.emplace_back (100, 100);

## 13. earse

first.earse(first.begin());
first.earse(first.begin(), first.begin()+5);

first.earse(
remove(first.begin(), first.end(), 6),
first.end());

## 14. get_allocator()

int *p;
p = first.get_allocator().allocate(5);

## 15. insert()

first.insert(first.begin(), 300);
first.insert(first.begin(), 2, 300);
first.insert(first.begin(), second.begin(), second.end());
int myints = {1, 2, 3}
first.inert(first.begin(), myints, myints+3);

## 19. resize

myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);

# 参考

posted @ 2017-03-03 23:56  mangoyuan  阅读(...)  评论(...编辑  收藏