473,320 Members | 1,859 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,320 software developers and data experts.

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 1957
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kaede | last post by:
Hi all, Consider the following code fragment: // some data structure class Data { ... } // Container for the data structure Class Container {
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
11
by: Steve Jorgensen | last post by:
I just came up with a really tidy little solution to the VB/VBA circular reference issue. It only works with Access 2000 or newer, but that's about the only down-side. The issue... You need an...
12
by: Mortos | last post by:
I need some quick advice. I just need to reference a containing class from the nested class. I'm familiar with Java but C# is proving tricky. public BigClass { public int ID_BigClass = -99; ...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
0
by: toton | last post by:
Hi, I have a little design related problem, and finding a good way to resolve it. To state the problem, I have points from writing of several documents. Each document represents a Session class ,...
12
by: John Henry | last post by:
Hi list, Just to make sure I understand this. Since there is no "pointer" type in Python, I like to know how I do that. For instance, if I do: ...some_huge_list is a huge list...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: AK | last post by:
Hello, I need to do the following with an xml document which has a list of assets: 1. Hash the assets 2. Hash the element describing the assets 3. Create a digital signature (using X.509...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.