vector iteration

If i have 5 vectors For example vector<string>player_0;
vector<string>player_1;
vector<string>player_2;
vector<string>player_3;
vector<string>player_4;
how can i use a for loop to go from player_0 to player_1. And i have to use vectors for my players?
1
2
3
4
5
6
7
8
vector<vector<string>> players;
players.resize(5);

for (int i=0; i<5; ++i)
{
  players[i];
}
i have to keep my strings as
vector<string>player_1;
vector<string>player_2;
vector<string>player_3;
vector<string>player_4; it is.

cz i have other functions and codes to use this way. And how can i go from player_0 to player_1 if i keep it this way ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <vector>
#include <functional>

int main()
{
    std::vector<std::string> player_0 = { "zero", "one", "two" } ;
    std::vector<std::string> player_1 = { "three", "four", "five", "six", "seven", "eight" } ;
    std::vector<std::string> player_2 = { "nine", "ten", "eleven", "twelve" } ;
    // ...

    // create a vector of wrapped references to all the strings
    // http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
    std::vector< std::reference_wrapper<std::string> > all_players( player_0.begin(), player_0.end() ) ;
    all_players.insert( all_players.end(), player_1.begin(), player_1.end() ) ;
    all_players.insert( all_players.end(), player_2.begin(), player_2.end() ) ;
    // ...

    int n = 0 ;
    for( auto& wrapped_str : all_players ) // iterate over the wrapped references
    {
        std::string& str = wrapped_str.get() ;
        std::cout << str << ' ' ;
        str = std::to_string(n++) + ". " + str ; // prefix each string with serial number
    }
    std::cout << '\n' ;

    for( const auto& str : player_1 ) std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/19e3af022c337956
Relying on the name of a variable at runtime (when the names of variables no longer exist, so actually you have to create extra variables just to give them values that help you think about names of other variables), by and large, a bad idea and I suspect that you don't need to do it; I suspect that you're simply having trouble thinking about programming.

Rethink your code and don't rely on variable names at runtime.
i have to keep my strings as
vector<string>player_1;
vector<string>player_2;
vector<string>player_3;
vector<string>player_4; it is.

cz i have other functions and codes to use this way.

In the long run, you'll find that it's easier to change the code as Moschops suggested (or if there are always exactly 5 players: vector<string> players[5];). Changing your existing code and functions is essentially just a cut & paste job. It's busy work but not difficult. Knuckle down and do it.
> i have other functions and codes to use this way. And how can i go from player_0 to player_1 if i keep it this way

In software, we are primarily limited by our own lack of ingenuity.
Here's is another way by which you can have your cake and eat it too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector< std::vector<std::string> >  all_players =
    {
        { "zero", "one", "two" },
        { "three", "four", "five", "six", "seven", "eight" },
        { "nine", "ten", "eleven", "twelve" }
        // ...
    };

    std::vector<std::string>& player_0 = all_players[0] ;
    std::vector<std::string>& player_1 = all_players[1] ;
    std::vector<std::string>& player_2 = all_players[0] ;
    // ...


    int n = 0 ;
    for( auto& vec : all_players ) // iterate over all the strings
    {
        for( auto& str : vec )
        {
            std::cout << str << ' ' ;
            str = std::to_string(n++) + ". " + str ; // prefix each string with serial number
        }
        std::cout << '\n' ;
    }

    player_0[1] += '!' ; 
    player_1.push_back( "13. thirteen" ) ;
    player_2.pop_back() ;
    // ...

    for( const auto& str : player_1 ) std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/22a3f99b9a52b050
Topic archived. No new replies allowed.