无法将迭代器设置为 std::pair 的 std::map

发布于 2025-01-04 19:36:25 字数 3164 浏览 0 评论 0 原文

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

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

src/DBConnector.cpp:在成员函数'int中 DBConnector::createGenericInsert(std::string, std::map, std::allocator; >、std::pair、std::allocator; > >, std::less, std::allocator; > >、std::allocator、std::allocator; >、std::pair、std::allocator; > > > > >, std::string&)': src/DBConnector.cpp:354: 错误: 与 'operator=' 不匹配 'l_oIterEnd = p_mapValues.std::map<_Key, _Tp, _Compare, _Alloc>::end 与 _Key = std::basic_string, std::allocator; >, _Tp = std::pair、std::allocator > >,_比较= std::less, std::allocator; > >, _Alloc = std::allocator、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; > >,_比较= std::less, std::allocator; > >, _Alloc = std::allocator、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 吗? (此外,映射可以包含持有不同类型的键吗?)

I have a map, whose values are std::pair.

The compiler complains as follows when trying to setup an iterator, but I can't understand why :

src/DBConnector.cpp: In member function ‘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::pair<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::string&)’:
src/DBConnector.cpp:354: error: no match for ‘operator=’ in
‘l_oIterEnd = p_mapValues.std::map<_Key, _Tp, _Compare, _Alloc>::end
with _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: note: candidates are:
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<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> > > > >]

My function is as is :

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();

    ...
}

What is the problem here ? Can a map contain std::pairs ? (Besides, can a map contain keys holding different types ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2025-01-11 19:36:26

您没有正确命名类型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值类型 code> 实际上是 pair,而 B 本身被称为“映射类型”。这很重要,因为许多操作都在 value 类型上进行,因此您需要访问它的第二部分才能获取映射的值。

You didn't name the type iterator correctly!

Please, please, save yourself trouble and use 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; // !!

    // ...
}

Please note that the value type of map<A, B> is actually pair<A, B>, while B itself is referred to as the "mapped type". This is important because many operations work on the value type, and thus you need to access the second part of it to get at the mapped value.

心如荒岛 2025-01-11 19:36:26

这些不是迭代器。

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;

these are not iterators.

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

above code should be changed to :

   std::map<std::string,std::pair<int,std::string> >::iterator l_oIter;
     std::map<std::string,std::pair<int,std::string> >::iterator l_oIterEnd;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文