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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With