C++添加顺序的字典/地图

发布于 2024-08-27 09:19:23 字数 254 浏览 5 评论 0 原文

我想要有类似于地图的东西,但在迭代时我希望它们的顺序与添加的顺序相同。

示例

map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

在迭代时,我希望项目类似于“一”、“二”、“三”。默认情况下,地图不提供此添加顺序。如何按照我添加的方式获取地图元素?我想要保留插入顺序

的任何内容都可以,或者其他替代建议也可以。

I want to have something similar to map but while iterating I want them to be in the same order as it is added.

Example

map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

While iterating I want the items to be like "one", ""two", "three"..By default, map doesn't provide this added order. How to get the map elements the way I have added? I want to retain the insertion order.

Anything with STL is fine or other alternative suggestions also fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-09-03 09:19:23

一个 std::map 跟踪顺序插入?
这是重复的(感谢尼尔·巴特沃斯)

您可以使用具有与之平行的排序结构的映射。

map<key, value>
vector<value*> //holds only pointers to map entries.
vector<key> //holds only map keys. Adds one indirection.

A std::map that keep track of the order of insertion?
this is a duplicate (thanks to neil butterworth)

You could use a map with a sorted structure parallel to it.

map<key, value>
vector<value*> //holds only pointers to map entries.
vector<key> //holds only map keys. Adds one indirection.
思念满溢 2024-09-03 09:19:23

Boost mult iindex 使得容器能够以多种不同的顺序进行迭代。

http://www.boost.org/doc/ libs/1_42_0/libs/multi_index/doc/index.html

稍微修改的示例:

struct record {
    int         insertion_index;
    std::string somestring;
    int         somevalue;
    bool operator < (const record& e) const {return insertion_index < e.insertion_index;}
};

typedef multi_index_container<
    record,
    indexed_by<
        // sort by record::operator<
        ordered_unique<insertion_indexentity<record> >,        
        // sort by less<string> on somestring
        ordered_non_unique<member<record,std::string,&record::somestring> >    
    >
> record_set;

Boost mult iindex makes it possible to have a container which can be iterated over in several different orders.

http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/index.html

Slightly modified example:

struct record {
    int         insertion_index;
    std::string somestring;
    int         somevalue;
    bool operator < (const record& e) const {return insertion_index < e.insertion_index;}
};

typedef multi_index_container<
    record,
    indexed_by<
        // sort by record::operator<
        ordered_unique<insertion_indexentity<record> >,        
        // sort by less<string> on somestring
        ordered_non_unique<member<record,std::string,&record::somestring> >    
    >
> record_set;
奶气 2024-09-03 09:19:23

实际上,std::map 默认情况下使用 std::less 对键进行排序,其中 T 是键的类型。如果您不希望按键对元素排序,则应该使用 std::list 。 > 或 std::vector > -- 其中 K 是您的键类型,V 是您的值类型,然后使用 push_back 添加元素到列表/向量的末尾。

Actually std::map by default sorts your keys using std::less<T> where T is your key's type. If you don't want the elements sorted by key, you should be using an std::list<std::pair<K,V> > or an std::vector<std::pair<K,V> > -- where K is your key type, and V is your value type, and then using push_back to add elements to the end of the list/vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文