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)

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.

Recommended Answers

All 3 Replies

All that changes with iterators that aren't 'int's is the template argument. http://www.cplusplus.com/reference/stl/vector/erase/

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 );

I didn't test this or anything. Just an example.

Just look at how I defined the iterator type and you should be fine.

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

dirPath.top().iter = dirPath.top().dirPtrs.erase(dirPath.top().dirPtrs.begin()+1);

and added an iterator to my File.h

vector<File> dirPtrs; //Array of pointers to files if you're using a directory
vector<File>::iterator iter;

and in the constructor (File.cpp) set the iterator to dirPtrs.Begin();

File::File(string FileName, int Size, string UserName, bool IsDir)
{
      fileName.assign(FileName);
      size = Size;
      createdBy.assign(UserName);
      isDir = IsDir;
      iter = 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()?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.