2

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

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

src/DBConnector.cpp: 在成员函数'int DBConnector::createGenericInsert(std::string, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::basic_string<char, std::char_traits<char >、std::allocator<char> > >、std::allocator<std::pair<const std::basic_string<char、std::char_traits<char>、std::allocator<char> >、std::对<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::string&)': src/DBConnector.cpp:354: 错误: 否匹配 'l_oIterEnd = p_mapValues.std::map<_Key, _Tp, _Compare, _Alloc> 中的 'operator='::以 _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::pair<int, std::basic_string<char, std::char_traits<char 结尾>, std::allocator<char> > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator <std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<int, std::basic_string<char, std::char_traits< char>, std::allocator<char> > > > >' /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<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::pair<int, std::basic_string<char, std::char_traits<字符>,std::allocator<char>>,_Compare = std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,_Alloc = std::分配器<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<int, std::basic_string<char, std::char_traits <char>, std::allocator<char> > > > >]basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]

我的功能是:

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 吗?(此外,地图可以包含持有不同类型的键吗?)

4

2 回答 2

5

您没有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<A, B>实际上是,pair<A, B>B它本身被称为“映射类型”。这很重要,因为许多操作都在类型上工作,因此您需要访问second它的一部分以获取映射值。

于 2012-02-12T17:30:35.080 回答
4

这些不是迭代器。

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;
于 2012-02-12T17:29:26.303 回答