The*_* do 412 c++ dictionary stl
我正在尝试检查给定的密钥是否在地图中,有些不能这样做:
typedef map<string,string>::iterator mi;
map<string, string> m;
m.insert(make_pair("f","++--"));
pair<mi,mi> p = m.equal_range("f");//I'm not sure if equal_range does what I want
cout << p.first;//I'm getting error here
Run Code Online (Sandbox Code Playgroud)
那么如何打印p中的内容呢?
小智 648
使用 map::find
if ( m.find("f") == m.end() ) {
// not found
} else {
// found
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*dRR 287
要检查映射中是否存在特定键,请使用count以下方法之一使用成员函数:
m.count(key) > 0
m.count(key) == 1
m.count(key) != 0
Run Code Online (Sandbox Code Playgroud)
该文档的map::find说:"其他部件的功能,map::count可以用来只检查一个特定的键是否存在."
所述文档为map::count表示:"因为在地图容器中的所有元素是唯一的,该函数只能返回1(如果该元件被发现),或零(否则)".
要通过您知道存在的键从地图中检索值,请使用map :: at:
value = m.at(key)
Run Code Online (Sandbox Code Playgroud)
与map :: operator []不同,map::at如果指定的键不存在,则不会在映射中创建新键.
Tho*_*ini 36
你可以使用.find():
map<string,string>::iterator i = m.find("f");
if (i == m.end()) { /* Not found */ }
else { /* Found, i->first is f, i->second is ++-- */ }
Run Code Online (Sandbox Code Playgroud)
Den*_*kov 21
C ++ 20使我们std::map::contains能够做到这一点。
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> example = {{1, "One"}, {2, "Two"},
{3, "Three"}, {42, "Don\'t Panic!!!"}};
if(example.contains(42)) {
std::cout << "Found\n";
} else {
std::cout << "Not found\n";
}
}
Run Code Online (Sandbox Code Playgroud)
aJ.*_*aJ. 14
m.find == m.end() // not found
Run Code Online (Sandbox Code Playgroud)
如果您想使用其他API,请找到 m.count(c)>0
if (m.count("f")>0)
cout << " is an element of m.\n";
else
cout << " is not an element of m.\n";
Run Code Online (Sandbox Code Playgroud)
Ste*_*sop 12
我想你想要的map::find.如果m.find("f")等于m.end(),则找不到密钥.否则,find返回一个指向找到的元素的迭代器.
该错误是因为p.first是一个迭代器,它不适用于流插入.将您的最后一行更改为cout << (p.first)->first;.p是一对迭代器,p.first是一个迭代器,p.first->first是关键字符串.
地图只能有一个给定键的元素,因此equal_range不是很有用.它是为map定义的,因为它是为所有关联容器定义的,但对于multimap来说它更有趣.
小智 6
map<string, string> m;
Run Code Online (Sandbox Code Playgroud)
检查 key 是否存在,并返回出现次数(map 中为 0/1):
int num = m.count("f");
if (num>0) {
//found
} else {
// not found
}
Run Code Online (Sandbox Code Playgroud)
检查 key 是否存在,并返回迭代器:
map<string,string>::iterator mi = m.find("f");
if(mi != m.end()) {
//found
//do something to mi.
} else {
// not found
}
Run Code Online (Sandbox Code Playgroud)
在你的问题中,错误是由坏operator<<过载引起的,因为p.first是map<string, string>,你无法打印出来。尝试这个:
if(p.first != p.second) {
cout << p.first->first << " " << p.first->second << endl;
}
Run Code Online (Sandbox Code Playgroud)
template <typename T, typename Key>
bool key_exists(const T& container, const Key& key)
{
return (container.find(key) != std::end(container));
}
Run Code Online (Sandbox Code Playgroud)
当然,如果你想变得更漂亮,你总是可以模板化一个函数,它也接受一个找到的函数和一个未找到的函数,就像这样:
template <typename T, typename Key, typename FoundFunction, typename NotFoundFunction>
void find_and_execute(const T& container, const Key& key, FoundFunction found_function, NotFoundFunction not_found_function)
{
auto& it = container.find(key);
if (it != std::end(container))
{
found_function(key, it->second);
}
else
{
not_found_function(key);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
std::map<int, int> some_map;
find_and_execute(some_map, 1,
[](int key, int value){ std::cout << "key " << key << " found, value: " << value << std::endl; },
[](int key){ std::cout << "key " << key << " not found" << std::endl; });
Run Code Online (Sandbox Code Playgroud)
这样做的缺点是想出了一个好名字,“find_and_execute”很尴尬,我想不出更好的主意……
C++17 simplified this a bit more with an If statement with initializer.
This way you can have your cake and eat it too.
if ( auto it{ m.find( "key" ) }; it != std::end( m ) )
{
// Destructure the returned pair in to
// its sub components. Get them by reference.
// You can also get them by value.
auto&[ key, value ] { *it };
// Grab either the key or value stored in the pair.
// The key is stored in the 'first' variable and
// the 'value' is stored in the second.
auto& mkey{ it->first };
auto& mvalue{ it->second };
// That or just grab the entire pair pointed
// to by the iterator.
auto& pair{ *it };
}
else
{
// Key was not found..
}
Run Code Online (Sandbox Code Playgroud)