Map-like container? 
March 14th, 2006, 02:45 PM
| | | Map-like container?
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 <senseiwa@mac.com>
The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer] | 
March 14th, 2006, 02:45 PM
| | | Re: Map-like container?
Sensei <senseiwa@mac.com> wrote:[color=blue]
> 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> >[/color]
So why not use just that?
regards
--
jb
(reply address in rot13, unscramble first) | 
March 14th, 2006, 02:45 PM
| | | Re: Map-like container?
Sensei wrote:[color=blue]
> 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 :)
>[/color]
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.] | 
March 14th, 2006, 02:55 PM
| | | Re: Map-like container?
Sensei wrote:[color=blue]
> I'm looking for something similar to the map container, but with a
> slight modification, a unique key, with multiple assignments to each
> one.[/color]
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> >.
[color=blue]
> 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 :)[/color]
<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 | 
March 14th, 2006, 03:05 PM
| | | Re: Map-like container?
Sensei skrev:[color=blue]
> 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!!
>[/color]
class SetWrapper {
private:
typedef std::set<int> seti;
seti d_set;
public:
void operator=(int i) {
d_set.insert(i);
}
seti::iterator begin() {
return d_set.begin();
}
seti::iterator end() {
return d_set.end();
}
};
int main() {
std::map<int,SetWrapper> m;
m[1] = 2;
m[1] = -3;
m[1] = 5;
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 << "this is " << (*it) << endl;
}
return 0;
}
--
TB @ SWEDEN | 
March 14th, 2006, 03:45 PM
| | | Re: Map-like container?
On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.Abazarov@comAcast.net> said:
[color=blue][color=green]
>> I'm looking for something similar to the map container, but with a
>> slight modification, a unique key, with multiple assignments to each
>> one.[/color]
>
> 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> >.[/color]
I come from C and ASM, where nothing is defined, no lists, nothing more
than memory allocation. I thought there were containers helping the
programmer. This construct seems quite useful, that's why I'm asking.
[color=blue]
> <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>[/color]
A simple ``no'' would suffice, I don't really like classes and
siblings, I feel better with memset and alike... but unfortunately for
me, C++ is required, and I must adapt myself. Hassle (to me of course)
is caring about copy constructors and whatever is more than
malloc()/memmove()... that is hassle :)
[color=blue]
> 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> >[/color]
The class seems the same as a map of int and list<something> to me :)
except of course, the = operator.
[color=blue]
> Remember that free cheese is only found in the mousetrap.[/color]
This is very true.
--
Sensei <senseiwa@mac.com>
The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer] | 
March 14th, 2006, 03:55 PM
| | | Re: Map-like container?
Sensei wrote:
[color=blue]
> On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.Abazarov@comAcast.net>
> said:
>[color=green][color=darkred]
>>> I'm looking for something similar to the map container, but with a
>>> slight modification, a unique key, with multiple assignments to each
>>> one.[/color]
>>
>> 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> >.[/color]
>
> I come from C and ASM, where nothing is defined, no lists, nothing more
> than memory allocation. I thought there were containers helping the
> programmer.[/color]
There are.
[color=blue]
> This construct seems quite useful, that's why I'm asking.
>[color=green]
>> <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>[/color]
>
> A simple ``no'' would suffice, I don't really like classes and
> siblings, I feel better with memset and alike... but unfortunately for
> me, C++ is required, and I must adapt myself. Hassle (to me of course)
> is caring about copy constructors and whatever is more than
> malloc()/memmove()... that is hassle :)[/color]
Hmm, the idea is actually the other way round. The constructors and that
stuff do things automatically for you that you usually would have to care
about yourself. You just push_back the object into your container and
you're done. No malloc, no memmove needed.
A copy constructor is mostly needed if you're handling some raw resources
that themselves aren't represented by a class with a proper copy
constructor.
[color=blue][color=green]
>> 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> >[/color]
>
> The class seems the same as a map of int and list<something> to me :)
> except of course, the = operator.[/color]
Well, it's still unclear what you want. What's wrong with the map of int and
list<int> (or int and vector<int>)? | 
March 14th, 2006, 03:55 PM
| | | Re: Map-like container?
[color=blue]
> Sounds like a multimap to me. ;-)[/color]
a multimap has several identical entries for the key values. That's
not what he wanted. He just wanted a map of array data:
std::map<int, std::vector<int> >
e.g. | 
March 14th, 2006, 04:05 PM
| | | Re: Map-like container?
Gernot Frisch wrote:
[color=blue]
>[color=green]
>> Sounds like a multimap to me. ;-)[/color]
>
> a multimap has several identical entries for the key values. That's
> not what he wanted. He just wanted a map of array data:
> std::map<int, std::vector<int> >
> e.g.[/color]
Hmm, what would be the difference? You store several values of the same type
under one key. | 
March 14th, 2006, 04:05 PM
| | | Re: Map-like container?
[color=blue]
> ... I feel better with memset and alike... but unfortunately for
> me, C++ is required, and I must adapt myself.[/color]
Oh boy! Get a C++ book and start thinking. You don't know what you're
missing. It's not C++ because classes and ineritance makes it more
complex, it's because it's more powerful and easier to maintain. I
know a good FORTRAN programmer can write FORTRAN code in any
language - but do you really want to?
C++ is _not_ C(++)
it is rather:
class Cpp : public C
{
public:
virtual Cpp& operator++() {return C::data++;}
};
-Gernot | 
March 14th, 2006, 04:05 PM
| | | Re: Map-like container?
[color=blue][color=green]
>>[color=darkred]
>>> Sounds like a multimap to me. ;-)[/color]
>>
>> a multimap has several identical entries for the key values. That's
>> not what he wanted. He just wanted a map of array data:
>> std::map<int, std::vector<int> >
>> e.g.[/color]
>
> Hmm, what would be the difference? You store several values of the
> same type
> under one key.[/color]
....now that I think of it... forget all I said. | 
March 14th, 2006, 04:25 PM
| | | Re: Map-like container?
On 2006-03-14 16:35:45 +0100, Artie Gold <artiegold@austin.rr.com> said:
[color=blue]
> Sounds like a multimap to me. ;-)[/color]
Can you explain this?
Following the SGI documentation, I understand it is possible to use
multimap with pairs, so the only way to achieve what I want is to be
tricky:
multimap<int, int> adj;
multimap<int, int>::iterator it_adj;
/*
1:
2, 4, 1
2:
6, 9, 4, 2, 1, 0
*/
adj.insert(pair<int,int>(1,2));
adj.insert(pair<int,int>(1,4));
// ``2'' inserted before ending the key ``1''
adj.insert(pair<int,int>(2,6));
adj.insert(pair<int,int>(2,9));
adj.insert(pair<int,int>(2,4));
adj.insert(pair<int,int>(2,1));
adj.insert(pair<int,int>(2,0));
// insert last ``1''
adj.insert(pair<int,int>(1,1));
adj.insert(pair<int,int>(2,2));
it_adj = adj.find(1);
while ((*it_adj).first == 1)
{
cout << "that's 2 key and value " << (*it_adj).second << endl;
it_adj++;
}
So I feel like there's no other way than this and a by-hand use of
map/lists... am I right? :)
--
Sensei <senseiwa@mac.com>
The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer] | 
March 14th, 2006, 05:15 PM
| | | Re: Map-like container?
On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net> said:
[color=blue]
>[color=green]
>> ... I feel better with memset and alike... but unfortunately for
>> me, C++ is required, and I must adapt myself.[/color]
>
> Oh boy! Get a C++ book and start thinking. You don't know what you're missing.[/color]
I bought C++, third edition, from Bjarne Stroustrup himself... not that
I bought it from him I mean :)
[color=blue]
> It's not C++ because classes and ineritance makes it more complex, it's
> because it's more powerful and easier to maintain. I know a good
> FORTRAN programmer can write FORTRAN code in any language - but do you
> really want to?[/color]
Possibly not. So, I think I'm writing C code in C++...
[color=blue]
> C++ is _not_ C(++)
>
> it is rather:
>
> class Cpp : public C
> {
> public:
> virtual Cpp& operator++() {return C::data++;}
> };[/color]
Better read that book pronto, since that & at the end of Cpp for the ++
operator dazzles me :)
--
Sensei <senseiwa@mac.com>
The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer] | 
March 14th, 2006, 05:15 PM
| | | Re: Map-like container?
Sensei wrote:[color=blue]
> On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net> said:
>[color=green]
>> C++ is _not_ C(++)
>>
>> it is rather:
>>
>> class Cpp : public C
>> {
>> public:
>> virtual Cpp& operator++() {return C::data++;}
>> };[/color]
>
> Better read that book pronto, since that & at the end of Cpp for the ++
> operator dazzles me :)[/color]
It's a reference. A bit like a pointer, but different.
Ben Pope
--
I'm not just a number. To many, I'm known as a string... | 
March 14th, 2006, 05:25 PM
| | | Re: Map-like container?
Ben Pope wrote:[color=blue]
> Sensei wrote:[color=green]
>> [..]
>> Better read that book pronto, since that & at the end of Cpp for the
>> ++ operator dazzles me :)[/color]
>
>
> It's a reference. A bit like a pointer, but different.[/color]
Heh... "It's like a pointer, only it's not." | 
March 14th, 2006, 05:45 PM
| | | Re: Map-like container?
Victor Bazarov wrote:[color=blue]
> Ben Pope wrote:[color=green]
>> Sensei wrote:[color=darkred]
>>> [..]
>>> Better read that book pronto, since that & at the end of Cpp for the
>>> ++ operator dazzles me :)[/color]
>>
>>
>> It's a reference. A bit like a pointer, but different.[/color]
>
> Heh... "It's like a pointer, only it's not."[/color]
Just trying to pique his curiosity without giving too much away ;)
Ben Pope
--
I'm not just a number. To many, I'm known as a string... | 
March 15th, 2006, 08:45 AM
| | | Re: Map-like container?
Sensei wrote:
[color=blue]
> On 2006-03-14 16:35:45 +0100, Artie Gold <artiegold@austin.rr.com> said:
>
>[color=green]
>> Sounds like a multimap to me. ;-)[/color]
>
>
> Can you explain this?
>
> Following the SGI documentation, I understand it is possible to use
> multimap with pairs, so the only way to achieve what I want is to be
> tricky:
>
> multimap<int, int> adj;
>
> multimap<int, int>::iterator it_adj;
>
> /*
> 1:
> 2, 4, 1
>
> 2:
> 6, 9, 4, 2, 1, 0
> */
>
> adj.insert(pair<int,int>(1,2));[/color]
adj.insert(make_pair(1,2));
[color=blue]
> adj.insert(pair<int,int>(1,4));
> // ``2'' inserted before ending the key ``1''
> adj.insert(pair<int,int>(2,6));
> adj.insert(pair<int,int>(2,9));
> adj.insert(pair<int,int>(2,4));
> adj.insert(pair<int,int>(2,1));
> adj.insert(pair<int,int>(2,0));
> // insert last ``1''
> adj.insert(pair<int,int>(1,1));
> adj.insert(pair<int,int>(2,2));
>
> it_adj = adj.find(1);
>
> while ((*it_adj).first == 1)[/color]
Use equal_range instead:
typedef multimap<int, int>::iterator iter;
pair<iter, iter> b = adj.equal_range(1);
for (iter it = b.first; it != b.second; ++it)
cout << "that's 2 key and value " << (*it).second << endl;
}
[color=blue]
> So I feel like there's no other way than this and a by-hand use of
> map/lists... am I right? :)[/color] | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|