如何打印包含整数和整数集的 std::maps?
Posted
技术标签:
【中文标题】如何打印包含整数和整数集的 std::maps?【英文标题】:How do I print std::maps that contains integers and sets of integers? 【发布时间】:2019-02-02 10:32:30 【问题描述】:我有这张地图,其中包含整数作为引用整数集的键。基本上我有一个 std::map(int, std::set(int))。
我尝试为地图和集合定义一个迭代器,但是当我尝试将 set_iterator 指向地图中的特定集合时,我不断收到错误消息。我在尝试使两个迭代器相等的等式符号下得到一条红线(没有运算符“=”与这些操作数匹配)。我正在使用 Visual Studio C++ 2017,我不断收到构建错误。
/mykarger.cpp 的第 63-70 行
map<int, std::set<int>>::iterator graph_it;
set<string>::const_iterator set_it, set_end;
std::cout << "Vertix\tEdges\n;";
for(graph_it = mygraph.begin(); graph_it != mygraph.end(); ++graph_it)
std::cout << graph_it->first << ":\t";
for(set_it = graph_it->second.begin();; set_it != graph_it->second.end(); ++set_it)
cout << *set_it << "\t";
我希望结果是这样打印的:
Vertex Edges
1: 2 3
2: 1 3
3: 1 2
但我得到了错误:
1>------ Build started: Project: mykarger, Configuration: Debug Win32 ------
1>mykarger.cpp
1>c:\users\alkamali\source\repos\mykarger\mykarger\mykarger.cpp(68): error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> *' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=int
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree(303): note: could be 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>::operator =(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &&)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree(303): note: or 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>::operator =(const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1>c:\users\alkamali\source\repos\mykarger\mykarger\mykarger.cpp(68): note: while trying to match the argument list '(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>, std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> *)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> and
1> [
1> _Ty=int
1> ]
1>c:\users\alkamali\source\repos\mykarger\mykarger\mykarger.cpp(69): error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> *' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=int
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree(303): note: could be 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>::operator =(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &&)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree(303): note: or 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>::operator =(const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> &)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1>c:\users\alkamali\source\repos\mykarger\mykarger\mykarger.cpp(69): note: while trying to match the argument list '(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>, std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> *)'
1> with
1> [
1> _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> and
1> [
1> _Ty=int
1> ]
1>Done building project "mykarger.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
【问题讨论】:
set<string>
?我在您的 std::map
声明中看不到任何字符串。由于您使用的是C++17
,因此这是使用auto
的绝佳案例,因此您不会遇到这些问题。
嵌套for
语句中的额外分号。此外,不能使用 std::set<std::string>::const_iterator
来迭代 std::set<int>
就是这样!我不小心放了一个 set错误消息很大,可能需要一段时间才能提取相关信息,但归根结底是您试图将std::set<int>::iterator
分配给std::set<string>::iterator
。
你有
map<int, std::set<int>>::iterator graph_it;
^^^
但是
set<string>::const_iterator set_it, set_end;
^^^^^^
同一循环的更现代的表述避免了许多潜在的问题:
for(const auto& vertex: my_graph)
std::cout << vertex.first << ":\t";
for (const auto& edge: vertex.second)
std::cout << edge << " ";
【讨论】:
以上是关于如何打印包含整数和整数集的 std::maps?的主要内容,如果未能解决你的问题,请参考以下文章