appending vectors then testing output

My roomie and I are in a class and we're trying to do a project....

I get error's when i try to compile this program and i'm not sure why. i'm trying to create a function (append) which combined to vectors then in main i'm trying to test the function to see if it works, while allowing the user to input into the vectors, but IDK if i'm passing the arguments right when i say append(mya, myb)

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
#include <iostream>
#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;
}

int main ()
{
	int input;
	int input2;
 for (int count=0; count<10; count++)
 	{

  	vector<int> mya;
  	cout << "Enter your numbers to be evaluated: " << endl;
  	cin >> input;
  	mya.push_back(input);
  	}
 for (int count1=0; count1<10; count1++)
 	{
 	
	 vector<int> myb;
  	cout << "Enter your numbers to be evaluated again: " << endl;
  	cin >> input2;
  	myb.push_back(input2);
    }
    vector<int> myTotal;
    myTotal = append (mya, myb);
    cout << "did it go right?" << myTotal;
    
    return 0;
}
closed account (3qX21hU5)
Just a suggestion, but you could just use the merge() function to merge both of the vectors together...

Here is a example of it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>    // Need this for the merge() function

using namespace std;

int main ()
{
    vector<int> one = {1, 2, 3, 4, 5};
    vector<int> two = {6, 7, 8, 9, 10};

    vector<int> mergedVec(one.size() + two.size());

    merge(one.begin(), one.end(), two.begin(), two.end(), mergedVec.begin());

    for (int x : mergedVec)
        cout << x << endl;
}


You could put it in a function also if you wanted to like so but it wouldn't make to much sense.

1
2
3
4
5
6
7
8
// In a fucntion it might look like this
// Returns a merged vector of the two vectors you pass in.
vector<int> mergeVectors(vector<int> &oneVec, vector<int> &twoVec)
{
    vector<int> mergedVector(oneVec.size() + twoVec.size());
    merge(oneVec.begin(), oneVec.end(), twoVec.begin(), twoVec.end(), mergedVector.begin());
    return mergedVector;
}


NOTE: If you assignment calls for you to write your own merge function I wouldn't use the STL merge() function unless you can walk your professor through exactly what it does and how it works. Otherwise you might get a deduction for using it.
Last edited on
1
2
3
4
5
6
7
8
 for (int count=0; count<10; count++)
 	{

  	vector<int> mya;
  	cout << "Enter your numbers to be evaluated: " << endl;
  	cin >> input;
  	mya.push_back(input);
  	}


Each iteration of the for loop you create a new vector, mya, get input for one int, push that onto the vector you just created and then the vector is destroyed.

Move the definition of mya outside the for loop. Do likewise with myb in the subsequent loop.
Last edited on
Topic archived. No new replies allowed.