我怎样才能在 C++ 中循环对
How can I loop over pair in c++
我正在尝试成对打印元素,但出现错误:"no matching function call"
代码:
#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;
}
你根本不是在制作矢量。您可能想这样做:
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;
}
我正在尝试成对打印元素,但出现错误:"no matching function call"
代码:
#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;
}
你根本不是在制作矢量。您可能想这样做:
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;
}