标签:last let cap first c++ erase const highlight delete
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int ar[10] = { 1,2,3,4,5,6,7,8,9,0 };
std::vector<int> vec5(ar, ar + 10);
// reverse
size_t cap1 = vec5.capacity(); // = 10
vec5.reserve(20); // = Request a change in capacity
size_t cap2 = vec5.capacity(); // = 20
// data()
int * pInt = vec5.data();
for (size_t i=0; i<vec5.size(); ++i)
{
cout << pInt[i];
}
cout << endl; // 1234567890
// begin end
for (auto it=vec5.begin(); it != vec5.end(); ++it)
{
cout << *it;
}
cout << endl; // 1234567890
// cbegin cend 常量,保证不改变 vector 中的元素
for (auto it = vec5.cbegin(); it != vec5.cend(); ++it)
{
cout << *it;
}
cout << endl; // 1234567890
// rbegin rend 注意偏移是 ++ 操作
for (auto it = vec5.rbegin(); it != vec5.rend(); ++it)
{
cout << *it;
}
cout << endl; // 0987654321
// iterator erase (const_iterator position);
vec5.erase(vec5.begin()); // delete 1; size = 9
// iterator erase (const_iterator first, const_iterator last);
vec5.erase(vec5.begin(), vec5.begin()+2); // delete 2, 3; size = 7
// max_size; Returns the maximum number of elements that the vector can hold.
// 系统或者库的设计上线。并非机器所能申请的最大大小。
size_t maxSize = vec5.max_size();
std::vector<int> vec6(2);
vec5.swap(vec6); // 两者交换
//vec5.swap(std::vector<int>()); // 2015支持,2017不支持
}
标签:last let cap first c++ erase const highlight delete
原文地址:https://www.cnblogs.com/alexYuin/p/12032231.html