如何将两个有序的一维数组合并为一个有序的一维数组合

如何将两个有序的一维数组合并为一个有序的一维数组合


// merge_array.cpp : 定义控制台应用程序的入口点。
//

#include “stdafx.h”
#include<list>
#include<iostream>
using namespace std;

void myinsert(list<int>&pp, int num);
int _tmain(int argc, _TCHAR* argv[])
{
int a[6] = { 2, 5, 10, 19, 45, 90 };
int b[4] = { 4, 11, 17, 34 };
list<int>aa;
for (int i = 0; i < 6; i++)
{
aa.push_back(a[i]);
}
for (int i = 0; i < 4; i++)
{
myinsert(aa, b[i]);
cout << b[i] << endl;
}

list<int>::iterator iter;
for (iter = aa.begin(); iter != aa.end(); iter++)
cout << *iter<<‘,’;
cout << endl;

system(“pause”);
return 0;
}

void myinsert(list<int>&pp, int num)
{
list<int>::iterator iter;
if (num < pp.front())
pp.push_front(num);
if (num>pp.back())
pp.push_back(num);
for (iter = pp.begin(); iter != pp.end(); iter++)
{
if (*iter >= num)
{
pp.insert(iter, num);
cout << pp.size() ;
cout<<endl;
break;
}
}
}

    原文作者:zbxzc
    原文地址: https://blog.csdn.net/u014568921/article/details/46732291
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞