简体   繁体   English

C ++ std ::地图 <std::string, std::set<std::string> &gt;。 如何循环设置值?

[英]c++ std::map<std::string, std::set<std::string>> . How to loop set values?

I have declared a set inside a map in c++ as std::map<std::string, std::set<std::string>> . 我已经在c ++中的一个地图内部声明了一个set为std::map<std::string, std::set<std::string>> How to loop to access or print the set values ? 如何循环访问或打印设置值?

If you know how to iterate a std::map or a std::set individually, you should have no troubles iterating them in combination. 如果您知道如何分别迭代std::mapstd::set ,则可以毫无困难地组合起来进行迭代。 Note that if you iterate a std::map using a range-based for loop, you're iterating std::pair<K, V> s where K is the ( const -qualified) type of the key and V the type of the values. 请注意,如果使用基于范围的for循环迭代std::map ,则将迭代std::pair<K, V> s,其中K是键的( const限定)类型,而V是键的( const限定)类型。价值。 If you only want to iterate a single set (associated with aa single key), simply access that element and iterate it as normal. 如果您只想迭代单个集合(与单个键相关联),只需访问该元素并像平常一样对其进行迭代。

#include <iostream>
#include <map>
#include <set>
#include <string>

int
main()
{
  auto stuff = std::map<std::string, std::set<std::string>> {
    {"fruits",     {"apple", "banana", "orange"}},
    {"vegetables", {"carrot", "cucumber"}},
    {"nuts",       {"peanut", "walnut", "hazelnut", "coconut"}},
  };
  // Iterate over a single set
  for (const auto& s : stuff["fruits"])
    std::cout << s << "\n";
  std::cout << "\n";
  // Iterate over the whole data structure
  for (const auto& pair : stuff)
    {
      std::cout << pair.first << ":";
      for (const auto& s : pair.second)
        std::cout << " " << s;
      std::cout << "\n";
    }
}

Output: 输出:

apple
banana
orange

fruits: apple banana orange
nuts: coconut hazelnut peanut walnut
vegetables: carrot cucumber

Presumably that you have a std::map<std::string, std::set<std::string>> , you can print the elements of the contained set s with a double ranged for loop as the example below: 假设您有一个std::map<std::string, std::set<std::string>> ,则可以打印包含set元素的for循环范围为double的元素,如下例所示:

std::map<std::string, std::set<std::string>> mp {{"1", {"1", "2", "3"}}, 
                                                 {"2", {"4", "5", "6"}}};

for(auto &&p : mp) {
  for(auto &&s : p.second) std::cout << s << " ";
  std::cout << std::endl;
}

Live Demo 现场演示

Or if you want something more fancy to impress your boss you could use a single ranged for loop with std::copy and std::ostream_iterator : 或者,如果您想给老板留下更多的印象,可以使用带有std::copystd::ostream_iterator的单个范围内的for循环:

std::map<std::string, std::set<std::string>> mp {{"1", {"1", "2", "3"}}, 
                                                 {"2", {"4", "5", "6"}}};

for(auto &&p : mp) {
  std::copy(std::begin(p.second), std::end(p.second), 
            std::ostream_iterator<std::string>(std::cout," "));
  std::cout << std::endl;
}

Live Demo 现场演示

edit: 编辑:

The goal is we have a table with camera name and specs as {std::map> cameraSpec {{"camera1, {"Spec1", "Spec2", "Spec3"}}, {"camera2", {"Spec2", "Spec4", "Spec6"}}}; and I want to write a code to check which specification is highly desirable to the user ? Something similar to finding the frequency of a word from the given table. How to access the specs? 我们的目标是创建一个表,其中包含摄像机名称和规格,例如{std :: map> cameraSpec {{“ camera1,{” Spec1“,” Spec2“,” Spec3“}},{” camera2“,{” Spec2“, “ Spec4”,“ Spec6”}}};并且我想编写一个代码来检查哪个规范对用户来说是非常理想的?类似于从给定表中查找单词出现频率的方法。

How to access individual elements of the map 如何访问地图的各个元素

Say you want to access an existent element in the map (ie, you're sure the specific key exists). 假设您要访问地图中存在的元素(即,您确定特定键已存在)。 You could use the map 's subscript operator in the following way: 您可以通过以下方式使用map的下标运算符:

for(auto &&s : mp["camera2"]) {
  // you loop over elements {"spec2", "spec4", "spec6"}
}

That is, mp["camera2"] will return a reference to set in the map with values {"spec2", "spec4", "spec6"} under the key "camera2". 也就是说, mp["camera2"]将返回在映射中设置的引用,其键为“ camera2”下的值为{"spec2", "spec4", "spec6"}

If you're not sure that a key exists in the map, then use the member function map::find . 如果不确定映射中是否存在键,请使用成员函数map::find That is, first query the map if specific key you're looking for exists in it and then access the elements of the value/set in the same way as shown above: 也就是说,首先查询地图,如果您要查找的特定键存在于其中,然后以与上述相同的方式访问值/集合的元素:

auto it = mp.find("camera2");
if(it != mp.end()) {
  for(auto &&s : it->second) {
    // you loop over elements {"spec2", "spec4", "spec6"}
  }
}

Notice that if key "camera2" exists in your map, then map::find will return an iterator pointing to element associated with that particular key. 请注意,如果映射中存在键“ camera2”,则map::find将返回一个迭代器,该迭代器指向与该特定键关联的元素。

Now, the elements of the map are of type std::pair<const key_type, value_type> (in our case std::pair<const std::string, std::set<std::string>> ). 现在,映射的元素类型为std::pair<const key_type, value_type> (在我们的示例中为std::pair<const std::string, std::set<std::string>> )。

If the key is not found std::map::find is going to return an iterator pointing to one pass the end element of the map. 如果找不到该键,则std::map::find将返回一个迭代器,该迭代器指向地图的end元素。

std::pair has two members std::pair::first and std::pair::second . std::pair有两个成员std::pair::firststd::pair::second In our case std::pair::first value is equal to the key's value (ie,. "cammera2") and std::pair::second is equal to the value under that key (ie, the std::set with values {"spec2", "spec4", "spec6"} ). 在我们的例子中, std::pair::first值等于键的值(即“ cammera2”),而std::pair::second等于该键下的值(即std::set{"spec2", "spec4", "spec6"} )。 That is why in the loop shown above we iterate over it->second (ie, the value/set under key "cammera2"). 这就是为什么在上面显示的循环中,我们将其迭代- it->second (即,键“ cammera2”下的值/设置)。

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

相关问题 如何遍历集合的映射 (std::map <string,std::set< string> &gt;) 在 C++ 中? - How to iterate over a map of set (std::map<string,std::set< string> >) in C++? C ++循环std :: vector <std::map<std::string, std::string> &gt; - c++ loop std::vector<std::map<std::string, std::string> > C ++在std :: map &lt;&gt;中使用std :: set &lt;&gt; - C++ Using std::set<> in std::map<> 迭代std :: set时出现分段错误 <std::string> 在C ++中 - Segmentation fault while iterating a std::set<std::string> in C++ std :: string不适用于std :: set - std::string not working with std::set 如何在 C++ 中使用默认值将 std::set 转换为 std::map - How to convert std::set to std::map with a default value in c++ 如何初始化std :: set <std::string> 是否正确? - How to initializing std::set<std::string> correctly? c++:转换std::map<std::string, double> 到 std::map<std::string_view, double></std::string_view,></std::string,> - c++ : convert std::map<std::string, double> to std::map<std::string_view, double> 无法在std :: map中设置值 <std::string,std::shared_ptr<class> &gt; - Can't set values in an std::map<std::string,std::shared_ptr<class>> 使用参数 Map 调用 Java 函数<String, String>来自带有 std::map 的 C++<std::string, std::string> - Call Java function with parameter Map<String, String> from c++ with std::map<std::string, std::string>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM