简体   繁体   English

遍历地图时出错<std::string, std::string>

[英]Error when iterating through a map<std::string, std::string>

I have tried several ways of iterating over my "entries" map, but all of them produce the same lengthy error message. 我尝试了几种遍历“入口”映射的方法,但是所有这些方法都会产生相同的冗长错误消息。

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:~$

Here is my most recent code. 这是我最近的代码。

dictionary.cpp 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;
}; 

So the error is in "dictionary.h"? 那么错误在“ dictionary.h”中吗?

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

In the constructor, use brace initialization for the map: 在构造函数中,对地图使用花括号初始化:

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

( EDIT: forgot one level of braces) 编辑:忘记一级括号)

or set the element in the constructor body 或在构造函数主体中设置元素

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

The problem is in the definition of the constructor of class dictionary . 问题在于class dictionary的构造函数的定义。 std::map doesn't have a constructor that takes a single key-value pair, so the easiest way to initialize with one pair is to use the universal initializer syntax. std::map没有采用单个键值对的构造函数,因此,使用一对初始化的最简单方法是使用通用的初始化语法。 You'll need two pairs of braces, one for the list of key-value pairs, and one for the single pair you want to define: 您将需要两对花括号,一对用于键-值对列表 ,一对用于您要定义的单对

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM