Maxim
Maxim

Reputation: 7

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:

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,

Upvotes: 0

Views: 3334

Answers (2)

Azeem
Azeem

Reputation: 14607

Here's an example with three vectors of strings of sizes 5, 10, and 15. This examples uses C++11's range-based for loop to print the vectors.

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

Upvotes: 2

Francisco Geiman
Francisco Geiman

Reputation: 400

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:

for(const auto &subVec : vec)
{
    for(const auto &str : subVec)
    {
        std::cout << str << "\t";
    }
    std::cout << std::endl;
}

Upvotes: 1

Related Questions