I don't know whats wrong with my code.

my code gives me a runtime error "vector iterator not incrementable"

here is my code and I'm pretty sure the problem is in the first function but I can't put my hands on what's wrong.
by the way I'm still learning ,I know there
might be better ways to achieve the result I want but that's what I've learned so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <D:\\std_lib_facilities.h>
#include <vector>

vector<int> filter (int num,char sign,vector<int> nums)
{
	if (sign == '>')
	{
		int counter = 0;
		for (int x : nums)
		{
			if (x < num)
			{
				nums.erase(nums.begin() +counter);
			}
			++counter;
		}
	}
	else if (sign == '<')
	{
		int counter = 0;
		for (int x : nums)
		{
			if (x > num)
			{
				nums.erase(nums.begin() + counter);
			}
			++counter;
		}
	}
	return nums;
}

int main()
{
	vector <int> numbers;
	int i = 0;
	//fill the vector with ints from 1-100
	while (i < 100)
	{
		numbers.push_back (i+1);
		++i;
	}
	char choice ;
	cout << "is the number smaller than 50 (y/n)\n";
	cin >> choice;
	if (choice == 'y')
	{
		//this is suposed to filter ints in verctor numbers and remove all numbers above 50
		for (int k : filter(50, '<', numbers))
			cout << k << " ";
	}

}
You are way too eager to use the ranged for syntax.


Besides, the logic has weak points. Lets process {7, 99, 5}:
counter=0
7<50: true
erase( begin+0 ); size()==2; {99,5}

counter=1
99<50: false

counter=2
5<50: true
erase( begin+2 ); out of range

(Your error is probably from the implementation of the ranged for that does not expect a shrink.)


Do consider the remove-erase idiom. More specifically with http://www.cplusplus.com/reference/algorithm/remove_if/
To reinforce what @keskiverto said, it is in fact illegal ("results in undefined behavior" is the idiom we tend to use) to perform any operation which can invalidate an iterator while in a range-based for loop.
Thanks for the reply
now I refined the function but I have a small problem with the else if (sign == '<') when the vector contains these values (32,34,36) it gives a range error I am a beginner don't know what does that even mean
this happens when the function arguments are
filter(34,'>',(and the vector that contains the elements above))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
vector<int> filter (int num,char sign,vector<int> nums)
{
	if (sign == '>')
	{
		int counter = 0;
		while (counter < nums.size())
		{
			if (nums[counter] < num*2)
			{
				nums.erase(nums.begin() + 0);
			}
			++counter;
		}
		nums.push_back(num);
		sort(nums);
	}
           //here here here here here here here 
	else if (sign == '<')
	{
		int counter = 0;
		while (counter < nums.size()&&nums.size()!=1)
		{
			if (nums[nums.size() - 1] > num / 2 )
			{
				nums.erase(nums.begin() + (nums.size()-1 ));
			}
			++counter;
		}
	}
	else if (sign == 'o')
	{
		int counter = 0;
		while (counter < nums.size())
		{
			if (nums[counter] %2==0)
			{
				nums.erase(nums.begin() + counter);
			}
			++counter;
		}
	}
	else if (sign == 'e')
	{
		int counter = 0;
		while (counter < nums.size())
		{
			if (nums[counter] % 2 != 0)
			{
				nums.erase(nums.begin() + counter);
			}
			++counter;
		}
	}
	return nums;
}

Thanks in regards
Last edited on
You do advance the counter whether you erase an element or not. That is not a good thing.


Did you read the std::remove_if link?
Topic archived. No new replies allowed.