vector insert function

My program should use the function find_if() to find the odd numbers in the vector and add a -1 in front of them, for some reason I'm getting an error?
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #include <iostream>
#include <ctime>
#include <vector>
#include <iomanip>
#include<algorithm>

const int SIZE =20;
unsigned int seed = (unsigned)time(0);
using namespace std;

double random (unsigned int &seed);
void print_vector (vector<int> :: iterator b,
                   vector<int> :: iterator e );
void initialize_vector(vector<int> &v);
bool odd(int i);
void insert_before_odd(vector<int>&v);
vector<int>v;

int main()
{
    vector<int>v;
    
    cout << endl;
    initialize_vector(v);
    cout << "Original vector is: " << endl;
    print_vector(v.begin(), v.end());
    insert_before_odd(v);
    cout << "Vector after erasing odds:" << endl;
    print_vector(v.begin(), v.end());
    cout << endl;
    
    return 0;
}

double random(unsigned int &seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
}

void initialize_vector(vector<int> &v)
{
    for (int i=0; i<SIZE; i++)
        v.push_back(random(seed)*10);
}

void print_vector (vector<int> :: iterator b,
                   vector<int> :: iterator e )
{
    vector<int> :: iterator p =b;
    while(p<e)
        cout << setw(3) << (*p++);
    cout << endl;
}

bool odd(int i)
{
    return i%2 ;
}

void insert_before_odd(vector<int>&v)
{
    vector<int> :: iterator p = v.begin();
    while (p!=v.end())
    {
        p = find_if(p, v.end(), odd);
    
        if(p!= v.end())
            v.insert(p,-1);
    }
}
You find an odd number
... then insert a -1 in front of it
... so that your iterator now points to the -1
... which is odd
... so you put a -1 in front of that
... and again
... and again
until your code gives up.

You would have to move two places forward after insertion.

One way would be changing
v.insert(p,-1);
to
{v.insert(p,-1); p+=2; }
but it looks horrible and may or may not be portable.


BTW, you are inserting a -1, not deleting odd numbers as implied by line 28. If you want to erase odd numbers use the erase-remove idiom (or erase-remove_if here).
v.erase( remove_if( v.begin(), v.end(), odd ), v.end() );
Last edited on
Hello cash,

The answer is 42 https://www.youtube.com/watch?v=aboZctrHfK8

You should realize that:
for some reason I'm getting an error
Does mot tell anyone what the problem is.

If i load the program any errors I get may be different from yours. Also you do not explain what is wrong.

I will give it a test and see what I find.

Andy
try this
if(p!= v.end())
{
v.insert(p,-1);
p+=2; //move p past the inserted 1 and past the value it found. it keeps finding the same value until it runs out of memory.
}

did you know that you could just insert the -1s as you build the vector? generate a random value, if its odd, insert a -1 then insert it, else ... if that makes sense in the bigger picture of whatever this is for (if there is anything more) ...
Last edited on
One way would be changing
v.insert(p,-1);
to
{v.insert(p,-1); p+=2; }
but it looks horrible and may or may not be portable.

Note: http://www.cplusplus.com/reference/vector/vector/insert/
If a reallocation happens, all iterators, pointers and references related to the container are invalidated.
Otherwise, only those pointing to position and beyond are invalidated, with all iterators, pointers and references to elements before position guaranteed to keep referring to the same elements they were referring to before the call.

Return value
An iterator that points to the first of the newly inserted elements.

Therefore:
{ p = v.insert( p, -1 ); p+=2; }
Thanks for the correction, @keskiverto. I completely missed the crucial p =

Insert isn't very efficient either. Might be better to build a new vector incrementally.
Last edited on
I missed the iterator invalidation as well, good catch indeed. I even saw the symptoms of this and missed it /blush .. I saw it adding random values to the array (before the p+=2) after it had added enough -1s to it to make it resize, and didnt connect the dots.

I still hate iterators lol. Its like someone hated pointers so much, they made something worse.
Last edited on
Its like someone hated pointers so much, they made something worse.

Why would someone base the design on something that they hate? It doesn't make sense.
Hiding paradigm... Ive seen a lot of times when people dislike something unavoidable, they hide it behind an interface... which is exactly what iterators are doing.
Iterator is more about making ++ polymorphic than about hiding the pointer.
Topic archived. No new replies allowed.