关于字典:如何从C ++中的map迭代器调用类成员函数?

How to call class member function from iterator of map in C++?

我无法使用map的迭代器调用show函数。使用迭代器有什么方法可以做到这一点吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <map>
using namespace std;
class A
{
    int i;
    public:
    A(int pi):i(pi)   {  cout<<"A()
"
; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()
"
; }
};
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()时,我得到以下错误:

In file included from /usr/include/c++/4.9/bits/stl_map.h:63:0,
from /usr/include/c++/4.9/map:61,
from 3: /usr/include/c++/4.9/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&,
std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>,
std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long
unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int
..._Indexes2 = {}; _T1 = const char; _T2 = A]':
/usr/include/c++/4.9/tuple:1093:63: required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 =
const char; _T2 = A]' /usr/include/c++/4.9/ext/new_allocator.h:120:4:
required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*,
_Args&& ...) [with _Up = std::pair; _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Tp =
std::_Rb_tree_node >]'
/usr/include/c++/4.9/bits/alloc_traits.h:253:4: required from
'static std::_Require::__construct_helper<_Tp, _Args>::type>
std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&&
...) [with _Tp = std::pair; _Args = {const
std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Alloc
= std::allocator > >; std::_Require::__construct_helper<_Tp, _Args>::type> =
void]' /usr/include/c++/4.9/bits/alloc_traits.h:399:57: required
from 'static decltype (_S_construct(__a, __p,
(forward<_Args>)(std::allocator_traits::construct::__args)...))
std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...)
[with _Tp = std::pair; _Args = {const
std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Alloc
= std::allocator > >; decltype (_S_construct(__a, __p,
(forward<_Args>)(std::allocator_traits::construct::__args)...)) =
]' /usr/include/c++/4.9/bits/stl_tree.h:423:42: required
from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Key =
char; _Val = std::pair; _KeyOfValue =
std::_Select1st >; _Compare =
std::less; _Alloc = std::allocator >;
std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type =
std::_Rb_tree_node >*]'
/usr/include/c++/4.9/bits/stl_tree.h:1790:64: required from
'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Key =
char; _Val = std::pair; _KeyOfValue =
std::_Select1st >; _Compare =
std::less; _Alloc = std::allocator >;
std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator =
std::_Rb_tree_iterator >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >]'
/usr/include/c++/4.9/bits/stl_map.h:519:8: required from
'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = A; _Compare = std::less; _Alloc = std::allocator >;
std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = A; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]' 17:14: required from here /usr/include/c++/4.9/tuple:1104:70: error: no matching function for
call to 'A::A()'
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^ /usr/include/c++/4.9/tuple:1104:70: note: candidates are: 9:5: note:
A::A(int) 9:5: note: candidate expects 1 argument, 0 provided 5:7:
note: constexpr A::A(const A&) 5:7: note: candidate expects 1
argument, 0 provided


1、it->second直接归还A而不是A*你应该换

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

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

2.std::map::operator[]需要A型默认可构造。

Inserts value_type(key, T()) if the key does not exist.

但是EDOCX1[1]没有默认的构造函数,您可以

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

或者如@jarod42所建议的那样:

1
mymap.emplace('a', a);

避免默认构造A


这可以解决你的问题

1
2
3
4
5
    A(int pi=0):i(pi)   {  cout<<"A("<<pi<<")
"
; }
...
    for(it = mymap.begin(); it != mymap.end() ; it++)
        (it->second).show();

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


这个怎么样?朋友。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <map>
using namespace std;
class A
{
    int i;
public:
    A(int pi=0):i(pi)   {  cout<<"A()
"
; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()
"
; }
};

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;
}

首先,您需要在中使用默认的构造函数:

1
A() {}

如果您提供了任何其他c-tor,编译器将不会创建默认的构造函数。

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

1
it->second.show();