编写程序将一个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;
}

你可能感兴趣的:(编写程序将一个list容器的所有元素赋值给一个vector容器。)