469,887 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

STL save reference inside container

Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsignedX;

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
Feb 24 '07 #1
4 1782
On Feb 23, 11:07 pm, Rares Vernica <rvern...@gmail.comwrote:
Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsignedX;

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

Feb 24 '07 #2
Rares Vernica wrote:
Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsignedX;

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, unsignedsu_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
Feb 24 '07 #3
John Harrison wrote:
Rares Vernica wrote:
>Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsignedX;

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, unsignedsu_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
Feb 24 '07 #4
Rares Vernica wrote:
John Harrison wrote:
>Rares Vernica wrote:
>>Hi,

How can I save a reference inside a container?

For example I have:
map<string, unsignedX;

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, unsignedsu_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, unsignedsu_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_thens;
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
Feb 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by toton | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.