將一個list容器的所有元素賦值給一個vector容器。

#include<list>
#include<vector>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char* pchar[]= {"what", "is", "your", "name"};
	list<char*> clist(pchar , pchar+4); //注意這裏初始化存儲指向c風格字符串的char*指針的容器的方法。
	vector<string> svec;
	string str;
	cout << "please input strings for vector:" << endl;
	while(cin >> str)
		svec.push_back(str);
	cout << "The elements in the list are: " << endl;
	for (list<char*>::iterator pl = clist.begin(); pl != clist.end(); ++pl)
		cout << *pl << " ";
	cout << endl;
	cout << "The elements in the vector are: " << endl;
	for (vector<string>::iterator pv = svec.begin(); pv != svec.end(); ++pv)
		cout << *pv << " ";
	cout << endl;
	cout << "After changing ,the elements in the vector are :" << endl;
	svec.assign(clist.begin(), clist.end());
	for (vector<string>::iterator pv = svec.begin(); pv != svec.end(); ++pv)
		cout << *pv << " ";
	return 0;
}

發佈了29 篇原創文章 · 獲贊 10 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章