| | |
C++ vector erase
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
Ok this is driving me nuts. I'm working in C++ and trying to delete an element from a vector
It's going something like this (dirPath is a stack of directories, and dirPtrs the list of what a directory points to)
From what I've seen online I have to use an iterator but the only examples of that I can find are when using a vector that holds the type<int> dirPtrs holds my own object type (<File>).
I should've just used an array >.< but it will be resized often so it lead me to using a vector
The issue being every time I try to do what I see in online examples dirPtrs::iterator it does not work. It gives me iterator is not a member of "File" being the header file for my object type I assume, and that dirPtrs is not a class or namespace name.
It's going something like this (dirPath is a stack of directories, and dirPtrs the list of what a directory points to)
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < dirPath.top().dirPtrs.capacity(); i++) { string temp = StringToUpper((dirPath.top()).dirPtrs.at(i).getFileName()); if(command.compare(temp) == 0) { dirPath.top().dirPtrs::iterator it= dirPath.top().dirPtrs.erase(dirPath.top().dirPtrs.begin()+1); foundDir = true; } }
From what I've seen online I have to use an iterator but the only examples of that I can find are when using a vector that holds the type<int> dirPtrs holds my own object type (<File>).
I should've just used an array >.< but it will be resized often so it lead me to using a vector
The issue being every time I try to do what I see in online examples dirPtrs::iterator it does not work. It gives me iterator is not a member of "File" being the header file for my object type I assume, and that dirPtrs is not a class or namespace name.
0
#2 Oct 16th, 2009
All that changes with iterators that aren't 'int's is the template argument. http://www.cplusplus.com/reference/stl/vector/erase/
I didn't test this or anything. Just an example.
Just look at how I defined the iterator type and you should be fine.
C++ Syntax (Toggle Plain Text)
std::vector<randomType> myVec; // Fill the vector with something std::vector<randomType>::iterator iter; // const_iterator if you aren't gonna be modifying anything iter = myVec.begin(); // Points to the first element myVec.erase( iter ); // Will delete the first element // Delete a searched element (#include <algorithm>) randomType value = ...; iter = std::find( myVec.begin(), myVec.end(), value ); if ( iter != myVec.end() ) myVec.erase( iter );
Just look at how I defined the iterator type and you should be fine.
Last edited by twomers; Oct 16th, 2009 at 4:04 pm.
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
0
#3 Oct 16th, 2009
Ok, so I have it doing that now. But now when I go to traverse the vector it still ends up out of range.
I changed this line the for loop
and added an iterator to my File.h
and in the constructor (File.cpp) set the iterator to dirPtrs.Begin();
It does erase it fine now, but I can't traverse it again afterwards...After erasing do I have to set it back to dirPtrs.Begin()?
I changed this line the for loop
C++ Syntax (Toggle Plain Text)
dirPath.top().iter = dirPath.top().dirPtrs.erase(dirPath.top().dirPtrs.begin()+1);
C++ Syntax (Toggle Plain Text)
vector<File> dirPtrs; //Array of pointers to files if you're using a directory vector<File>::iterator iter;
C++ Syntax (Toggle Plain Text)
File::File(string FileName, int Size, string UserName, bool IsDir) { fileName.assign(FileName); size = Size; createdBy.assign(UserName); isDir = IsDir; iter = dirPtrs.begin(); }
![]() |
Similar Threads
- Does std::vector's pop_back free the memory? (C++)
- Assigning vector element to vector iterator (C++)
- Erasing Elements in a Vector (C++)
- vector::erase (C++)
- stl vector - can you delete by position? (C++)
Other Threads in the C++ Forum
- Previous Thread: free() = delete or delete[]?
- Next Thread: output of 0 when i know everything has a value?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






