为了账号安全,请及时绑定邮箱和手机立即绑定

还是我理解的根本就不对,求高手们指点!?

还是我理解的根本就不对,求高手们指点!?

函数式编程 2022-08-04 17:07:40
//default string constructor and five string copy constructors invokedvector<string> svec(5);书上说,编译器首先使用string默认构造函数创建一个临时值来初始化svec,然后使用复制构造函数将临时值复制到svec的每一个元素。1、首先这个临时值是一个string类型的临时值吧,用它来初始化svec,是不是创建了一个空的string类型的容器?2、然后说调用复制构造函数,是一个什么样的复制构造函数,从英文解释来看,应该是string类的复制构造函数,但是怎么能用上面那个临时制调用string类的复制构造函数呢?
查看完整描述

2 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

程序清单:

#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"};
/*calculate how many elements in words*/
size_t words_size = sizeof(words)/sizeof(char *);
/*将数组长度加到指向第一个元素的指针上就可以得到指向超出数组末端的下一个位置
的指针。通过指向第一个元素的指针words和指向数组中最后一个元素的下一个位置
的指针,实现了words1和words2的初始化。第二个指针提供停止复制的条件,其
所指向的位置上存放的元素并没有复制*/
/*initialize words1*/
vector<string> words1(words,words+words_size);
/*initialize words2*/
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;

/*find midpoint int the vector--words1*/
vector<string>::iterator mid = words1.begin() + words1.size()/2;

/*initialize vfront with first half of words1*/
vector<string> vfront(words1.begin(),mid);
/*initialize vback with second half of words1*/
vector<string> vback(mid,words1.end());

/*不能直接将一种容器中的元素复制给另一种容器,但系统允许通过一对迭代器间接
实现该功能*/
/*initialize dfront with first half of words1*/
deque<string> dfront(words1.begin(),mid);
/*initialize dback with second half of words1*/
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;

/*25 strings ,each is "love"*/
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


查看完整回答
反对 回复 2022-08-08
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

这是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,也就是说不调用赋制构造函数的,更说明书上说的是错误的
当然你的问题也就回答了,
自己运行一下这个程序,应该明白的


查看完整回答
反对 回复 2022-08-08
  • 2 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信