0

Does structure unbinding doesn't work for vector of vector in string? Like in Python or javascript, it is easier to unpack them. Is that not possible in C++ 17 yet? Here is my code, when I am trying to unpack the two values from the array, it throws me an error. Here is the entire code.

string tournamentWinner(vector<vector<string>> competitions,
                        vector<int> results)
{

   string best_Team = "";
   map<string, int> scores = {{best_Team, 0}};

    for(int idx = 0; idx < competitions.size(); idx ++)
    {
        auto competition = competitions[idx];
        
        auto [homeTeam, awayTeam] = competition;
        //auto homeTeam = competition[0];
        //auto awayTeam = competition[1];
        auto result = results[idx];

        string winningTeam = result == 1 ? homeTeam : awayTeam;

        //updateScores(winningTeam, 3, scores);

        if( scores[winningTeam] > scores[best_Team] )
        {
            best_Team = winningTeam;
        }

    }
    return best_Team;
}

int main()
{
    vector<vector<string>> competitions{{"HTML", "C#"}, {"C#", "Python"}, {"Python", "C#"}};
    vector<int> results{0, 0, 1};
    string winner = tournamentWinner(competitions, results);
    cout << winner.c_str();
    return 0;
}

Here is the compilation error -

 In function 'std::string tournamentWinner(std::vector<std::vector<std::__cxx11::basic_string<char> > >, std::vector<int>)':
tournament_winner_simple.cpp:28:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   28 |         auto [homeTeam, awayTeam] = competition;
      |              ^
tournament_winner_simple.cpp:28:14: error: 2 names provided for structured binding
   28 |         auto [homeTeam, awayTeam] = competition;

0 Answers0