“例如,假设用一个名为text的字符串向量存放文本文件中的数据,其中的元素或者是一段话或者是一个用于表示段落分隔的空字符串。如果要输出text中第一段的内容,可以利用迭代器写一个循环令其遍历text,直到遇到空字符串的元素为止:”
//《C++Primer第五版》Page 98页部分解释
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector <string>text{ "Hello World!", "","Good job!"};
//不要上面一行代码改用下面这一行代码就是另外一种输出结果(其实就是中间那串字符串加多一个空格)
//vector <string>text{ "Hello World!", " ","Good job!" };
for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
cout << *it <<endl;
return 0;
}
上面的第八行的代码,我原本写为
string text{ "Hello World!"," ","Good job!" };
但是无法编译通过,相关原因可以参考
https://blog.youkuaiyun.com/JOKER0707/article/details/101569646