473,323 Members | 1,574 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,323 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 1512
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 a project herarchie and contains the fields...
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 http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I...
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? I have a list of items that need to be modified...
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 category column also holds the index no of the record...
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 into the presentation. Is it possible to somehow...
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 via a command button. I can't seem to find the...
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) NetStrm.Write(szData, 0, szData.Length); - this line ...
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 are complaining that the listbox doesn't have an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.