Inserting vector into vector


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..
I kinda need to insert as many vectors (M) as there are min values in another vector (C).
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.
   }
}

(not tested)
Hey, thanks for trying to help me!

The thing is, I did exactly the same you did a little while back, but I was told that I have to do it without using int "i" in for.

I know that it makes no sense doing something the hard way when there's an easy way, but that's what I have to do.

Is it at least possible? I spent more than 5 hours on this one...

It would be great if you could help me figure it out.

Thanks!
I don't understand what you really want. But I've tried my best and now I've written this code:
Maybe it can help you:

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
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

int main() {

	int arr_nMyInts[] = {19, 56, 8, 11};
	vector<int> vMyVector(arr_nMyInts, arr_nMyInts + sizeof(arr_nMyInts) / sizeof(int));

	int arr_nAnotherArray[] = {15, 20, 23, 25};
	vector<int> vAnotherVector(arr_nAnotherArray, arr_nAnotherArray + sizeof(arr_nAnotherArray) / sizeof(int));

	vector<int>::iterator it;
	vector<int>::iterator min = vMyVector.begin();

	cout << "1) Values of \'vMyVector\':\n" << endl;
	for(it = vMyVector.begin(); it < vMyVector.end(); it++) {
		cout << *it << endl;
	}
	cout << endl;

	for(it = vMyVector.begin(); it < vMyVector.end(); it++) {
		if(*it < *min) {
			min = it;
		}
	}

	vMyVector.insert(min += 1, vAnotherVector.begin(), vAnotherVector.end());

	cout << "2) Values of \'vMyVector\':\n" << endl;
	for(it = vMyVector.begin(); it < vMyVector.end(); it++) {
		cout << *it << endl;
	}
 
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	return 0;
}


Snowy greetings from Germany
Fab
Oh ... I was to slow. ;-)
Fab, thanks a lot for your help!

That's not exactly what I needed, but thanks!
I believe the program you wrote works only if there's one min value. :)

No problem.

Yes, you are right. I believed that there is only one min-value.
well, you could probably cheat.
1
2
3
4
5
6
7
for(it = C.begin(); it != C.end(); it++){
   if(*it == min){
      int j = it-C.begin();//totally not 'i' :-)
      C.insert(it, M.begin(), M.end());
      it = C.begin()+j+M.size();
   }
}


I have a (wasteful) idea!
1
2
3
4
5
6
vector<sometype> D;
for(it = C.begin(); it != C.end(); it++){
   D.push_back(*it);
   if(*it == min) D.insert(D.end(), M.begin(), M.end());
}
C = D;
Omh! (Oh my hamsterman)

Thanks a lot!
The first solution was exactly what I needed!

P.S. Pastebėjau, kad lietuvis, tai ačiū :).
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.

Greetings Fab
Fab - yes :).

Either way, the problem is solved.

Thanks a lot guys!
Topic archived. No new replies allowed.