排序值C++映射具有多个键值的数据结构
Sorting values C++ map data structure with many values for a key
这是我的输入文件的一行的格式:
人口|城市|州|高速公路列表===
6|Oklahoma City|Oklahoma|I-35;I-44;I-40
6|Boston|Massachusetts|I-90;I-93
8|Columbus|Ohio|I-70;I-71
我需要创建一个具有以下格式的输出文件:
Population
(newline)
City, State
Interstates: Comma-separated list of interstates, sorted by interstate number ascending
(newline)
===>示例:
6
Boston, Massachusetts
Interstates: I-90, I-93
Oklahoma City, Oklahoma
Interstates: I-35, I-40, I-44
8
Columbus, Ohio
Interstates: I-70, I-71
在这里,人口相同的州应该被分组在一起,必须先按州的字母顺序排序,然后按城市排序。我能够得到正确的格式,但我无法确定使用哪种数据结构来对州和城市进行排序。
我现在有map<int, vector<string> >
。关键是种群,其余的是矢量。欢迎提出任何建议。
我根本不会为此使用映射。您可能应该弄清楚数据的每个元素实际需要什么信息,并创建任何需要支持的数据类型。例如
struct State
{
unsigned int Population;
std::vector<std::string> Cities;
std::vector<unsigned int> Highways;
};
然后,您可以解析数据,并创建一个std::vector<State>
。使用std::Sort对向量和数据进行适当的排序(您可以使用lambdas,或者在必要时创建比较函数或函子)。
相关文章:
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 遍历我的数据结构,并向其中输入随机值
- C++类似于 std::map 的数据结构,具有多个键级别
- 如何将 JSON 键值(我使用 cpp boost 解析)存储在相关数据结构中?
- 键:: MAP或类似数据结构中的键压缩
- constexpr 值的数据结构
- 首选数据结构,如果您有两个键并且无法使用Boost
- 从保存变量数据类型的数据结构中检索值,而不指定返回类型
- 如何使用 boost 连接映射的键和值,特别是如果值是结构
- 用于在具有更新查询的二维矩阵中查找最大值的最佳数据结构
- c o(1)[x in s],其中x是一个值,s是数据结构
- 是否有一个更适合键值数据结构的QABSTRACTITEMMODEL类型类/接口
- 哪个集合/数据结构可以处理键、值、值
- 我应该使用哪些键值数据结构?按值快速检索和按键快速检索
- 排序值C++映射具有多个键值的数据结构
- 用于存储唯一<键,值>元组的数据结构,其中<value>最小值
- 用于 Hadoop 的可移植键值数据文件格式
- 用于存储元组键的数据结构:参数关系列表
- 如何使用rapidjson C++在JSON文件中检索键值数据
- 存储文件中的键值数据