【问题标题】:Unable to setup an iterator to a std::map of std::pair无法为 std::pair 的 std::map 设置迭代器
【发布时间】:2012-03-04 06:52:22
【问题描述】:

我有一张地图,其值为 std::pair。

编译器在尝试设置迭代器时抱怨如下,但我不明白为什么:

src/DBConnector.cpp: 在成员函数‘int DBConnector::createGenericInsert(std::string, std::map<:basic_string std::char_traits>, std::allocator >, std::pair, std::allocator > >, std::less<:basic_string std::char_traits>, std::allocator > >, std::allocator<:pair std::basic_string std::char_traits>, std::allocator >、std::pair、std::allocator > > > > >、std::string&)’: src/DBConnector.cpp:354: 错误: 中的‘operator=’不匹配 'l_oIterEnd = p_mapValues.std::map<_key _tp _compare _alloc>::end with _Key = std::basic_string, std::allocator >, _Tp = std::pair, std::allocator >>, _Compare = std::less<:basic_string std::char_traits>, std::allocator >>, _Alloc = std::allocator<:pair std::basic_string std::char_traits>, std::allocator >, std::pair, std::allocator > > > >’ /usr/include/c++/4.4/bits/stl_map.h:251:注意:候选人是: std::map<_key _tp _compare _alloc>& std::map<_key _tp _compare _alloc>::operator=(const std::map<_key _tp _compare _alloc>&) [with _Key = std::basic_string, std::allocator > , _Tp = std::pair, std::allocator >>, _Compare = std::less<:basic_string std::char_traits>, std::allocator >>, _Alloc = std::allocator<:pair std::basic_string std::char_traits>, std::allocator >, std::pair, std::allocator > > > >]

我的功能是这样的:

int DBConnector::createGenericInsert ( std::string p_sTable , std::map<std::string , std::pair<int,std::string> > p_mapValues , std::string & po_sInsert ) {

    std::map<std::string,std::pair<int,std::string> > l_oIter;
    std::map<std::string,std::pair<int,std::string> > l_oIterEnd;

    std::string s_Fieldnames;

    l_oIterEnd = p_mapValues.end(); // This is line 354
    l_oIter = p_mapValues.begin();

    s_Fieldnames += l_oIter.first();

    ...
}

这里有什么问题?地图可以包含 std::pairs 吗? (此外,地图可以包含持有不同类型的键吗?)

【问题讨论】:

  • 您将l_oIter 声明为std::map,而不是迭代器...

标签: c++ map iterator std-pair


【解决方案1】:

这些不是迭代器。

std::map<std::string,std::pair<int,std::string> > l_oIter;
 std::map<std::string,std::pair<int,std::string> > l_oIterEnd;

上面的代码应该改成:

   std::map<std::string,std::pair<int,std::string> >::iterator l_oIter;
     std::map<std::string,std::pair<int,std::string> >::iterator l_oIterEnd;

【讨论】:

    【解决方案2】:

    你没有正确命名类型iterator

    拜托,拜托,省去你的麻烦,使用typedef

    typedef std::map<std::string, std::pair<int, std::string>> map_type;
    
    int DBConnector::createGenericInsert (std::string p_sTable, map_type p_mapValues,
                                          std::string & po_sInsert)
    {
        std::string s_Fieldnames;
    
        map_type::iterator l_oIterEnd = p_mapValues.end();
        map_type::iterator l_oIter = p_mapValues.begin();
    
        s_Fieldnames += l_oIter->first; // !!
    
        // ...
    }
    

    请注意map&lt;A, B&gt;值类型实际上是pair&lt;A, B&gt;,而B本身被称为“映射类型”。这很重要,因为许多操作都适用于 value 类型,因此您需要访问其中的 second 部分才能获得映射的值。

    【讨论】:

    • OP:您还需要取消引用迭代器,例如l_oIter-&gt;first()
    • 或者,如果您有可用的 C++11 功能,请使用 autoauto l_oIterEnd = p_mapValues.end();
    • 我觉得太笨了,没有注意到 ::iterator 丢失了。接受! +1 建议 typedef ;)
    猜你喜欢
    • 2012-01-06
    • 2012-11-24
    • 2011-06-22
    • 2012-04-27
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多