I've been looking around and I couldn't find a decent solution that would help me to insert one vector into another.
Heres what I have to do:
I have to find the lowest (minimum) number in vector C, and once I found it, I have to insert a vector M after it. Here's my code:
1 2 3 4 5 6
for (it = C.begin(); it != C.end(); it++) {
if (*it == *min) {
C.insert(it, M.begin(), M.end());
}
}
The main problem is inserting a vector into another vector - I get an error "Vector iterator not incrementable". Can you guys help me out?
The problem is that when you insert something into vector, it may get reallocated and then you can't know what 'it' points to.
The solution would be firstly to find an iterator ('min') pointing to the minimum value in a for loop and then do C.insert(min, M.begin(), M.end()); outside the loop.
Unless you want to insert the vector as many times as the value is repeated..
Do you have to use vectors? I believe you wouldn't be having this problem with a list.
If you do, then I suggest
1 2 3 4 5 6
for(int i = 0; i < C.size(); i++){
if(C[i] == min){
C.insert(C.begin()+i, M.begin(), M.end());
i += M.size();//in case there are any mins in M. that would be recursive.
}
}
I hope that I'm allowed to ask a comprehension question.
Am I right?
You've got on constant value (min) and you are searching for this value in your whole vector and after every value, which is equals min, you insert your second vector and continue your search after the inserted values.