c++将两个有序数组合并为一个有序数组 用函数传递 谢谢!

如题所述

用STL的muliset定义两个变量a,b,a,b分别代表一个数组。本身就是有序的,然后将b中所有元素插入到a中去就行了,最后,a就是连个有序数组a,b的集合。追问

可以给个简单的程式么。非常感谢!

追答

#include
#include
using namespace std;
multiset merge(multiset a,multiset b)
{
multiset::iterator pos;
for(pos=b.begin();pos != b.end();++pos)
{
a.insert(*pos);
}
return a;
}

int main()
{
multiset a,b,c;
multiset::iterator pos1,pos2;
for(int i=0;i<6;i++)
{
a.insert(i*2+1);
b.insert(i*3-1);
}
cout<<"the original array a is:"<<endl;
for(pos1=a.begin();pos1 != a.end();++pos1)
cout<<*pos1<<" ";
cout<<endl<<"the original array b is:"<<endl;
for(pos2=b.begin();pos2 != b.end();++pos2)
cout<<*pos2<<" ";
cout<<endl;

c=merge(a,b);
cout<<"after the merge,the new array is:"<<endl;
for(pos1=c.begin();pos1!=c.end();++pos1)
cout<<*pos1<<" ";
cout<<endl;
return 0;
}

温馨提示:答案为网友推荐,仅供参考