中易网

C++ 容器元素的初始化

答案:4  悬赏:40  
解决时间 2021-02-03 09:43
//default string constructor and five string copy constructors invoked
vector<string> svec(5);
书上说,编译器首先使用string默认构造函数创建一个临时值来初始化svec,然后使用复制构造函数将临时值复制到svec的每一个元素。

1、首先这个临时值是一个string类型的临时值吧,用它来初始化svec,是不是创建了一个空的string类型的容器?

2、然后说调用复制构造函数,是一个什么样的复制构造函数,从英文解释来看,应该是string类的复制构造函数,但是怎么能用上面那个临时制调用string类的复制构造函数呢?

还是我理解的根本就不对,求高手们指点!?
最佳答案
程序清单:

#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <deque>

using namespace std;

int main(int argc,char *argv[])
{
char *words[] = {"first","second","third","fourth","fifth"};

size_t words_size = sizeof(words)/sizeof(char *);


vector<string> words1(words,words+words_size);

list<string> words2(words,words+words_size);

cout<<"The elements of words1 are below:"<<endl;
for(vector<string>::iterator it=words1.begin();
it!=words1.end();++it)
{
cout<<*it<<" ";
}
cout<<endl;

cout<<"The elements of words2 are below:"<<endl;
for(list<string>::iterator it=words2.begin();it!=words2.end();
++it)
{
cout<<*it<<" ";
}
cout<<endl;


vector<string>::iterator mid = words1.begin() + words1.size()/2;


vector<string> vfront(words1.begin(),mid);

vector<string> vback(mid,words1.end());



deque<string> dfront(words1.begin(),mid);

deque<string> dback(mid,words1.end());

cout<<"The elements of vfront are below:"<<endl;
for(vector<string>::iterator it=vfront.begin();it!=vfront.end();
++it)
{
cout<<*it<<" ";
}
cout<<endl;

cout<<"The elements of vback are below:"<<endl;
for(vector<string>::iterator it=vback.begin();it!=vback.end();
++it)
{
cout<<*it<<" ";
}
cout<<endl;

cout<<"The elements of dfront are below:"<<endl;
for(deque<string>::iterator it=dfront.begin();it!=dfront.end();
++it)
{
cout<<*it<<" ";
}
cout<<endl;

cout<<"The elements of dback are below:"<<endl;
for(deque<string>::iterator it=dback.begin();it!=dback.end();
++it)
{
cout<<*it<<" ";
}
cout<<endl;


list<string> slist(25,"love");
int count = 0;
cout<<"The elements of slist are below:"<<endl;
for(list<string>::iterator it=slist.begin();it!=slist.end();
++it)
{
cout<<*it<<" ";
++count;
if(count%5 == 0)
{
cout<<endl;
}
}
// cout<

return 0;
}
编译并运行程序后的执行结果:
The elements of words1 are below:
first second third fourth fifth
The elements of words2 are below:
first second third fourth fifth
The elements of vfront are below:
first second
The elements of vback are below:
third fourth fifth
The elements of dfront are below:
first second
The elements of dback are below:
third fourth fifth
The elements of slist are below:
love love love love love
love love love love love
love love love love love
love love love love love
love love love love love
全部回答
对于vector容器来说,声明变量后讲调用构造函数,假如你已经开始学习面向对象编程了,你将容易理解。 vector x(5);调用一个参数的构造函数,构造出具有5个int型值得对象,默认值为0 (5,5);调用2个参数的构造函数,初始化为5个相同的元素,其值为5。 而(5,5,5);没有对应的构造函数,将调用错误。
书上的意思大概是 svec[0]=string(); for (size_t i=1;i<5;i++) svec[i]=svec[0]; 之类的东西吧……
书上说的有问题,做一个实验即可验证: 这是vector的一个构造函数,const Allocator&是迭代器,不用管 explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); Repetitive sequence constructor: Initializes the vector with its content set to a repetition, n times, of copies of value. 从这句翻译上是,n次调用构造函数(类型是T,value)来初始化vector对象的每一个元素,而不是仅仅调用一次T类型的构造函数,在这里的T相当于你的string,value使用默认,为空string 实验: #include<iostream> #include<vector> #include<string> using namespace std; 例: class string1{ public: string1(){cout<<"default"<<endl;}; string1(const string1& x){cout<<"copy"<<endl;} }; int main(){ vector<string1> sec(5); return 0; } 运行结果为5个default,也就是说不调用赋制构造函数的,更说明书上说的是错误的 当然你的问题也就回答了, 自己运行一下这个程序,应该明白的
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
UG6.0界面模糊怎么办
喜欢吃油腻,辛辣食物对人身体的影响
寺坪大桥/S305(路口)在什么地方啊,我要过去
临床上怎样合理选用利尿药和脱水药?
给城建局的请示怎么写
已知直线l的极坐标方程为 θ=3π/4,曲线c的参
宁波哪个学校有开设机电一体化专业
金良餐厅地址在什么地方,想过去办事
黄瓜鸡蛋减肥同时能吃番茄和桃吗
洛阳铺铺旺能帮助找店吗?效果怎么样?
奥拉星奥天怎么打?
SDMT-159r女主角 第一个是谁 谢谢
爱尚美烫染造型在什么地方啊,我要过去处理事
铭瑄MS-M3N52L主板 AMD X255 铭瑄GT240显卡
左眼皮上有一个黑痣,是好还是坏?
推荐资讯
呈贡龙顺达蔬菜包装用品经营部地址在什么地方
磨砂玻璃上的水渍及污渍如何去除酒店卫生间内
鸡东县文化广电体育局地址有知道的么?有点事
酒店和物业公司关于消防之间如何发公函
做凉皮用的辣椒油的方法
民勤县康盛塑业有限公司在哪里啊,我有事要去
steiff 中国专卖店
广东炒米粉砂锅我想知道这个在什么地方
儿童性早熟是乳房先发育还是子宫先发育
venue, address区别
四级词汇是不是就是高中所学的单词?
家里添了孙子 安老辈的说法叫添什么之喜呀?
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?