472,328 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

how to take a reference of inserted items?

Hello

Given this:

map<int, mystruct> m;
m[4] = item;
item.id++;
m[2] = item;

How can I get a direct refrence to the location of the just inserted item?
(which is now managed by the map)

I would do a find() then get a reference to its "second".

Same question <list> <vector> how can I get reference directly after
insertion? is the back() / front() the only way for this case?

--
Elias
Jul 22 '05 #1
3 1477
lallous wrote:
Given this:

map<int, mystruct> m;
m[4] = item;
item.id++;
m[2] = item;

How can I get a direct refrence to the location of the just inserted item?
(which is now managed by the map)

I would do a find() then get a reference to its "second".
That's not a bad idea.
Same question <list> <vector> how can I get reference directly after
insertion? is the back() / front() the only way for this case?


No. What if you inserted in the middle?

Dereferencing a non-const iterator gives you a reference. So, if you
use 'find' and then do

list<T>::iterator myObjIter = mylist.find(someT);
if (myObjIter != mylist.end()) {
T & myObjectRef = *myObjIter;
... // do something with 'myObjectRef'
}

However, keep in mind that references do become invalid, more often in
a 'vector' than in a 'list', but still.

Victor
Jul 22 '05 #2
lallous wrote:
Hello

Given this:

map<int, mystruct> m;
m[4] = item;
item.id++;
m[2] = item;

operator[] returns such a reference.
Jul 22 '05 #3
lallous wrote:
Hello

Given this:

map<int, mystruct> m;
m[4] = item;
item.id++;
m[2] = item;

How can I get a direct refrence to the location of the just
inserted item? (which is now managed by the map)

I would do a find() then get a reference to its "second".

Same question <list> <vector> how can I get reference directly
after insertion? is the back() / front() the only way for this
case?


All of them have an insert() method that returns an iterator, so:

vector<int> v(3, 0);
list<int> l;
map<char, int> m;
int& rv = *v.insert(v.begin() + 1, 35);
int& rl = *l.insert(l.end(), 17);
int& rm = m.insert(make_pair('k', 80)).first->second;
Martin

--
Quidquid latine dictum sit, altum viditur.
Jul 22 '05 #4

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

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
7
by: Wolfgang Kreuzer | last post by:
Hello all, I have two tables - Projects and ProjectStruct Table Projects contains master records of the projects, ProjectStruct allows to define...
0
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? ...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the...
2
by: Johan | last post by:
Hi, I'm writing an PowerPoint Add-In using C# which adds a toolbar and a button. When the user clicks the button, an activex control is inserted...
0
melcoats
by: melcoats | last post by:
Hello. I have a user form with a spread sheet inserted. I want a value from one of the boxes to be inserted into the cells of the spread sheet...
4
by: yogarajan | last post by:
hi i had developed pop3 Account mail view in my web page but i got error in (System.NullReference Exception: Object reference not set)...
2
by: Dave | last post by:
The problem is with my listbox on the form. I am trying to run 2 threads simulataneously an update two different listbox controls but my functions...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.