类似地图的容器? [英] Map-like container?

查看:81
本文介绍了类似地图的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在寻找类似于地图容器的东西,但是有一个

略微修改,一个唯一的密钥,有多个分配给每个

一个。我认为STL中的一些容器有它,但我看不到它。


所以我可以分配一个像地图一样的键:


//伪c ++代码

container< int,int> m;


// 1:< 2,-3,5>

m [1] = 2;

m [ 1] = -3;

m [1] = 5;


// 2:< 7,1,0,-1,8>

m [2] = 7;

m [2] = 1;

m [2] = 0;

m [ 2] = -1;

m [2] = 8;


并获得每个密钥的迭代器:


for(it = m [1] .begin(); it!= m [1] .end(); it ++)

cout<< 这是 << (* it)<< endl;

结果将类似于


map< key_T,list< data_T> >


没有麻烦:)


感谢任何人!!


-

Sensei< se ****** @ mac.com>


乐观主义者认为这是所有可能世界中最好的。

悲观主义者担心这是真的。 [J. Robert Oppenheimer]

Hi!

I''m looking for something similar to the map container, but with a
slight modification, a unique key, with multiple assignments to each
one. I think some container in the STL has it, but I cannot see it.

So I can assign to a key like a map:

// pseudo c++ code
container<int, int> m;

// 1: <2, -3, 5>
m[1] = 2;
m[1] = -3;
m[1] = 5;

// 2: <7, 1, 0, -1, 8>
m[2] = 7;
m[2] = 1;
m[2] = 0;
m[2] = -1;
m[2] = 8;

And get an iterator for each key:

for (it = m[1].begin(); it != m[1].end(); it++)
cout << "this is " << (*it) << endl;
The result would be something like a

map< key_T, list<data_T> >

without hassles :)

Thanks to anyone!!

--
Sensei <se******@mac.com>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

推荐答案

Sensei< se ****** @ mac.com>写道:
Sensei <se******@mac.com> wrote:
嗨!

我正在寻找类似于地图容器的东西,但稍作修改,一个唯一的键,有多个任务给每个人一个。我认为STL中的某个容器有它,但是我看不到它。

所以我可以分配一个像地图一样的键:

//伪c ++代码
container< int,int> m;

// 1:< 2,-3,5>
m [1] = 2;
m [1] = -3;
m [1] = 5;

// 2:< 7,1,0,-1,8>
m [2] = 7;
m [2 ] = 1;
m [2] = 0;
m [2] = -1;
m [2] = 8;

并得到一个迭代器对于每个键:

for(it = m [1] .begin(); it!= m [1] .end(); it ++)
cout<< 这是 << (* it)<< endl;

结果将类似于

地图< key_T,list< data_T> >
Hi!

I''m looking for something similar to the map container, but with a
slight modification, a unique key, with multiple assignments to each
one. I think some container in the STL has it, but I cannot see it.

So I can assign to a key like a map:

// pseudo c++ code
container<int, int> m;

// 1: <2, -3, 5>
m[1] = 2;
m[1] = -3;
m[1] = 5;

// 2: <7, 1, 0, -1, 8>
m[2] = 7;
m[2] = 1;
m[2] = 0;
m[2] = -1;
m[2] = 8;

And get an iterator for each key:

for (it = m[1].begin(); it != m[1].end(); it++)
cout << "this is " << (*it) << endl;
The result would be something like a

map< key_T, list<data_T> >




为什么不使用它?


问候

-

jb


(rot13中的回复地址,先解读)



So why not use just that?

regards
--
jb

(reply address in rot13, unscramble first)


Sensei写道:
嗨!

我正在寻找类似于地图容器的东西,但是稍作修改,一个独特的键,每个都有多个任务
一。我认为STL中的某个容器有它,但是我看不到它。

所以我可以分配一个像地图一样的键:

//伪c ++代码
container< int,int> m;

// 1:< 2,-3,5>
m [1] = 2;
m [1] = -3;
m [1] = 5;

// 2:< 7,1,0,-1,8>
m [2] = 7;
m [2 ] = 1;
m [2] = 0;
m [2] = -1;
m [2] = 8;

并得到一个迭代器对于每个键:

for(it = m [1] .begin(); it!= m [1] .end(); it ++)
cout<< 这是 << (* it)<< endl;

结果将类似于

地图< key_T,list< data_T> >

没有麻烦:)
Hi!

I''m looking for something similar to the map container, but with a
slight modification, a unique key, with multiple assignments to each
one. I think some container in the STL has it, but I cannot see it.

So I can assign to a key like a map:

// pseudo c++ code
container<int, int> m;

// 1: <2, -3, 5>
m[1] = 2;
m[1] = -3;
m[1] = 5;

// 2: <7, 1, 0, -1, 8>
m[2] = 7;
m[2] = 1;
m[2] = 0;
m[2] = -1;
m[2] = 8;

And get an iterator for each key:

for (it = m[1].begin(); it != m[1].end(); it++)
cout << "this is " << (*it) << endl;
The result would be something like a

map< key_T, list<data_T> >

without hassles :)




对我来说听起来像是一个多图。 ;-)


HTH,

- g

-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com

你不能亲吻*除非你错过**

[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]



Sounds like a multimap to me. ;-)

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


Sensei写道:
我正在寻找类似于地图容器的东西,但是稍作修改,一个独特的钥匙,每个都有多个任务。


每个人的多项任务是什么?意思?你是否想尝试
创建某种哈希表?使用来自SGI的''哈希表''。或者使用

std :: map< int,std :: vector< int> > ;.

我认为STL中的某个容器有它,但是我看不到它。

所以我可以分配一个像地图一样的键:

//伪c ++代码
容器< int,int> m;

// 1:< 2,-3,5>
m [1] = 2;
m [1] = -3;
m [1] = 5;

// 2:< 7,1,0,-1,8>
m [2] = 7;
m [2 ] = 1;
m [2] = 0;
m [2] = -1;
m [2] = 8;

并得到一个迭代器对于每个键:

for(it = m [1] .begin(); it!= m [1] .end(); it ++)
cout<< 这是 << (* it)<< endl;

结果将类似于

地图< key_T,list< data_T> >

没有麻烦:)
I''m looking for something similar to the map container, but with a
slight modification, a unique key, with multiple assignments to each
one.
What does "multiple assignments to each one" mean? Are you trying to
create some kind of hash table? Use ''hashtable'' from SGI. Or use
std::map<int, std::vector<int> >.
I think some container in the STL has it, but I cannot see it.

So I can assign to a key like a map:

// pseudo c++ code
container<int, int> m;

// 1: <2, -3, 5>
m[1] = 2;
m[1] = -3;
m[1] = 5;

// 2: <7, 1, 0, -1, 8>
m[2] = 7;
m[2] = 1;
m[2] = 0;
m[2] = -1;
m[2] = 8;

And get an iterator for each key:

for (it = m[1].begin(); it != m[1].end(); it++)
cout << "this is " << (*it) << endl;
The result would be something like a

map< key_T, list<data_T> >

without hassles :)




< rant>

什么是麻烦 ?你想避免调用push_back吗?为什么你不想要b $ b你试图避免编程,只要想想你想要的更多

它可能会发生......

< / rant>


嗯,你可以发明自己的课程,当分配到它自己的内部集合时会增加




模板< class T> struct auto_list

{

std :: list< T>收集;

public:

auto_list& operator =(T t){collection.push_back(t); }

// ......你还需要什么

};


然后在你的''map'中使用它':


std :: map< key_T,auto_list< data_T> >


请记住,免费奶酪只能在捕鼠器中找到。


V

-

请在邮寄回复时从我的地址中删除资金



<rant>
What''s a "hassle"? Do you want to avoid calling "push_back"? Why don''t
you try to avoid programming at all, just think harder of what you want
and it might just happen...
</rant>

Well, you could you invent your own class that when assigned to would grow
its own internal collection:

template<class T> struct auto_list
{
std::list<T> collection;
public:
auto_list& operator=(T t) { collection.push_back(t); }
// ... whatever else you need
};

and then use it in your ''map'':

std::map<key_T, auto_list<data_T> >

Remember that free cheese is only found in the mousetrap.

V
--
Please remove capital As from my address when replying by mail


这篇关于类似地图的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆