简体   繁体   English

如何使用以列表为值的无序映射

[英]How to use an unordered map with lists as a values

I am trying to figure out how to maneuver around an unordered map with lists as a values. 我试图弄清楚如何围绕以列表为值的无序地图进行操作。 Is it possible to assign a list to a value by specified string key and then later add to that list some value or modify it or replace entire list? 是否可以通过指定的字符串键将一个列表分配给某个值,然后再在该列表中添加一些值或对其进行修改或替换整个列表? I also do not understand how you would print the specific list either. 我也不明白您将如何打印特定列表。

Example: 例:

std::unordered_map <string, list<int> > testmap;
list<int> templist;
templist.pushfront(10);
templist.pushfront(5);
testmap["First"] = templist;

How would I print the list templist from the unordered map and how can I add onto the list after its been assigned to "First" 如何从无序映射中打印列表临时列表,以及在将其分配给“第一”后如何添加到列表中

You can do with an unordered map with a list as a value as you need. 您可以根据需要使用列表作为值的无序地图。

For example for such definitions: 例如,此类定义:

std::unordered_map <string, list<int> > testmap;
list<int> templist;

you can: 您可以:

  • replace or create entire list value: 替换或创建整个列表值:
testmap["First"] = templist;
  • print some list item 打印一些列表项
std::cout << (*testmap["First"].begin()) << endl;
  • or print all items 或打印所有项目
for (auto v : testmap["First"])
  std::cout << v << "\n";
  • replace some list value 替换一些列表值
std::replace(testmap["First"].begin(), testmap["First"].end(), 20, 99)

How would I print the list templist from the unordered map and how can I add onto the list after its been assigned to "First" 如何从无序映射中打印列表临时列表,以及在将其分配给“第一”后如何添加到列表中

Here is a demonstrative program 这是一个示范节目

#include <iostream>
#include <string>
#include <list>
#include <unordered_map>

int main()
{
    std::unordered_map<std::string, std::list<int>> testmap;

    std::list<int> templist;

    templist.push_front( 10 );
    templist.push_front( 5 );

    testmap["First"] = templist;

    for ( const auto &item : testmap["First"] ) std::cout << item << ' ';
    std::cout << '\n';

    testmap["First"].push_front( 0 );

    for ( const auto &item : testmap["First"] ) std::cout << item << ' ';
    std::cout << '\n';
}

The program output is 程序输出为

5 10 
0 5 10 

Or you can even use for example an ordinary loop (or a standard algorithm like std::copy) to output the list. 或者甚至可以使用例如普通循环(或诸如std :: copy之类的标准算法)来输出列表。

#include <iostream>
#include <string>
#include <list>
#include <unordered_map>


int main()
{
    std::unordered_map<std::string, std::list<int>> testmap;

    std::list<int> templist;

    templist.push_front( 10 );
    templist.push_front( 5 );

    testmap["First"] = templist;

    for ( auto first = testmap["First"].begin(); first != testmap["First"].end(); ++first ) std::cout << *first << ' ';
    std::cout << '\n';

    testmap["First"].push_front( 0 );

    for ( auto first = testmap["First"].begin(); first != testmap["First"].end(); ++first ) std::cout << *first << ' ';
    std::cout << '\n';
}

The program output is 程序输出为

5 10 
0 5 10 

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

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