|
-
June 14th, 2011, 04:33 AM
#1
Vector Iterators Incompatible
Hello, I am working on a project, and I keep getting the Vector Iterators Incompatible. Perhaps someone here knows what is going on. I am trying to use the erase function from std::vector and it results in this error every time. Since my project is starting to get really complex, I created a console application simplifying what I am really trying to do.
Code:
#include "stdafx.h"
#include <vector>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
vector<vector<int>> vecvec;
vector<int> vec;
for(int i=0;i<10;i++)
vec.push_back(i);
vecvec.push_back(vec);
vector<int>::iterator it = vecvec[0].begin();
bool Found=false;
while(it!=vecvec[0].end()&&Found==false)
{
if((*it)==5)
{
Found=true;
vecvec[0].erase(it);
}
else
it++;
}
return 0;
}
What I am trying to do is erase a single integer element from a vector which is inside another vector. I do have the index of the desired vector in which I want to search. Am I declaring wrong the iterators? The problem is when the program tries to erase the element, otherwise it works perfectly because it does find the desired element.
Thanks in advance for the help.
-
June 14th, 2011, 04:37 AM
#2
Re: Vector Iterators Incompatible
Calling erase invalidates all iterators and pointers to elements of a vector. After this call you are left with an invalid iterator, which you then compare to vecvec[0].end(). You should use std::find instead of writing your own loop.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 14th, 2011, 04:44 AM
#3
Re: Vector Iterators Incompatible
Well, I cant find the std::find function. Is it located in another library? I am using Visual C++ 2008 Express Edition by the way, if that changes anything.
And how should I erase the position after finding it?
Im sorry, I am kind of inexperienced with std::vector.
-
June 14th, 2011, 04:45 AM
#4
Re: Vector Iterators Incompatible
vecvec[0].erase(it);
break;
-
June 14th, 2011, 04:46 AM
#5
Re: Vector Iterators Incompatible
Oh that works. Thank you very much, I feel bad for not finding that out myself
-
June 14th, 2011, 05:32 AM
#6
Re: Vector Iterators Incompatible
 Originally Posted by limpit
Well, I cant find the std::find function. Is it located in another library? I am using Visual C++ 2008 Express Edition by the way, if that changes anything.
And how should I erase the position after finding it?
Im sorry, I am kind of inexperienced with std::vector.
How are your online search skills? Second hit on google: http://www.cplusplus.com/reference/algorithm/find/
You need to include the <algorithm> header for std::find.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 14th, 2011, 09:56 AM
#7
Re: Vector Iterators Incompatible
 Originally Posted by limpit
Hello, I am working on a project, and I keep getting the Vector Iterators Incompatible.
Code:
int main(array<System::String ^> ^args)
That code is not C++. Do you know what languages you're using to develop your programs with? What are those "^" thingabobs doing there?
That language is something called "Managed C++", and it is off-topic here.
Here is a C++ version of your program that does not use hand-coded loops or flags to erase an element in the inner vectors:
Code:
#include <vector>
#include <algorithm>
using namespace std;
struct EraseMe
{
private:
int m_num;
public:
EraseMe(int num) : m_num(num) { }
void operator()(std::vector<int>& vect) const
{
vect.erase(remove(vect.begin(), vect.end(), m_num), vect.end());
}
};
int main()
{
vector<vector<int>> vecvec;
vector<int> vec;
for(int i=0;i<10;i++)
vec.push_back(i);
vecvec.push_back(vec);
for_each(vecvec.begin(), vecvec.end(), EraseMe(5));
return 0;
}
Note the use of remove(), vect.erase(), for_each(), function objects. Also note how main() is declared properly.
The nice thing about this is I don't need to run it and I know it works, and the reason is that I use the algorithms. Algorithms work every single time if you pass them the correct parameters.
With your code, I need to run it, and look at it more than onces to make sure it erases correctly.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|