Map-like container?

S

Sensei

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!!
 
J

Jakob Bieling

Sensei said:
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> >

So why not use just that?

regards
 
A

Artie Gold

Sensei said:
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 :)

Sounds like a multimap to me. ;-)

HTH,
--ag
 
V

Victor Bazarov

Sensei said:
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 said:
> 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>
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
 
T

TB

Sensei skrev:
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!!

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;
}
 
S

Sensei

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 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.
<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>

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 :)
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.

This is very true.
 
R

Rolf Magnus

Sensei said:
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.

There are.
This construct seems quite useful, that's why I'm asking.


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 :)

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.
The class seems the same as a map of int and list<something> to me :)
except of course, the = operator.

Well, it's still unclear what you want. What's wrong with the map of int and
list<int> (or int and vector<int>)?
 
G

Gernot Frisch

Sounds like a multimap to me. ;-)

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.
 
R

Rolf Magnus

Gernot said:
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.

Hmm, what would be the difference? You store several values of the same type
under one key.
 
G

Gernot Frisch

... I feel better with memset and alike... but unfortunately for
me, C++ is required, and I must adapt myself.

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
 
S

Sensei

Sounds like a multimap to me. ;-)


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? :)
 
S

Sensei

Oh boy! Get a C++ book and start thinking. You don't know what you're missing.

I bought C++, third edition, from Bjarne Stroustrup himself... not that
I bought it from him I mean :)
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?

Possibly not. So, I think I'm writing C code in C++...
C++ is _not_ C(++)

it is rather:

class Cpp : public C
{
public:
virtual Cpp& operator++() {return C::data++;}
};

Better read that book pronto, since that & at the end of Cpp for the ++
operator dazzles me :)
 
B

Ben Pope

Sensei said:
Better read that book pronto, since that & at the end of Cpp for the ++
operator dazzles me :)

It's a reference. A bit like a pointer, but different.

Ben Pope
 
B

Ben Pope

Victor said:
Ben said:
Sensei said:
[..]
Better read that book pronto, since that & at the end of Cpp for the
++ operator dazzles me :)


It's a reference. A bit like a pointer, but different.

Heh... "It's like a pointer, only it's not."

Just trying to pique his curiosity without giving too much away ;)

Ben Pope
 
R

Rolf Magnus

Sensei said:
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(make_pair(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)

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top