- reverse vs resize
- reserve: size-内容不变,指定的reserve参数 > size 变化,<size 不变
- resize: 截掉/扩充vector,if(resize参数 < capacity)capacity不变,else{ vector扩容,capacity肯定改变}
- capacity vs size
要分清哦
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::cout << "\n=======测试resize===\n" << endl;
vector<int> v1{1, 2, 3, 4, 5};
std::cout << "v1 source data is : " << "\n";
for (auto& v : v1)
{
std::cout << v << " ";
}
std::cout << "\n" << " v1.size=" << v1.size() << " v1.capacity=" << v1.capacity() << endl;
std::cout << "after resize to 2" << endl;
v1.resize(2);
for (auto& v : v1)
{
std::cout << v << " ";
}
std::cout << " v1.size=" << v1.size() << " v1.capacity=" << v1.capacity() << endl;
std::cout << "after resize to 12" << endl;
v1.resize(12, 10000);
for (auto& v : v1)
{
std::cout << v << ", ";
}
std::cout << "\n" << " v1.size=" << v1.size() << " v1.capacity=" << v1.capacity() << endl;
v1.push_back(88);
std::cout << "\n" << "v1扩容, v1.size=" << v1.size() << " v1.capacity=" << v1.capacity() << endl;
std::cout << "\n=======测试reverse===\n" << endl;
vector<int> v2{1, 2, 3, 4, 5};
std::cout << "v2 source data is : " << "\n";
for (auto& v : v2)
{
std::cout << v << ", ";
}
std::cout << "\n" << " v2.size=" << v2.size() << " v2.capacity=" << v2.capacity() << endl;
std::cout << "after reserve to 2" << endl;
v2.reserve(2);
for (auto& v : v2)
{
std::cout << v << ", ";
}
std::cout << "\n" << " v2.size=" << v2.size() << " v2.capacity=" << v2.capacity() << endl;
std::cout << "\nafter reserve to 12" << endl;
v2.reserve(12);
for (auto& v : v2)
{
std::cout << v << ", ";
}
std::cout << "\n" << " v2.size=" << v2.size() << " v2.capacity=" << v2.capacity() << endl;
return 0;
}