string s1 = "bob";
string s2 = "hey";
string s3 = "joe";
string s4 = "doe";

vector<string> myVec;

myVec.push_back(s1);
myVec.push_back(s2);
myVec.push_back(s3);
myVec.push_back(s4);


如何在myVec上使用迭代器输出“ bob hey”“ bob hey joe”“ bob hey joe doe”?

任何帮助提示或帮助将不胜感激

最佳答案

以下应该工作:

for (auto it = myVec.begin(), end = myVec.end(); it != end; ++it)
{
    for (auto it2 = myVec.begin(); it2 != (it + 1); ++it2)
    {
        std::cout << *it2 << " ";
    }
    std::cout << "\n";
}


输出示例:

bob
bob hey
bob hey joe
bob hey joe doe


Live Example

关于c++ - 使用迭代器连接 vector 中的元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22124894/

10-11 22:58