【问题标题】:How to iterate through a specific key in a map containing vector as value?如何遍历包含向量作为值的映射中的特定键?
【发布时间】:2016-02-05 05:05:31
【问题描述】:

如何遍历map["a"]的内容来检索callcall1

std::vector<std::string> point
std::map<std::string, point> alloc

map["a"] = call, call1
map["i"] = call

我尝试使用 for 循环使用 map 迭代器并在该 for 循环内对向量进行另一个 for 循环,然后检查映射迭代器映射的值是否等于“a”但不断收到错误.

【问题讨论】:

  • 我担心你必须比这更具体。
  • 感谢谁更改了代码。我已经输入了,但使用的是移动版本,所以它必须已更改为代码
  • 我想我在下面解释了答案。看看吧!

标签: c++ dictionary vector


【解决方案1】:

我认为您对某些语法和编程语言以及标准库容器的语义有一点误解。我会解释我认为你做错了什么。

首先,您有一个名为pointstring 对象向量,这是一个对象,而不是类型。一个对象就是一个类型的变量,例如

string name = "curious";

这里的name是一个类型/类string的对象,所以你不能输入point作为map的模板参数,你必须输入一个类型。所以应该是string

第二件事是您正在使用逗号运算符,我不确定您是否知道您正在这样做。逗号操作符的作用如下

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;

int main() {
    cout << ("Hello", "World") << endl;

    return 0;
}

^ 这将产生一个编译器错误,因为没有使用“Hello”,但关键是逗号运算符计算表达式的第一部分,然后返回右边的内容;所以这将打印出来

World

第三件事是你如何遍历地图。当您在 C++ 中遍历 std::map 时,您实际上是在遍历一系列 std::pairs 所以下面的代码

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <map>
using std::map;

int main() {
    map<string, int> map_string_int {{"curious", 1}, {"op", 2}};

    for (auto iter = map_string_int.begin(); iter != map_string_int.end();
            ++iter) {
        cout << iter->first << " : " << iter->second << endl;
    }

    return 0;
}

将产生以下输出

curious : 1
op : 2

键将按字母顺序排列,因为它们存储在二叉搜索树中 (https://en.wikipedia.org/wiki/Binary_search_tree)

现在我想你想要一个从 string 对象到 vectors 的映射,所以你可以这样构建你的代码

std::vector<string> point
std::map<string, std::vector<string>> alloc;

alloc["a"] = {"call", "call1"};
alloc["i"] = {"call"};

你会像这样遍历它

for (auto iter = alloc.begin(); iter != alloc.end(); ++iter) {
    cout << iter->first << " : " << iter->second << endl;
}

你会像这样遍历alloc["a"]

// sanity check
assert(alloc.find("a") != alloc.end());
for (auto iter = alloc["a"].begin(); iter != alloc["a"].end(); ++iter) {
        cout << *iter << endl;
}

希望有所帮助!

【讨论】:

  • 我实际上只想要 alloc["a"] 的内容。具体怎么弄的?
  • 现在有意义吗?
【解决方案2】:

我假设您的意思是 std::multimap 而不是 std::map,基于您的用例(同一个键下的多个值)。它在同一个 &lt;map&gt; 标头中。

std::multimap<std::string, int> map;
map.insert(std::make_pair("first", 123));
map.insert(std::make_pair("first", 456));

auto result = map.equal_range("first");
for (auto it = result.first; it != result.second; ++it)
    std::cout << " " << it->second;

参考:std::multimap::equal_range

【讨论】:

    【解决方案3】:

    如果我理解正确,这应该可以满足您的需求。

    std::vector<string> point = { "Hello", "World" };
    std::map<std::string, decltype(point)> my_map;
    
    //if you dont wan't to use decltype (or cant):
    //std::map<std::string, std::vector<std::string>> my_map;
    
    my_map["A"] = point;
    my_map["B"] = { "Something", "Else" };
    
    //this will iterate only trought my_map["A"]
    for (const auto &vector_val : my_map["A"])
        std::cout << vector_val << std::endl;
    
    //this will iterate trought the whole map
    for (const auto &map_pair : my_map)
    {       
        std::cout << "map: " << map_pair.first << std::endl;
    
        for (const auto &vector_val : map_pair.second)
            std::cout << vector_val << std::endl;
    
        std::cout << "---------------------------------" << std::endl;
    }
    

    【讨论】:

      【解决方案4】:

      我很想知道在这种情况下什么更合适,即 multimap 或 map_of_vectors 。 如果连续有人想要迭代与地图中的特定/所有键关联的向量 什么会更有效/最佳。

      map<string ,vector<string>> mp;
      // initialize your map...
      for(auto itr=mp.begin(); itr!=mp.end() ;itr++)
         for(auto itr2=itr->second.begin(); itr2!=itr->second.end() ;itr2++)
            cout<<*itr2
      

      对于特定的键,只需按照以下说明更改第一个循环

      auto itr=mp.find(key);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-01
        • 2020-04-16
        相关资源
        最近更新 更多