如何将起始地址分配给 map<pair<long long int,long long int> 的迭代器,long long int >

How to assign starting address to an interator of a map<pair<long long int,long long int>,long long int >

我创建了一个 pair 和 long long int 的映射 -

map< pair< long long int, long long int >,long long int >; 

和一个交互器 -

map< pair< long long int, long long int >, long long int >::iterator it1;

当我这样做时 it1=same.begin() 编译器抛出错误,为什么会这样?

产生错误:

no viable overloaded '=' 
it1=same12.begin();
~~~^~~~~~~~~~~~~~~

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:648:29: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from '__map_iterator<__tree_iterator<__value_type, [...]>, __node_pointer, [...]>>' to 'const __map_iterator<__tree_iterator<__value_type, [...]>, std::__1::__tree_node, long long>, void *> *, [...]>>' for 1st argument class _LIBCPP_TYPE_VIS_ONLY __map_iterator

错误告诉你:

no known conversion from '__map_iterator<...>' to 'const __map_iterator<_...>'

注意 const.

在这里,看起来 same12 是一个 const map(或一个 const map&,或者它可能在 const 方法中使用,在这种情况下常量正确性正在申请)。这意味着 .begin() will return const_iterator,而不是 iterator

您需要将 it1 的类型更改为 const_iterator 而不是 iterator。完整类型应为:map<pair<long long int, long long int>, long long int>::const_iterator.

或者直接使用auto it1 = same12.begin();.