473,783 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1984
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::iterato r 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::iterato r 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_t ype>.

If reference_type is map<...>::itera tor 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::iterat or 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_t ype>.

If reference_type is map<...>::itera tor 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::iterato r su_map_iter;

struct su_map_iter_les s_then : public binary_function <su_map_iter,
su_map_iter, bool>
{
bool operator()(su_m ap_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_les s_thens;
s.insert(m.find ("abc"));
s.insert(m.find ("def"));
}

The class su_map_iter_les s_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
2285
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
5974
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
9055
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 object model that includes a container object, and the object in the container need to interact with the container itself. The problem is that, to access the container, each item needs a reference to the container, and that constitutes a...
12
4045
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; public MiniClass{ public void MiniFunc(){
3
4235
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 that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
0
1442
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 , and a SessionManager holds a deque of Session. Session holds deque of Page for that document. Page in turn holds vector of Char in that page. However Char do not hold the Point associated with that Char. Instead it holds a pair of iterator for...
12
1639
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
12402
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
2363
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 certificate) over the hashes from step 1 and 2 Most of the examples I've been looking at are doing a digital
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.