如何遍历包含向量作为值的地图中的特定键?

时间:2016-02-05 05:05:32

标签: c++ dictionary vector

如何遍历地图[" a"]的内容以检索callcall1

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

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

我尝试使用for循环使用map迭代器,并在for循环向量上另一个for循环,然后检查map迭代器map的值是否等于&#34;&#34;但不断出错。

4 个答案:

答案 0 :(得分:1)

我认为你的意思是std::multimap而不是std::map,基于你的用例(同一个键下的多个值)。它位于相同的<map>标题中。

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

答案 1 :(得分:1)

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

首先,您有一个名为string的{​​{1}}个对象向量,这是对象而不是类型。对象是类型的变量,例如

point

此处name是类型/类string name = "curious"; 的对象,因此您无法键入string作为point的模板参数,您必须输入类型。那应该是map

第二件事是你正在使用逗号运算符,我不确定你是否知道你在做那个。逗号运算符的工作方式如下

string

^这将生成编译器错误,因为未使用“Hello”,但重点是逗号运算符计算表达式的第一部分,然后返回右侧的东西;所以这将打印

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

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

    return 0;
}

第三件事是你如何遍历地图。当您在C ++中迭代World 时,您实际上正在迭代一系列std::map,因此以下代码

std::pair

将产生以下输出

#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;
}

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

现在我想你想要一个从curious : 1 op : 2 个对象到string的地图,所以你要构建你的代码

vector

你会像这样迭代这个

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"]

希望有所帮助!

答案 2 :(得分:0)

如果我理解正确的话,这应该做你想要的。

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;
}

答案 3 :(得分:0)

我很好奇知道哪种更适合这种情况,即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);