How to Merge two vectors

can someone help me out here, say i have two vectors, one equals [1,2,3] and the other equals [9,8,7],what i am trying to do is make it so that the program prints out [1 9 2 8 3 7]. if anyone can point me onto the right direction i would greatly appreciate it. thank you who comment in advance.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <algorithm> 
#include <vector>

using namespace std;

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;
}

vector<int> merge(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;
}

vector<int> merge_sorted(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 main()
{
    cout << "Please enter a set of numbers, insert -1 when done.\n";
    vector<int>a;
    bool more = true;
    while (more)
    {
        int n;
        cin >> n;
        if (n == -1)
            more = false;
        else
            a.push_back(n);
    }


    cout << "Please enter another set of numbers, insert -1 when done.\n";
    vector<int>b;
    more = true;
    while (more)
    {   
        int m;
        cin >> m;
        if (m == -1)
            more = false; 
        else 
            b.push_back(m);
    }

    vector<int>d = append(a,b);
    {
  
        int i;
		cout << "Appended: ";
        for (i= 0; i < d.size(); i++)

       cout << d[i] << " ";
	   cout << "\n";
	}

	vector<int>r = merge(a,b);
	{
		//int i;

	    cout << "Merged: ";

	   
		//for (i = 0; i < r.size();++i)
		/*{
			merge (a.begin(), a.end(), b.begin(), b.end(), r.begin());
		}*/
		//cout << r[i] << " ";

		sort (a.end(),a.begin());
		sort (b.end(),b.begin());
		merge (a.end(),a.begin(),b.end(),b.begin(),r.begin());

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

	    cout << "\n";
	}
    vector<int>z = merge_sorted(a,b);
	{

	a.insert( a.end(), b.begin(), b.end() );
	sort( a.begin(), a.end() );

	cout << "Sorted: ";

	for (vector<int>::iterator it = a.begin(); it != a.end(); ++it)
    cout << *it << " ";
	cout << "\n";
	} 
}
Last edited on
> one equals [1,2,3] and the other equals [9,8,7],
> what i am trying to do is make it so that the program prints out [1 9 2 8 3 7].

To avoid confusion, you should not call that operation merge.

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

std::vector<int> interleave( const std::vector<int>& a, const std::vector<int>& b )
{
   std::vector<int> result ;

   auto m = std::min( a.size(), b.size() ) ;

   for( std::size_t i=0 ; i<m ; ++i )
   {
       result.push_back(a[i]) ;
       result.push_back(b[i]) ;
   }
   if( m < a.size() ) result.insert( result.end(), a.begin()+m, a.end() ) ;
   if( m < b.size() ) result.insert( result.end(), b.begin()+m, b.end() ) ;

   return result ;
}


int main()
{
    for( int v : interleave( {0,1,2,3,4,5}, {6,7,8,9} ) ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}


http://liveworkspace.org/code/1JkyNQ$0
Topic archived. No new replies allowed.