Merging and sorting vectors.

Apr 10, 2012 at 12:58am
Well i have an assignment for class and im having a little trouble understanding how to code this. I have 2 vectors (A & B), the goal is to 1) merge the vectors into a separate vector in the pattern of a[1], b[1], a[2], b[2], etc including duplicates if they were to occur. 2) Take those same vectors ( A & B) and merge and sort them in numerical order( lowest to biggest) then in the main function take the results and display them. Can someone set me off with some code to help me through this. Please and thank you.
Apr 11, 2012 at 6:31pm
Bump?
Apr 11, 2012 at 6:41pm
What have you got so far??
Apr 11, 2012 at 7:07pm
We need something to work off, attempt the assignment first.
Apr 11, 2012 at 11:59pm
This is all i have done 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
#include <iostream>
#include <vector>

using namespace std;

int vector<int> append(vector<int> a, vector<int> b)
{
    int n = a.size();
    int m = b.size();
    vector<int> c(n + m);
    int i;

    for (i = 0; i < n; i++)
        c[i] = a[i];

    for (i = 0; i < m; i++)
        c[n + i] = b[i];

    return c;
}

int vector<int> merge(vector<int> a, vector<int> b)
{
	while (counter < a.size && b.size)

}

int vector<int> merge_sorted(vector<int> a, vector<int> b)
{

}

int main()
{
    int input;

	cout<< "Input numbers for vector A. Terminate with -1"
	while (cin >> input)
		a.push_back(input)
	count<< "Input numbers for vector B. Terminate with -1"
	while (cin >> input);
		b.push_back(input);
	cout<< "Vector A is" << << "\n";
	cout<< "vector B is" << << "\n";
}
Apr 15, 2012 at 5:42am
Bump? Sorry but i really need help on this.
Apr 15, 2012 at 5:50am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <algorithm>


int main(){
    std::vector<int> myints;
    std::vector<int> mynewints;
    
    for(int i = 0; i <= 10; i++){
        myints.push_back(i);
    }
    
    copy(myints.begin(), myints.end(), mynewints); 
    
    for(int i = 0; i <= 10; i++){
        std::cout << mynewints[i]; 
    }
    return 0; 
} 

the template is
1
2
3
4
5
6
template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}
Last edited on Apr 15, 2012 at 5:53am
Apr 15, 2012 at 6:51am
Your append function could be reduced to this: It will append b to a.
a.insert(a.end(), b.begin(), b.end());

Then to sort the array, you could use an STL algorithm:
std::sort( a.begin(), a.end() );

Tada!

Here's a complete example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>    // std::vector<T>
#include <algorithm> // std::sort()
#include <iostream>  // std::cout

int main()
{
    std::vector<int> a, b;
    
    for (int i = 0; i < 10; ++i)
        a.push_back(i),                      // 0 1 2 3 4 5 6 7 8 9
        b.push_back(i+1);                    // 1 2 3 4 5 6 7 8 9 10

    a.insert( a.end(), b.begin(), b.end() ); // 0 1 2 3 4 5 6 7 8 9  1 2 3 4 5 6 7 8 9 10

    std::sort( a.begin(), a.end() );         // 0 1 1 2 2 3 3 4 4 5  5 6 6 7 7 8 8 9 9 10

    for (std::vector<int>::iterator it = a.begin(); it != a.end(); ++it)
        std::cout << *it << " ";

    return 0;
}
Last edited on Apr 15, 2012 at 7:00am
Apr 15, 2012 at 3:03pm
1
2
3
4
5
int vector<int> merge(vector<int> a, vector<int> b)
{
	while (counter < a.size && b.size)

}
Nothing then...
Suppose that the two vectors have the same size.
1
2
3
4
for(size_t K=0; K<size; ++K){
  c.push_back( a[K] );
  c.push_back( b[K] );
}
Apr 15, 2012 at 8:12pm
while (counter < a.size && b.size)
size is a function and therefore needs () behind it.
Topic archived. No new replies allowed.