C++ vector erase

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster

C++ vector erase

 
0
  #1
Oct 16th, 2009
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)

  1. for(int i = 0; i < dirPath.top().dirPtrs.capacity(); i++)
  2. {
  3. string temp = StringToUpper((dirPath.top()).dirPtrs.at(i).getFileName());
  4. if(command.compare(temp) == 0)
  5. {
  6. dirPath.top().dirPtrs::iterator it= dirPath.top().dirPtrs.erase(dirPath.top().dirPtrs.begin()+1);
  7. foundDir = true;
  8. }
  9. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
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/
  1. std::vector<randomType> myVec;
  2. // Fill the vector with something
  3. std::vector<randomType>::iterator iter; // const_iterator if you aren't gonna be modifying anything
  4. iter = myVec.begin(); // Points to the first element
  5. myVec.erase( iter ); // Will delete the first element
  6.  
  7. // Delete a searched element (#include <algorithm>)
  8. randomType value = ...;
  9. iter = std::find( myVec.begin(), myVec.end(), value );
  10. if ( iter != myVec.end() )
  11. myVec.erase( iter );
I didn't test this or anything. Just an example.

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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster
 
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
  1. dirPath.top().iter = dirPath.top().dirPtrs.erase(dirPath.top().dirPtrs.begin()+1);
and added an iterator to my File.h
  1. vector<File> dirPtrs; //Array of pointers to files if you're using a directory
  2. vector<File>::iterator iter;
and in the constructor (File.cpp) set the iterator to dirPtrs.Begin();
  1. File::File(string FileName, int Size, string UserName, bool IsDir)
  2. {
  3. fileName.assign(FileName);
  4. size = Size;
  5. createdBy.assign(UserName);
  6. isDir = IsDir;
  7. iter = dirPtrs.begin();
  8. }
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()?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster
 
0
  #4
Oct 16th, 2009
boy do I feel stupid...probably shouldn't use vector.capacity() once I switched my traversals to (int)vector.size() I stopped getting out of bounds exceptions. I mean that would make sense >.<

Thanks for the help on the iterator though, I did end up using that!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC