| | |
map question
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
How do you erase an element thats inside a map. Not the key but the element the key is pointing to? This doesnt seem to be doing what i want it to.
C++ Syntax (Toggle Plain Text)
map<int, vector<int> > my_map; map<int, vector<int> >::iterator it; void erase(int num) { for(map::iterator item=my_map.begin();item != my_map.end()) { if(it == num) my_map.erase(it++); else ++it; } }
looks like there's a couple things, I don't think that would even compile: comparing a map<int, vector<int> >::iterator to an int inside your loop doesn't seem safe to me. As well as not declaring the map type for your iterator.
anyway, see if this is a good starting point:
I think that's what you were going for, hope that's helpful
~J
anyway, see if this is a good starting point:
c++ Syntax (Toggle Plain Text)
map<int, vector<int> > my_map; //I assume these are global or in a class, as you had it map<int, vector<int> >::iterator it; void erase(int num) { for(it = my_map.begin(); it != my_map.end(); it++) { if((*it).first == num) { my_map.erase(it); break; } } }
I think that's what you were going for, hope that's helpful
~J
Ps. 121
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
![]() |
Similar Threads
- Simple Map Question (C++)
- Area Shape Map question (JavaScript / DHTML / AJAX)
- google map question (PHP)
- (was) STL <map> question (C++)
- STL <map> question? (C++)
- is there anyway my program can recognize words (C++)
Other Threads in the C++ Forum
- Previous Thread: Loading and Setting text
- Next Thread: Trigger sendmessage?
Views: 367 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





