| | |
Update element in list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 20
Reputation:
Solved Threads: 0
Hey guys,
Can anyone post sample code about updating an object that is already in an std::list? I know that it sounds as a really stupid question, but obviously I'm doing something wrong.
In my code I'm going through the list with an iterator until I reach the object of interest. My intention is to replace the current node in the list with another object.
The following snippet of code demonstrates that but I'm sure that I do something wrong in the if statement.
Thanks in advance..!
Can anyone post sample code about updating an object that is already in an std::list? I know that it sounds as a really stupid question, but obviously I'm doing something wrong.
In my code I'm going through the list with an iterator until I reach the object of interest. My intention is to replace the current node in the list with another object.
The following snippet of code demonstrates that but I'm sure that I do something wrong in the if statement.
Thanks in advance..!
c++ Syntax (Toggle Plain Text)
bool updateListElement(Object* air) { list <Object>::iterator iObject; for (iObject= list.begin(); iObject != list.end(); ++iObject) { Object temp = *iObject; if (temp.getCode() == air->getCode()) { temp=*air; return true; } } return false; }
1
#2 Oct 22nd, 2009
Close, but a temporary copy of the object is still an independent copy. C++ does not make reference copies of objects like Java or C#. To replace the object the iterator refers to, you need to assign to the dereferenced iterator. This will overwrite the object that it refers to:
C++ Syntax (Toggle Plain Text)
bool updateListElement(Object* air) { std::list <Object>::iterator iObject; for (iObject= list.begin(); iObject != list.end(); ++iObject) { Object temp = *iObject; if (temp.getCode() == air->getCode()) { *iObject = *air; return true; } } return false; }
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- return each element of a list (Python)
- How to check element of list are present in other list? (Python)
- link list with copy and delete elements (C++)
- deleting an element in a linked list (C++)
- Minimal element in a linked list? (Computer Science)
- Quicksorting linked list - simple algorithm (C)
- how to assign a constant element of a list (C)
- Checking for duplicates in a orderedered linked list (C++)
- Need Help to Print Doubly Linked List(DLL) (Java)
Other Threads in the C++ Forum
- Previous Thread: Object's value turn into -858993460?
- Next Thread: Sorting
| Thread Tools | Search this Thread |
7 64bit 2010 apple array arrays browser bulletin button c# collaboration control count database datepart design directory element error errors file filename firefox firmware ftp gamer gaming input install internet iphone iphone2.1 java jvm leopard link linked list lists login mac method microsoft mozilla msaccess mssql news numerical object order oriented osx output panel password patch php playstation pointer prototype ps3 pwnage python radio ratings recursion remote security session sharepoint software sony sorting sql sqlserver string syntax tables trojan tuple update upload user virus vulnerability win windows xp






