如何打印包含整数和整数集的std :: maps?

时间:2019-02-02 10:32:31

标签: c++ visual-c++ c++17

我有这张地图,其中包含整数作为引用整数集的键。基本上我有一个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 ==========

1 个答案:

答案 0 :(得分:0)

该错误消息非常多,提取相关信息可能需要一些时间,但其归结为您正在尝试将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 << " ";
    }
}