C++ 如何使用一对作为键在映射上迭代?
我有一个C++ 如何使用一对作为键在映射上迭代?,c++,dictionary,iterator,output,stdmap,C++,Dictionary,Iterator,Output,Stdmap,我有一个映射,其中对作为键,第三个整数作为值。我如何迭代地图的键以打印它们?我的示例代码粘贴在下面: #include <iostream> #include <map> #include <algorithm> using namespace std; int main () { map <pair<int, int>, int> my_map; pair <int, int> my_pair; my_p
映射
,其中对作为键,第三个整数作为值。我如何迭代地图的键以打印它们?我的示例代码粘贴在下面:
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main ()
{
map <pair<int, int>, int> my_map;
pair <int, int> my_pair;
my_pair = make_pair(1, 2);
my_map[my_pair] = 3;
// My attempt on iteration
for(map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it) {
cout << it->first << "\n";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
绘制我的地图;
配对我的双;
我的配对=制造配对(1,2);
我的地图[我的配对]=3;
//我对迭代的尝试
for(map::iterator it=my_-map.begin();it!=my_-map.end();+it){
不能先#包括
#包括
#包括
使用名称空间std;
int main()
{
绘制我的地图;
配对我的双;
我的配对=制造配对(1,2);
我的地图[我的配对]=3;
//我对迭代的尝试
for(map::iterator it=my_-map.begin();it!=my_-map.end();+it){
cout first.first你可以访问c++11吗,auto关键字可以让它看起来更好。我编译并运行得很好
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main ()
{
map <pair<int, int>, int> my_map;
pair <int, int> my_pair;
my_pair = make_pair(1, 2);
my_map[my_pair] = 3;
// My attempt on iteration
for(auto it = my_map.begin(); it != my_map.end(); ++it) {
cout << it->first.first << "\n";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
绘制我的地图;
配对我的双;
我的配对=制造配对(1,2);
我的地图[我的配对]=3;
//我对迭代的尝试
for(自动it=my_map.begin();it!=my_map.end();++it){
cout first.first嗯,映射键是第一项,但它本身是一对
所以
pair&key(*it);
coutit->first
是类型为const std::pair
的对象,即它是键。it->second
是类型为int
的对象,即它是映射值。如果只想输出键和映射值,可以编写
for ( map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it )
{
cout << "( " << it->first.first << ", "
<< it->first.second
<< " ): "
<< it->second
<< std::endl;
}
for(map::iterator it=my_-map.begin();it!=my_-map.end();++it)
{
cout默认情况下,std::ostream
不知道如何处理std::pair
。因此,如果要打印密钥,最简单的方法是直接访问它们的两个元素,如其他答案所示。但是,如果您可以访问功能,那么您也可以使用a在地图上进行迭代,如下所示:
int main() {
std::map<std::pair<int, int>, int> my_map{ {{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9} };
for (auto const &kv : my_map)
std::cout << "(" << kv.first.first << ", " << kv.first.second << ") = " << kv.second << std::endl;
return 0;
}
你想做什么?你的问题是什么?(顺便说一句:在打印it->的时候,没有超负荷工作。ostream&,pair
)首先是而是:@VladfromMoscow:我想迭代键并打印它们。另外,一个更好的范围是:for(auto&&x:my_map)是否基于范围的for
可以让它看起来更好:)
for ( map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it )
{
cout << "( " << it->first.first << ", "
<< it->first.second
<< " ): "
<< it->second
<< std::endl;
}
for ( const auto &p : my_map )
{
cout << "( " << p.first.first << ", "
<< p.first.second
<< " ): "
<< p.second
<< std::endl;
}
int main() {
std::map<std::pair<int, int>, int> my_map{ {{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9} };
for (auto const &kv : my_map)
std::cout << "(" << kv.first.first << ", " << kv.first.second << ") = " << kv.second << std::endl;
return 0;
}
for (auto const &[k, v] : my_map)
std::cout << "(" << k.first << ", " << k.second << ") = " << v << std::endl;