Advertisement

merging vectors

Started by June 09, 2021 11:26 PM
23 comments, last by shubham.puri.7742 3 years, 4 months ago

I am trying to merge two vectors and return a vector. if vector a is {1,4,9,16} and vector b is {9,7,4,9,11} and the merged vector is {1,9,4,7,9,4,16,9,11} here is my beginning code.

#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

vector<int> merge(vector<int>, vector<int>);

int main() 
{
	int num_one, num_two, exit = 0;
	vector<int> num_a;
	vector<int> num_b;
	while (exit != -1)
	{
		cout << "Enter first vector element: ";
		cin >> num_one;
		cout << endl;

		cout << "Enter second vector element: ";
		cin >> num_two;
		cout << endl;

		cout << merge(num_a, num_b) << endl;
	
		cout << "Enter exit(-1): ";
		cin >> exit;
		cout << endl;
	}

	return 0;
}

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




}

I would get hanged for answering this, i'd like to see pure std implementation of that with one line. But the idea is to copy both contents to another stream,

And you don't pass cin elements to defined vectors.

After cin you can write num_a.push_back(num_one);

Same for second one

Now to write merge function.first of all i am unsure if cout will shootout vector elements and with that you can do for loop and cout each vector element cout << num_a[i]

Now there is a misconception of merge. Despite this fact you can write something like this

vector<int> merge(vector<int> a, vector<int> b)

{

int as = int( a.size() );

int bs = int(b.size());

std::vector<int> p;

for (int i=0; i< as;i++)

}

Advertisement

I am still stuck on how to return a vector from function of vectors, here is my updated code.

#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

vector<int> merge(vector<int>, vector<int>);

int main() 
{
	int num_one, num_two, exit = 0;
	vector<int> num_a;
	vector<int> num_b;
	while (exit != -1)
	{
		cout << "Enter first vector element: ";
		cin >> num_one;
		cout << endl;

		cout << "Enter second vector element: ";
		cin >> num_two;
		cout << endl;

		num_a.push_back(num_one);
		num_b.push_back(num_two);

		cout << merge(num_a, num_b) << endl;
	
		cout << "Enter exit(-1): ";
		cin >> exit;
		cout << endl;
	}

	return 0;
}

vector<int> merge(vector<int> a, vector<int> b)
{
	int as = int(a.size());
	int bs = int(b.size());

	for (int i = 0; i < as; i++)
	{
		cout << a[i];
	}

	for (int i = 0; i < bs; i++)
	{
		cout << b[i];
	}
}

cout is copying the value to your output screen. While you can read it then, it's not inside the program any more.

Instead make a new vector inside ‘merge’, fill it with whatever you want returned from the function, and at the end, return the new vector with a ‘return’ statement as normal.

Allright, that was odd that editor cut half of my post, without detailing in correctness of any code below you want to do this:

Note that it seems this is some kind of school project you need to pass since this is ridiculous piece of code

Feel free to ask

	std::vector<int> * merge(std::vector<int> a, std::vector<int> b)
    {
    std::vector<int> * p = new std::vector<int>();
    int as = int( a.size() );
    int bs = int( b.size() );
    //either use memcpy or this loop below (for memcpy you need to call p->resize(as+bs);
    for (int i=0; i < as; i++) p->push_back(a[i]);
    for (int i=0; i < bs; i++) p->push_back(b[i]);
    return p;
    }

    but this is not ultimate solution for you not to mention in your case vector will get destroyed (not in the code above)
    anyway, code above returns a new position in memory where data is associated - you do not want that cause from what i understand you want to merge everything till -1 is written



    //the code
    std::vector<int> * p;
    void merge(std::vector<int> a, std::vector<int> b, std::vector<int> *& p)
    {
        int as = int(a.size());
        int bs = int(b.size());

        for (int i=0; i < as; i++) p->push_back(a[i]);
        for (int i=0; i < bs; i++) p->push_back(b[i]);
    }

    void ShootOut(std::vector<int> * a)
    {
        int as = int(a->size());
        for (int i=0; i < as; i++)  cout << (*a)[i];
    }
            num_a.clear();
    num_b.clear();
    int main()
    {
    p = new std::vector<int>();
        int num_one, num_two, exit = 0;
        vector<int> num_a;
        vector<int> num_b;
        while (exit != -1)
        {
            cout << "Enter first vector element: ";
            cin >> num_one;
            cout << endl;

            cout << "Enter second vector element: ";
            cin >> num_two;
            cout << endl;

//this is ridicilous thou you want to merge that into p so you should actually clear num_a and num_b
            num_a.clear();
            num_b.clear();
            num_a.push_back(num_one);
            num_b.push_back(num_two);



            merge(num_a, num_b, p);


            ShootOut(p); //you don't want to to use first merge function that returns a new pointer element
            cout << endl;
            cout << "Enter exit(-1): ";
            cin >> exit;
            cout << endl;
        }

        return 0;
    }

Well i'm bored again…

@_weirdcat_ s solution did not respect the expected interleaved order form OP.

So here's my solution:

std::vector<int> Merge(const std::vector<int> &a, const std::vector<int> &b)
{
	std::vector<int> merged;
	merged.reserve(a.size() + b.size());

	size_t maxSize = max(a.size(), b.size());
	for (size_t i=0; i < maxSize; i++)
	{
		if (i < a.size()) merged.push_back(a[i]);
		if (i < b.size()) merged.push_back(b[i]);
	}
	return merged;
}

Did not test it.

Advertisement

well joe I implemented your code but I getting a hex number when I run my code. here is my code, I have almost solved my problem. thanks for all the code

#include <iostream>
#include <vector>
#include <math.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

vector<int> Merge(const vector<int>&, const vector<int>&);

int main() 
{
	int num_one, num_two, exit = 0;
	vector<int> num_a(100);
	vector<int> num_b(100);
	while (exit != -1)
	{
		cout << "Enter first vector element: ";
		cin >> num_one;
		cout << endl;

		cout << "Enter second vector element: ";
		cin >> num_two;
		cout << endl;

		num_a.push_back(num_one);
		num_b.push_back(num_two);

		Merge(num_a, num_b);

		cout << "Enter exit(-1): ";
		cin >> exit;
		cout << endl;
	}

	cout << Merge;

	return 0;
}
vector<int> Merge(const vector<int> &a, const vector<int> &b)
{
	std::vector<int> merged;
	merged.reserve(a.size() + b.size());

	size_t maxSize = max(a.size(), b.size());
	for (size_t i = 0; i < maxSize; i++)
	{
		if (i < a.size()) merged.push_back(a[i]);
		if (i < b.size()) merged.push_back(b[i]);
	}
	return merged;
}

pbivens67 said:
cout << Merge;

Likely this prints some memory address of the vector, and you need to iterate to print the numbers:

for (size_t i = 0; i < merged.size(); i++) 
	cout << merged[i];

Oh no - ‘Merge’ is the function name, so you printed address of the function, and the result of it's actual call was never stored.

So need to change some more things:

int main() 
{
	int num_one, num_two, exit = 0;
	vector<int> num_a(100);
	vector<int> num_b(100);
	while (exit != -1)
	{
		cout << "Enter first vector element: ";
		cin >> num_one;
		cout << endl;

		cout << "Enter second vector element: ";
		cin >> num_two;
		cout << endl;

		num_a.push_back(num_one);
		num_b.push_back(num_two);

		//Merge(num_a, num_b); // returned result was not stored

		cout << "Enter exit(-1): ";
		cin >> exit;
		cout << endl;
	}

	//cout << Merge;
	
	
vector<int> merged = Merge(num_a, num_b);
	
	
for (size_t i = 0; i < merged.size(); i++) 
		cout << merged[i];

	return 0;
}
vector<int> Merge(const vector<int> &a, const vector<int> &b)
{
	std::vector<int> merged;
	merged.reserve(a.size() + b.size());

	size_t maxSize = max(a.size(), b.size());
	for (size_t i = 0; i < maxSize; i++)
	{
		if (i < a.size()) merged.push_back(a[i]);
		if (i < b.size()) merged.push_back(b[i]);
	}
	return merged;
}

well it prints out alot of zeros and then the numbers here is my updated code

#include <iostream>
#include <vector>
#include <math.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

vector<int> Merge(const vector<int>&, const vector<int>&);

int main() 
{
	int num_one, num_two, exit = 0;
	vector<int> num_a(100);
	vector<int> num_b(100);
	while (exit != -1)
	{
		cout << "Enter first vector element: ";
		cin >> num_one;
		cout << endl;

		cout << "Enter second vector element: ";
		cin >> num_two;
		cout << endl;

		num_a.push_back(num_one);
		num_b.push_back(num_two);

//		Merge(num_a, num_b);

		cout << "Enter exit(-1): ";
		cin >> exit;
		cout << endl;
	}

//	cout << Merge;
	vector<int> merged = Merge(num_a, num_b);


	for (size_t i = 0; i < merged.size(); i++)
		cout << merged[i];

	return 0;
}
vector<int> Merge(const vector<int> &a, const vector<int> &b)
{
	std::vector<int> merged;
	merged.reserve(a.size() + b.size());

	size_t maxSize = max(a.size(), b.size());
	for (size_t i = 0; i < maxSize; i++)
	{
		if (i < a.size()) merged.push_back(a[i]);
		if (i < b.size()) merged.push_back(b[i]);
	}
	return merged;
}

pbivens67 said:
vector num_a(100); vector num_b(100);

This creates 100 undefined numbers, and on input we only set new numbers after that. To fix it, simply create empty vectors:

vector num_a, num_b;

Also my output is ugly. This should look better:

cout << merged[i] << “ , ”;

This topic is closed to new replies.

Advertisement