合并矢量

时间:2013-04-13 06:20:51

标签: c++ vector merge

到目前为止这是我的代码,我想要做的是说我输入1 2 3为第一个矢量和 9 8 7对于第二个向量,我希望它打印我们的1 9 2 8 3 7.但我无法弄明白,有人能指出我正确的方向。提前谢谢。

#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);
{
  cout << "Merged: ";

  vector<int> all_nodes(a.size() + b.size());

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

      merge(a.begin(), a.end(), b.begin(), b.end(), all_nodes.begin());
  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 << " ";
    } 
}

2 个答案:

答案 0 :(得分:2)

Assuming both the vectors of same size

vector<int> vec1;
vector<int> vec2;
vector<int> result;

vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);

vec2.push_back(7);
vec2.push_back(8);
vec2.push_back(9);

int nsize = vec1.size();//Take any one vector;
int j = 0;
for(int i=0;i<nsize;i++)
{
  result.push_back(vec1[i]);
  result.push_back(vec2[i]);
}

答案 1 :(得分:1)

您的合并代码完全与您的追加代码相同。这怎么样?你没有考虑的另一个问题是当你有不等大小的向量时合并是如何工作的。在问自己这个问题之前,你无法解决这个问题。

这样的事情可能就是你所需要的。重要的区别是只有一个循环。另一个区别是我使用push_back来构建向量,而不是试图预先计算它的大小。我觉得这个算法更加自然。

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

    for (i = 0; i < n || i < m; i++)
    {
        if (i < n)
           c.push_back(a[i]);
        if (i < m)
           c.push_back(b[i]);
    }
    return c;
}