迭代map <std :: string,std :: string =“”>时出错

时间:2015-07-30 16:15:14

标签: c++ c++11 iterator stdmap

我已经尝试了几种方法来迭代我的&#34;条目&#34; map,但所有这些都会产生相同的冗长错误消息。

dylan@Aspire-one:~$ g++ -std=c++11 dictionary.cpp
In file included from /usr/include/c++/4.8/map:60:0,
                 from dictionary.h:6,
                 from dictionary.cpp:1:
/usr/include/c++/4.8/bits/stl_tree.h: In instantiation of ‘void 
std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, 
_Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = 
std::basic_string<char>; _Key = std::basic_string<char>; _Val = 
std::pair<const std::basic_string<char>, std::basic_string<char> >; 
_KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char>, 
std::basic_string<char> > >; _Compare = std::less<std::basic_string<char> 
>; _Alloc = std::allocator<std::pair<const std::basic_string<char>, 
std::basic_string<char> > >]’:
/usr/include/c++/4.8/bits/stl_map.h:226:11:   required from 
‘std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, 
_InputIterator) [with _InputIterator = std::basic_string<char>; _Key = 
std::basic_string<char>; _Tp = std::basic_string<char>; _Compare = 
std::less<std::basic_string<char> >; _Alloc = 
std::allocator<std::pair<const std::basic_string<char>, 
std::basic_string<char> > >]’
dictionary.h:11:66:   required from here
/usr/include/c++/4.8/bits/stl_tree.h:1721:28: error: no match for 
‘operator++’ (operand type is ‘std::basic_string<char>’)
for (; __first != __last; ++__first)
                        ^
/usr/include/c++/4.8/bits/stl_tree.h:1722:29: error: no match for 
‘operator*’ (operand type is ‘std::basic_string<char>’)
    _M_insert_unique_(end(), *__first);
                         ^
dylan@Aspire-one:~$

这是我最近的代码。

dictionary.cpp

#include "dictionary.h"
//I have included <string> <map> <iterator> "from dictionary.h"
bool dictionary::search_term(const std::string& term){
    std::map<std::string, std::string>::iterator it;
    for (it = entries.begin(); it != entries.end(); ++it){
        if(it->first != term);
        else return true;
    }return false;
}; 

所以错误发生在&#34; dictionary.h&#34;?

dictionary.h

#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <iterator>
#include <string>
#include <map>

class dictionary{
    public:
        dictionary(const std::string& title, const std::string& definition = "")
            : entries(std::map<std::string, std::string>(title, definition)){;};
        bool write_entry(const std::string& term, const std::string& definition = "");
        bool define_term(const std::string& term, const std::string& definition);
        bool erase_entry(const std::string& term);
        bool search_term(const std::string& term);
    private:
        std::map<std::string, std::string> entries;
};

#endif//DICTIONARY_H

2 个答案:

答案 0 :(得分:2)

在构造函数中,对地图使用大括号初始化:

    dictionary(const std::string& title, const std::string& definition = "")
        : entries{ {title, definition} } {;};

编辑:忘了一个级别的大括号​​)

或在构造函数体中设置元素

    dictionary(const std::string& title, const std::string& definition = "")
    {
      entries[title] = definition;
    }

答案 1 :(得分:1)

问题在于vagrant up的构造函数的定义。 class dictionary没有采用单个键值对的构造函数,因此使用一对初始化的最简单方法是使用通用初始化程序语法。您需要两对大括号,一个用于键值对的列表,另一个用于您要定义的单对

std::map