我一直怀疑《C++ Primer Answer book》这本书是不是太过急于出版了。对于练习6.7,我写了一个test程序:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<string>svec;
svec.reserve(1024);
int n;
cout<<" please input n : ";
cin>>n;
do
{
if(n == 0) return 0;
for(int i = 0; i < n; i++)
svec.push_back("a");
svec.resize( svec.size() + svec.size()/2 );
cout <<"after resize "<<svec.size()<<endl;
cout <<" max size "<<svec.max_size()<<endl;
cout <<"capacity size "<<svec.capacity()<<endl;
cout<<" please input n: ";
cout<<endl;
svec.clear();
}while(cin>>n);
}
gnu c++编译器上的执行的结果如下:
please input n : 256
after resize 384
max size 1073741823
capacity size 1024
please input n: 512
after resize 768
max size 1073741823
capacity size 1024
please input n: 1000
after resize 1500
max size 1073741823
capacity size 2000
please input n: 1048
after resize 1572
max size 1073741823
capacity size 2000
please input n: 0 //Exit
vc2003编译器执行结果如下:
please input n : 256
after resize 384
max size 153391689
capacity size 1024
please input n: 512
after resize 768
max size 153391689
capacity size 1066
please input n: 1000
after resize 1500
max size 153391689
capacity size 1599
please input n: 1048
after resize 1572
max size 153391689
capacity size 1599
please input n: 0 //exit
结论:
这道题没有确切的答案,如果谁有了确切的答案,那肯定是错误的!
本文地址:http://com.8s8s.com/it/it25407.htm