1.当使用一个容器初始化另一个容易的拷贝时,两个容器的容器类型和元素类型必须相同。
vector<const char*> articles = {"a","b","c"};
vector<string> wordsss(articles);//错误
vector<string> articles = { "a", "b", "c" };
vector<string> wordsss(articles);//正确
vector<const char*> articles = { "a", "b", "c" };
vector<string> wordsss(articles.begin(), articles.end());//正确
2.输出string
for (string &data : wordsss)
{
ostringstream ostring;//注意include头文件<#include <sstream>>
ostring << data;
cout << ostring.str() << endl;
ostring.clear();
}
由于<<没有string类型对应的函数,故使用下面的函数实现
for (string &data : wordsss)
{
cout << data.c_str() << endl;
}