一、首先必须弄清楚两个概念:
- capacity:指容器在分配新的存储空间之前能存储的元素总数。
- size:指当前容器所存储的元素个数
由于vector对象在插入或添加时自动调整长度(注意:只会自动增加长度,不会自动减小长度),因此可以将vector的长度设置成0;当然,也可指定存储长度:
vector<typename> objection_name(n_elem);//存储n_elem个类型为typename的元素;
二、push_back()
push_back()成员函数的作用是在容器尾部添加一个元素。
三、push_back后iterator程序崩溃的原因分析
先看以下程序:
#include<iostream>
#include<vector>
using namespace std;
void display(vector<int>::iterator &p_num_,vector<int> &num_ )
{
while (p_num_ != num_.end())
{
cout << *p_num_ << endl;
p_num_++;
}
}
int main()
{
vector<int> num(100);
num = { 1,2,3,4,5,6 };
cout << "the capacity of num :" << num.capacity() << endl;
vector<int>::iterator p_num = num.begin();
display(p_num, num);
int a[5] = { 11,22,33,44,55 };
for (int i = 0; i < 5; i++)
{
num.push_back(a[i]);
cout << "after " << i << " push_back() the capacity is:" <<num.capacity()<< endl;
}
display(p_num, num);//error!!!
system("pause");
return 0;
}
在第二次display时出现错误,究其原因如下:
vector容器是动态分配内存的,在初始化1,2,3…,6后,当前的vector对象num的大小是6,若此时通过push_back()向num中添加元素,此时,编译器会重新分配一段内存用来保存此时的所有元素。而此时,迭代器p_num里面存放的依然是为扩容之前的地址,扩容后(由于是动态内存分配)num对象的地址发生改变,原来的p_num指向的是一块未知的内存空间,造成内存泄漏;
四、改进
#include<iostream>
#include<vector>
using namespace std;
void display(vector<int>::iterator &p_num_,vector<int> &num_ )
{
while (p_num_ != num_.end())
{
cout << *p_num_ << endl;
p_num_++;
}
}
int main()
{
vector<int> num(100);
num = { 1,2,3,4,5,6 };
cout << "the capacity of num :" << num.capacity() << endl;
vector<int>::iterator p_num = num.begin();
display(p_num, num);
int a[5] = { 11,22,33,44,55 };
for (int i = 0; i < 5; i++)
{
num.push_back(a[i]);
cout << "after " << i << " push_back() the capacity is:" <<num.capacity()<< endl;
}
vector<int>::iterator p_num_= num.begin();
display(p_num_, num);//重新定义一个迭代器p_num_;
system("pause");
return 0;
}
五、验证
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAXNUM = 5;
int main()
{
vector<int> num_data;
cout << num_data.capacity() << endl;
for (int i = 0; i < MAXNUM; i++)
{
num_data.push_back(i);
cout <<"realtime capacity of vector:"<< num_data.capacity() << endl;
cout << "the address of first number:" << &num_data[0] << endl;
}
vector<int> num_data_1(10);
for (int i = 0; i < MAXNUM+10; i++)
{
num_data_1.push_back(i);
cout << "realtime capacity of vector:" << num_data_1.capacity() << endl;
cout << "the address of first number:" << &num_data_1[0] << endl;
}
system("pause");
return 0;
}
}
0
realtime capacity of vector:1
the address of first number:00751638
realtime capacity of vector:2
the address of first number:00752550
realtime capacity of vector:3
the address of first number:00752908
realtime capacity of vector:4
the address of first number:00753498
realtime capacity of vector:6
the address of first number:0074DFF0
realtime capacity of vector:15
the address of first number:00752FA8
realtime capacity of vector:15
the address of first number:00752FA8
realtime capacity of vector:15
the address of first number:00752FA8
realtime capacity of vector:15
the address of first number:00752FA8
realtime capacity of vector:15
the address of first number:00752FA8
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:22
the address of first number:00745040
realtime capacity of vector:33
the address of first number:0074A0E8
realtime capacity of vector:33
the address of first number:0074A0E8
realtime capacity of vector:33
the address of first number:0074A0E8
请按任意键继续. . .
可见,在未确定vector对象的容量时,每进行一次push_back()就重新分配一段内存。
若第定义vector对象的大小后,当空间未被存储满时,不会动态分配内存,仅当内存空间不够时,才重新分配空间;
注意:
在指定vector对象num大小后,通过num.capacity()返回的空间总比定义的高处50%;