Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop over pair in c++ [closed]

I am trying to print the elements in pair, but it's throwing an error: "no matching function call"
Code:

#include <utility>
#include <iostream>
using namespace std;

int main()
{
    pair<int, string> pairVec;
    pairVec = make_pair(1, "One");
    pairVec = make_pair(2, "Two");
    pairVec = make_pair(3, "Three");
    for(auto iter:pairVec)
    {
        std::cout << "First: " << iter.first << ", Second: "
                  << iter.second << std::endl;
    }
    return 0;
}
like image 851
Bobtheblobber Avatar asked Dec 05 '25 23:12

Bobtheblobber


1 Answers

You're not making a vector at all. You probably wanted to do this instead:

int main()
{
    std::vector<pair<int, string>> pairVec;  // create a vector of pairs
    pairVec.emplace_back(1, "One"); // adding pairs to the vector
    pairVec.emplace_back(2, "Two");
    pairVec.emplace_back(3, "Three");
    for (auto iter : pairVec) {
        std::cout << "First: " << iter.first << ", Second: "
        << iter.second << std::endl;
    }
    return 0;
}
like image 59
Blaze Avatar answered Dec 08 '25 13:12

Blaze