vector理解
之前一直以为自己对std::vector比较了解。当一次偶然的机会,在导航项目中,突然发现。自己的认知还是差的太多,今天就将发现的问题记录下来。希望自己能继续成长!
std::vector<int> vecNum;
printf("init capacity = %d\n", vecNum.capacity());
printf("init size = %d\n", vecNum.size());
vecNum.push_back(1);
vecNum.push_back(2);
vecNum.push_back(3);
vecNum.push_back(4);
vecNum.push_back(5);
printf("capacity = %d\n", vecNum.capacity());
printf("size = %d\n", vecNum.size());
for (int i = 0; i < vecNum.size(); ++i)
{
printf("vecNum[%d] = %d\n", i, vecNum[i]);
}
printf("\n");
vecNum.erase(vecNum.begin());
for (int i = 0; i < vecNum.size(); ++i)
{
printf("vecNum[%d] = %d\n", i, vecNum[i]);
}
printf("erase capacity = %d\n", vecNum.capacity());
printf("erase size = %d\n", vecNum.size());
其实大家要是能在这个程序中看懂一些细节。那就证明你真的比我强多了。我要向你学习才对!