-1

到目前为止,这是我的代码,我想做的是说我为第一个向量输入 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 << " ";
    } 
}
4

2 回答 2

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]);
}
于 2013-04-13T06:29:42.750 回答
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;
}
于 2013-04-13T06:51:40.453 回答