我们可以在C++中将向量<string>放入列表中吗?

Can we put vector<string> to the list in C++ ?

本文关键字:gt 列表 string C++ 向量 lt 我们      更新时间:2023-10-16

像这样:

列表<向量<字符串>>?

如果答案是肯定的,我如何使用它来按For Cycle打印列表中的每个元素?

谢谢大家。我知道如何打印列表<int>,FOR CYCLE可以遍历它。但事实上,如果我使用FOR CYCLE和迭代器来列出<向量<string>>,我会失败的。

如果您的编译器是最新的标准:

std::list<std::vector<std::string>> lst;
for ( auto& vec : lst ) // Iterate through all std::vector's
   for ( auto& str : vec ) // Iterate through all std::string's
       std::cout << str << std::endl; // str is your std::string

当然这是可能的。你可以使用迭代器来打印列表中的每个元素,试试这样的方法:

list<vector<string> >::iterator it;
for(it=your_list.begin();it!=your_list.end();it++)
{
    //your code to print waht you want
    //it is a pointer to a vector<string>
    //...
}