简体   繁体   English

如何使用map :: find在地图上

[英]how to use map::find on map of maps

I'm having an issue making some unit tests in a large code base. 我在大型代码库中进行一些单元测试时遇到问题。 Basically I need to find a value in map of maps. 基本上,我需要在地图map中找到一个值。 I wrote a code snippet to explain what my problem is. 我写了一段代码来解释我的问题是什么。 The following represents how the data is build: 下面显示了如何构建数据:

map<string, map<string, string>> testmap;
map<string, string> item;
item.insert(pair<string,string>("hello","world"));
testmap.insert(pair<string, map<string,string> > ("key",item));

then, at some later point, I need to check that the values have been added before obtaining a key. 然后,在以后的某个时间点,我需要在获取密钥之前检查是否已添加值。 From the cppreference, the return of the [] operator is 从cppreference中,[]运算符的返回为

Reference to the mapped value of the new element if no element with key key existed. 如果不存在带有key key的元素,则引用新元素的映射值。 Otherwise a reference to the mapped value of the existing element whose key is equivalent to key. 否则,引用其键与键等效的现有元素的映射值。

If I understand correctly, which from the comments is clear that I am not, this means that a new element is inserted if there was no element with that key. 如果我正确理解(从注释中可以清楚地表明我不是),则意味着如果没有带有该键的元素,则会插入一个新元素。 I want to avoid that, and return an error message. 我想避免这种情况,并返回错误消息。

The following is wrong and I'm trying to understand why: 以下是错误的 ,我正试图了解原因:

if (testmap.find(map<string,string>("hello","world")) != testmap.end()) 
{
    ...
    return testmap["hello"]["world"];
}

To find a value in a map, you cannot use find member function, which finds a key. 若要在映射中查找值,则不能使用find成员函数来查找键。 However, you can find a value, eg, by using std::find_if with a custom comparator: 但是,您可以找到一个值,例如,通过将std::find_if与自定义比较器一起使用:

using value_t = std::map<std::string, std::string>;
using key_t = std::string;
using map_t = std::map<key_t, value_t>;

map_t m { { "key" , { { "hello", "world" } } } };

value_t v { { "hello", "world" } };  // value to be found
auto iter = std::find_if(m.begin(), m.end(),
               [&v](const auto& e){ return e.second == v; });
std::cout << (iter != m.end()) << std::endl;

Live demo: https://wandbox.org/permlink/1b0bjbnnPY8E0uiU 现场演示: https : //wandbox.org/permlink/1b0bjbnnPY8E0uiU


Note that this will work since C++14. 请注意,这将从C ++ 14开始生效。 In C++11, you need to write: 在C ++ 11中,您需要编写:

auto iter = std::find_if(m.begin(), m.end(),
               [&v](const map_t::value_type& e){ return e.second == v; });

UPDATE UPDATE

It's still kind-of unclear to me what you are trying to achieve. 我仍然不清楚您要达到的目标。 If you need a reference to the inner-most value only if it's both-level keys exist, then you can do (C++17 syntax): 如果仅在两个级别的键都存在时才需要引用最内层的值,则可以这样做(C ++ 17语法):

if (auto i1 = m.find("key"); i1 != m.end())
   if (auto i2 = i1->second.find("hello"); i2 != i1->second.end())
      std::cout << i2->second << std::endl;  // same as m["key"]["hello"] here but faster

在我看来,您想要的是testmap.find("key") ,即搜索键,但是由于某种原因,您认为应该搜索值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM