473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 <se******@mac.com>

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

Mar 14 '06 #1
16 2049
Sensei <se******@mac.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Mar 14 '06 #2
Sensei wrote:
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
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 14 '06 #3
Sensei wrote:
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>
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
Mar 14 '06 #4
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;
}

--
TB @ SWEDEN
Mar 14 '06 #5
On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.********@comAcast.net> 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<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> >
The class seems the same as a map of int and list<something> to me :)
except of course, the = operator.
Remember that free cheese is only found in the mousetrap.


This is very true.

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

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

Mar 14 '06 #6
Sensei wrote:
On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.********@comAcast.net>
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<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.


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


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


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

Mar 14 '06 #7
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.
Mar 14 '06 #8
Gernot Frisch wrote:
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.


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

Mar 14 '06 #9
... 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
Mar 14 '06 #10
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.


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


....now that I think of it... forget all I said.

Mar 14 '06 #11
On 2006-03-14 16:35:45 +0100, Artie Gold <ar*******@austin.rr.com> said:

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

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

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

Mar 14 '06 #12
On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net> said:
... 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.


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

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

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

Mar 14 '06 #13
Sensei wrote:
On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net> said:
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 :)


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...
Mar 14 '06 #14
Ben Pope wrote:
Sensei wrote:
[..]
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."
Mar 14 '06 #15
Victor Bazarov wrote:
Ben Pope wrote:
Sensei wrote:
[..]
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
--
I'm not just a number. To many, I'm known as a string...
Mar 14 '06 #16
Sensei wrote:
On 2006-03-14 16:35:45 +0100, Artie Gold <ar*******@austin.rr.com> said:

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(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;
}
So I feel like there's no other way than this and a by-hand use of
map/lists... am I right? :)


Mar 15 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's...
6
by: Noixe | last post by:
Hello, I'm italian then sorry for my bad english: In this source #include <iostream> #include <map> #include <string>
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
0
by: windandwaves | last post by:
Hi Folk Some of you may find this function useful. It makes it easier to create maps using Google Maps. I link it to a database of points. The main use of the function is that it creates all...
17
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find...
5
by: Haro Panosyan | last post by:
#include <iostream> #include <map> #include <string> using namespace std; void InitMap( map<string, int>& Map) { Map = 0;
11
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that...
7
by: Jim Langston | last post by:
I'm working on a program, and at one time I find it necessary to load classes into a map. Now, these classes don't have default constructors, so I can't retrieve them using MyMap. So I wound...
3
by: maheshr22 | last post by:
Hey Heres what i want to do: I want to create a map which has <string, vector<vector<int>>>. The string is always unique. What i need to do is, as i read each string from a file, i compare it...
1
by: JimpsEd | last post by:
Hi all, This is not a Google Map's question per se, but probably a simply JavaScript concept I am missing. I'll describe the scenario nonetheless. My map is loaded into: <div id="map"></div>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.