STL save reference inside container

R

Rares Vernica

Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsigned> X;

I would like to be able to save a reference to a position inside X.

For a vector, the reference would be the index inside the vector. For
map the reference would be the key (in this case a string). The problem
is that I don't want to store the string as it might be too long. I
would like to have a smaller size reference, like an unsigned or a pointer.

Thanks,
Ray
 
J

jrojaspin

Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsigned> X;

I would like to be able to save a reference to a position inside X.

For a vector, the reference would be the index inside the vector. For
map the reference would be the key (in this case a string). The problem
is that I don't want to store the string as it might be too long. I
would like to have a smaller size reference, like an unsigned or a pointer.

Thanks,
Ray

hi ray, i'm not so shure i fully understand, but, here it goes :)

i dont know if its possible to have a 'pointer' to inside of a
container ... and even if you could, i think that it would only make
sense for one item of it. if you had more than one, you would have to
find it in a list (or somewhere else and you'd be losing all you
gain).

if the string length is something that bothers into your sistem, you
could try make a hash-table structure.
it takes the best of both worlds. if you could asign an 'unique'
number to each lengthy string, you could use it as the map-key.

yes, this has its own pack of problems (but nothing a list cant fix);
maybe this is too much work if only a couple of keys are length, but
if you can find an easy function string->number its something to
consider.

hope this can help/orient/at-least-not-confuse you =)
 
J

John Harrison

Rares said:
Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsigned> X;

I would like to be able to save a reference to a position inside X.

For a vector, the reference would be the index inside the vector. For
map the reference would be the key (in this case a string). The problem
is that I don't want to store the string as it might be too long. I
would like to have a smaller size reference, like an unsigned or a pointer.

Thanks,
Ray


Use an iterator, that's what they're for.

typedef map<string, unsigned> su_map;

su_map X;
X["abc"] = 1;
X["def"] = 2;
X["ghj"] = 3;
X["wer"] = 4;

su_map::iterator i = X.find("def");

i is now a 'reference' to the position of ("def", 2) inside the map.

You really need to read up on iterators in the STL, yu're not going to
get very far without them.

john
 
R

Rares Vernica

John said:
Rares said:
Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsigned> X;

I would like to be able to save a reference to a position inside X.

For a vector, the reference would be the index inside the vector. For
map the reference would be the key (in this case a string). The
problem is that I don't want to store the string as it might be too
long. I would like to have a smaller size reference, like an unsigned
or a pointer.

Thanks,
Ray


Use an iterator, that's what they're for.

typedef map<string, unsigned> su_map;

su_map X;
X["abc"] = 1;
X["def"] = 2;
X["ghj"] = 3;
X["wer"] = 4;

su_map::iterator i = X.find("def");

i is now a 'reference' to the position of ("def", 2) inside the map.

You really need to read up on iterators in the STL, yu're not going to
get very far without them.

john

Hi,

I tried the iterators. Here is the next phase of my problem.

Assume I get references inside map<stirng, unsigned>. Now, I need to
store this references inside a set, that is set<reference_type>.

If reference_type is map<...>::iterator then the problem is that
elements of reference_type cannot be stored in a set as they need to be
comparable with "<".

So, the full problem is that I need to store references to a map<...> in
a set<...>.

Thanks a lot,
Ray
 
J

John Harrison

Rares said:
John said:
Rares said:
Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsigned> X;

I would like to be able to save a reference to a position inside X.

For a vector, the reference would be the index inside the vector. For
map the reference would be the key (in this case a string). The
problem is that I don't want to store the string as it might be too
long. I would like to have a smaller size reference, like an unsigned
or a pointer.

Thanks,
Ray



Use an iterator, that's what they're for.

typedef map<string, unsigned> su_map;

su_map X;
X["abc"] = 1;
X["def"] = 2;
X["ghj"] = 3;
X["wer"] = 4;

su_map::iterator i = X.find("def");

i is now a 'reference' to the position of ("def", 2) inside the map.

You really need to read up on iterators in the STL, yu're not going to
get very far without them.

john


Hi,

I tried the iterators. Here is the next phase of my problem.

Assume I get references inside map<stirng, unsigned>. Now, I need to
store this references inside a set, that is set<reference_type>.

If reference_type is map<...>::iterator then the problem is that
elements of reference_type cannot be stored in a set as they need to be
comparable with "<".

So, the full problem is that I need to store references to a map<...> in
a set<...>.

Thanks a lot,
Ray

When you need a set or map, but the elements of the set or map doesn't
have a predefined operator< (or the predefined one isn't what you want)
all you have to do is define your own.

It looks complicated but really is quite simple once you know the form.
Here's an example, you can look up the details in a book on the STL.

#include <map>
#include <set>
#include <string>
#include <functional>
using namespace std;

typedef map<string, unsigned> su_map;
typedef su_map::iterator su_map_iter;

struct su_map_iter_less_then : public binary_function<su_map_iter,
su_map_iter, bool>
{
bool operator()(su_map_iter x, su_map_iter y) const
{
return x->first < y->first;
}
};

int main()
{
su_map m;
m["abc"] = 1;
m["def"] = 2;
set<su_map_iter, su_map_iter_less_then> s;
s.insert(m.find("abc"));
s.insert(m.find("def"));
}

The class su_map_iter_less_then is what defines 'less than' for the set
s in the function main. It's an example of what's called a function
object or functor.

john
 

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

Margins inside flexBox 2
STL Container Choice 11
Building a Large Container 26
A Better Container Choice? 3
Save mp3s to local storage 1
template with STL container 10
STL Container? 4
The perfect STL container 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top