第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

還是我理解的根本就不對(duì),求高手們指點(diǎn)!?

還是我理解的根本就不對(duì),求高手們指點(diǎn)!?

//default string constructor and five string copy constructors invokedvector<string> svec(5);書上說,編譯器首先使用string默認(rèn)構(gòu)造函數(shù)創(chuàng)建一個(gè)臨時(shí)值來初始化svec,然后使用復(fù)制構(gòu)造函數(shù)將臨時(shí)值復(fù)制到svec的每一個(gè)元素。1、首先這個(gè)臨時(shí)值是一個(gè)string類型的臨時(shí)值吧,用它來初始化svec,是不是創(chuàng)建了一個(gè)空的string類型的容器?2、然后說調(diào)用復(fù)制構(gòu)造函數(shù),是一個(gè)什么樣的復(fù)制構(gòu)造函數(shù),從英文解釋來看,應(yīng)該是string類的復(fù)制構(gòu)造函數(shù),但是怎么能用上面那個(gè)臨時(shí)制調(diào)用string類的復(fù)制構(gòu)造函數(shù)呢?
查看完整描述

2 回答

?
三國紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

程序清單:

#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 *);
/*將數(shù)組長度加到指向第一個(gè)元素的指針上就可以得到指向超出數(shù)組末端的下一個(gè)位置
的指針。通過指向第一個(gè)元素的指針words和指向數(shù)組中最后一個(gè)元素的下一個(gè)位置
的指針,實(shí)現(xiàn)了words1和words2的初始化。第二個(gè)指針提供停止復(fù)制的條件,其
所指向的位置上存放的元素并沒有復(fù)制*/
/*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());

/*不能直接將一種容器中的元素復(fù)制給另一種容器,但系統(tǒng)允許通過一對(duì)迭代器間接
實(shí)現(xiàn)該功能*/
/*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;
}
編譯并運(yùn)行程序后的執(zhí)行結(jié)果:
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


查看完整回答
反對(duì) 回復(fù) 2022-08-08
?
翻翻過去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

這是vector的一個(gè)構(gòu)造函數(shù),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次調(diào)用構(gòu)造函數(shù)(類型是T,value)來初始化vector對(duì)象的每一個(gè)元素,而不是僅僅調(diào)用一次T類型的構(gòu)造函數(shù),在這里的T相當(dāng)于你的string,value使用默認(rèn),為空string

實(shí)驗(yàn):
#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;
}
運(yùn)行結(jié)果為5個(gè)default,也就是說不調(diào)用賦制構(gòu)造函數(shù)的,更說明書上說的是錯(cuò)誤的
當(dāng)然你的問題也就回答了,
自己運(yùn)行一下這個(gè)程序,應(yīng)該明白的


查看完整回答
反對(duì) 回復(fù) 2022-08-08
  • 2 回答
  • 0 關(guān)注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)