2 回答

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

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)該明白的
- 2 回答
- 0 關(guān)注
- 176 瀏覽
添加回答
舉報(bào)