【问题标题】:Error when iterating through a map<std::string, std::string>遍历 map<std::string, std::string> 时出错
【发布时间】:2015-10-22 02:51:53
【问题描述】:

我尝试了几种迭代“条目”映射的方法,但所有方法都会产生相同的冗长错误消息。

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

这是我最近的代码。

字典.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;
}; 

所以错误在“dictionary.h”中?

字典.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

【问题讨论】:

  • 您没有显示包含错误的代码。 (dictionary.h:11)
  • 是因为我在构造函数中初始化地图的方式吗?
  • 是的。您希望如何从两个字符串创建地图?
  • 条目[术语] = 定义
  • 但这不是你所做的。您将两个字符串传递给构造函数,而 map 没有一个接受单个键和值的构造函数...

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


【解决方案1】:

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

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

【讨论】:

  • 感谢您的解释。
【解决方案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;
    }

【讨论】:

  • 这行得通。我知道如何分配第二个示例中的元素,但我想在类初始化列表中进行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2022-10-22
  • 1970-01-01
相关资源
最近更新 更多