如何从C ++中的map迭代器调用类成员函数?

时间:2016-04-18 09:02:41

标签: c++ dictionary stl iterator

我无法使用map的迭代器调用show函数。有没有办法用iterator做到这一点?

#include <iostream>
#include <string>
#include <map>
using namespace std;
class A 
{ 
    int i;
    public: 
    A(int pi):i(pi)   {  cout<<"A()\n"; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()\n"; }
};
int main()
{
    map<char, A > mymap;
    A a(9) , b(8) , c(7);
    mymap['a'] =  a;
    mymap['b'] =  b;
    mymap['c'] =  c;
    map<char,A >::iterator it;
    for(it = mymap.begin(); it != mymap.end() ; it++)
        (*(it->second)).show();
    return 0;
}

在使用it->second.show()时,我收到以下错误:

  

在/usr/include/c++/4.9/bits/stl_map.h:63:0中包含的文件中,                    来自/usr/include/c++/4.9/map:61,                    从3开始:/usr/include/c++/4.9/tuple:实例化&#39; std :: pair&lt; _T1,_T2&gt; :: pair(std :: tuple&lt; _Args1 ...&amp;&amp ;,,   std :: tuple&lt; _Args2 ...&gt;&amp;,std :: _ Index_tuple&lt; _Indexes1 ...&gt;,   std :: _ Index_tuple&lt; _Indexes2 ...&gt;)[with _Args1 = {char&amp;&amp;};长   unsigned int ..._ Indexes1 = {0ul}; _Args2 = {}; long unsigned int   ..._ Indexes2 = {}; _T1 = const char; _T2 = A]&#39;:   /usr/include/c++/4.9/tuple:1093:63:来自&#39; std :: pair&lt; _T1,   _T2&gt; :: pair(std :: piecewise_construct_t,std :: tuple&lt; _Args1 ...&gt;,std :: tuple&lt; _Args2 ...&gt;)[with _Args1 = {char&amp;&amp;}; _Args2 = {}; _T1 =   const char; _T2 = A]&#39; /usr/include/c++/4.9/ext/new_allocator.h:120:4:   需要来自&#39; void __gnu_cxx :: new_allocator&lt; _Tp&gt; :: construct(_Up *,   _args&安培;&安培; ...)[与_Up = std :: pair; _Args = {const std :: piecewise_construct_t&amp;,std :: tuple,std :: tuple&lt;&gt;}; _Tp =   std :: _ Rb_tree_node&gt;]&#39;   /usr/include/c++/4.9/bits/alloc_traits.h:253:4:从   &#39; static std :: _ Require :: __ construct_helper&lt; _Tp,_Args&gt; :: type&gt;   std :: allocator_traits&lt; _Alloc&gt; :: _ S_construct(_Alloc&amp;,_Tp *,_ Args&amp;&amp;   ...)[与_Tp = std :: pair; _Args = {const   std :: piecewise_construct_t&amp;,std :: tuple,std :: tuple&lt;&gt;}; _Alloc   = std :: allocator&gt;取代; std :: _ Require :: __ construct_helper&lt; _Tp,_Args&gt; :: type&gt; =   空隙]&#39; /usr/include/c++/4.9/bits/alloc_traits.h:399:57:必需   来自&#39;静态decltype(_S_construct(__ a,__ p,   (正向&LT; _args&GT)(标准:: allocator_traits ::构造:: __参数)...))   std :: allocator_traits&lt; _Alloc&gt; :: construct(_Alloc&amp;,_Tp *,_ Args&amp;&amp; ...)   [与_Tp = std :: pair; _Args = {const   std :: piecewise_construct_t&amp;,std :: tuple,std :: tuple&lt;&gt;}; _Alloc   = std :: allocator&gt;取代; decltype(_S_construct(__ a,__ p,   (forward&lt; _Args&gt;)(std :: allocator_traits :: construct :: __ args)...))=   ]&#39; /usr/include/c++/4.9/bits/stl_tree.h:423:42:必填   来自&#39; std :: _ Rb_tree_node&lt; _Val&gt; * std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,   _Compare,_Alloc&gt; :: _ M_create_node(_Args&amp;&amp; ...)[with _Args = {const std :: piecewise_construct_t&amp;,std :: tuple,std :: tuple&lt;&gt;}; _Key =   烧焦; _Val = std :: pair; _KeyOfValue =   std :: _ Select1st&gt ;; _Compare =   性病::少; _Alloc = std :: allocator&gt ;;   std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_ Alloc&gt; :: _ Link_type =   std :: _ Rb_tree_node&gt; *]&#39;   /usr/include/c++/4.9/bits/stl_tree.h:1790:64:需要   &#39; std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_ Alloc&gt; :: iterator   std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,   _Alloc&gt; :: _ M_emplace_hint_unique(std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_Alloc&gt; :: const_iterator,_Args&amp;&amp; ...)[与_Args = {const std :: piecewise_construct_t&amp;,std :: tuple,的std ::元组&LT;&GT;}; _Key =   烧焦; _Val = std :: pair; _KeyOfValue =   std :: _ Select1st&gt ;; _Compare =   性病::少; _Alloc = std :: allocator&gt ;;   std :: _ Rb_tree&lt; _Key,_Val,_KeyOfValue,_Compare,_ Alloc&gt; :: iterator =   std :: _ Rb_tree_iterator&gt ;;的std :: _ Rb_tree&LT; _key,   _Val,_KeyOfValue,_Compare,_Alloc&gt; :: const_iterator = std :: _ Rb_tree_const_iterator&gt;]&#39;   /usr/include/c++/4.9/bits/stl_map.h:519:8:需要   &#39; std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: mapped_type&amp;的std ::地图&LT; _key,   _Tp,_Compare,_ Alloc&gt; :: operator [](std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: key_type&amp;&amp;)[with _Key = char; _Tp = A; _Compare = std :: less; _Alloc = std :: allocator&gt ;;   std :: map&lt; _Key,_Tp,_Compare,_Alloc&gt; :: mapped_type = A;的std ::地图&LT; _key,   _Tp,_Compare,_Alloc&gt; :: key_type = char]&#39; 17:14:从这里要求/usr/include/c++/4.9/tuple:1104:70:错误:没有匹配功能   致电A :: A()&#39;            第二(标准::向前&LT; _Args2&GT;(标准::得到&LT; _Indexes2&GT;(__ tuple2))...)                                                                         ^ /usr/include/c++/4.9/tuple:1104:70:注意:候选人是:9:5:注意:   A :: A(int)9:5:注意:候选人需要1个参数,0提供5:7:   注意:constexpr A :: A(const A&amp;)5:7:注意:候选人需要1   参数,0提供

4 个答案:

答案 0 :(得分:1)

1。it->second将直接返回A,而不是A*,您应该更改

(*(it->second)).show();

(it->second).show();

2. std::map::operator[]需要将A类型设为DefaultConstructible

  

如果密钥不存在,则插入value_type(key,T())。

但是A没有默认构造函数,你可以

mymap.insert({'a', a});

或@ Jarod42建议:

mymap.emplace('a', a);

以避免A被默认构建。

答案 1 :(得分:0)

首先,您需要A:

中的默认构造函数
A() {}
如果您提供了任何其他c-tor,

编译器将不会创建默认构造函数。

第二件事是如何调用你的函数:

it->second.show();

答案 2 :(得分:0)

这是怎么回事?朋友。

#include <iostream>
#include <string>
#include <map>
using namespace std;
class A 
{ 
    int i;
public: 
    A(int pi=0):i(pi)   {  cout<<"A()\n"; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()\n"; }
};

int main()
{
    map<char, A > mymap;
    A a(9) , b(8) , c(7);
    mymap['a'] =  a;
    mymap['b'] =  b;
    mymap['c'] =  c;
    map<char,A >::iterator it;
    for(it = mymap.begin(); it != mymap.end() ; it++)
        it->second.show();
    return 0;
}

答案 3 :(得分:0)

这将解决您的烦恼

    A(int pi=0):i(pi)   {  cout<<"A("<<pi<<")\n"; }
...
    for(it = mymap.begin(); it != mymap.end() ; it++)
        (it->second).show();

你应该覆盖operator =以使其正常工作