C++地图中的算法find_if
我想在地图中找到一对,然后打印。 这是我的代码:
std::map<std::string, std::string> mLoginPasswordMap{ "test", "test2" };
std::string key1("test");
std::string value1("test2");
auto it = std::find_if( mLoginPasswordMap.cbegin(),
mLoginPasswordMap.cend(),
[&key1, &value1]( const auto& elem )
{
return ( key1 == elem.first && value1 == elem.second );
});
if( it != mLoginPasswordMap.cend() )
{
std::cout << it->first << endl;
std::cout << it->second << endl;
}
if( it == mLoginPasswordMap.cend() )
{
std::cout << "No pair in map" << endl;
}
当程序编译时,我会收到这种错误:
usr/include/c++/9/bits/stl_map.h:273:4: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator) [with _InputIterator = const char*; _Key = std::__cxx11::basic_string<char>; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >]’
main.cpp:23:79: required from here
/usr/include/c++/9/ext/new_allocator.h:146:4: error: no matching function for call to ‘std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::pair(const char&)’
146 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我不知道我在做什么错。 有人可以帮我吗?
I want to find pair in map and just print it.
This is my code:
std::map<std::string, std::string> mLoginPasswordMap{ "test", "test2" };
std::string key1("test");
std::string value1("test2");
auto it = std::find_if( mLoginPasswordMap.cbegin(),
mLoginPasswordMap.cend(),
[&key1, &value1]( const auto& elem )
{
return ( key1 == elem.first && value1 == elem.second );
});
if( it != mLoginPasswordMap.cend() )
{
std::cout << it->first << endl;
std::cout << it->second << endl;
}
if( it == mLoginPasswordMap.cend() )
{
std::cout << "No pair in map" << endl;
}
and I receive this kind of error when program is compiling:
usr/include/c++/9/bits/stl_map.h:273:4: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::map(_InputIterator, _InputIterator) [with _InputIterator = const char*; _Key = std::__cxx11::basic_string<char>; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >]’
main.cpp:23:79: required from here
/usr/include/c++/9/ext/new_allocator.h:146:4: error: no matching function for call to ‘std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::pair(const char&)’
146 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I don't know what I'm doing wrong.
Anyone can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需以简单的方式填写地图,就可以:
然后您的代码将起作用。
也就是说,这不是使用
MAP
的方法。如果您绝对需要搜索(并不真正适用于地图),那么更好的方法是:这样,您实际上利用
MAP
,并且不要全部穿越。upper_bound
和lower_bound
是搜索范围的正常方法。现在,如果您有
地图
,并且想搜索给定的密钥:Just fill the map in the simple way, instead:
then your code will work.
That said, this is not the way to use a
map
. If you absolutely need a search (doesn't really apply to a map), a better way is:This way, you actually leverage the
map
and don't traverse it all. Andupper_bound
andlower_bound
are the normal way to search a range.Now, if you have a
map
and want to search for a given key: