[英]How do I iterate over a vector of string vectors? (c++)
I need to loop over vector of vector of strings in the same manner as I would do this on this example with integers : 我需要以与在此示例中使用整数相同的方式遍历字符串的vector :
int main()
{
vector<vector<int>> stuff;
//fill the inner vector, then insert it into the outer vector
for (int i = 0; i < 999; i++)
{
vector<int>temp;
for (int j = 0; j < 9; j++)
{
temp.push_back(i);
++i;
}
stuff.push_back(temp);
}
//display all elements ...
for (int i = 0; i < stuff.size(); i++)
{
for (int j = 0; j < stuff[i].size(); j++) {
cout << stuff[i][j] << "\t";
}
cout << endl;
}
}
But strings require different approach as they are more complex, Here I`m iterating over 1-Dimensional string: 但是字符串需要使用不同的方法,因为它们更加复杂,在这里我将对一维字符串进行迭代:
vector<string> first_arr = {};
string line;
ifstream myfile("source.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
first_arr.push_back(line); //READ from file
}
myfile.close();
}
else cout << "Unable to open file";
But I have completely stuck on going into the inner circle. 但是我完全坚持进入内部圈子。 Also, I am expecting strings of very different length 另外,我期望字符串的长度完全不同
I was not using c++ for a while so, please forgive my question if it seems too obvious to you, 我有一段时间没有使用c ++了,如果您觉得它太明显了,请原谅我的问题,
You can use the same logic. 您可以使用相同的逻辑。 Unless you want to print every string one letter at a time. 除非您想一次打印每个字符串一个字母。
#include <string>
#include <iostream>
#include <vector>
using std::vector;
int main()
{
vector<vector<std::string> > vec;
std::string tmp;
for(int i = 0; i < 2; ++i)
{
vector<std::string> v;
for(int j = 0; j < 9; ++j)
{
std::cin >> tmp;
v.push_back(tmp);
}
vec.push_back(v);
}
for(int i = 0; i < vec.size(); ++i)
{
for(int j = 0; j < vec[i].size(); ++j)
{
std::cout << vec[i][j] << "\t";
}
std::cout << std::endl;
}
return 0;
}
If you want to do the c++11 way, you could replace the last loop for this snippet: 如果要使用c ++ 11方式,则可以替换此代码段的最后一个循环:
for(const auto &subVec : vec)
{
for(const auto &str : subVec)
{
std::cout << str << "\t";
}
std::cout << std::endl;
}
Here's an example with three vector
s of string
s of sizes 5
, 10
, and 15
. 下面是具有三个示例vector
号第string
尺寸号第5
, 10
和15
。 This examples uses C++11
's range-based for loop to print the vector
s. 本示例使用C++11
的基于范围的for循环打印vector
。
Code: 码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
using SList = std::vector< std::string >;
using VList = std::vector< SList >;
VList vlist;
for ( int i = 5; i <= 15; i += 5 )
{
SList slist { i };
std::fill( slist.begin(), slist.end(), "Test" );
vlist.push_back( slist );
}
for ( const auto& v : vlist )
{
std::cout << "{ ";
for ( const auto& s : v )
{
std::cout << s << ' ';
}
std::cout << "}\n";
}
return 0;
}
Output: 输出:
{ Test Test Test Test Test }
{ Test Test Test Test Test Test Test Test Test Test }
{ Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test }
Here's the live example on Ideone: http://ideone.com/PyYD5p 这是Ideone上的实时示例: http ://ideone.com/PyYD5p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.