【新手求教】C++合并数组的??

只要马渕洸的双叶 2015-05-13 08:58:47
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
int m,n;
int a[100],b[100],c[100];
while(cin>>m)
{
for(int i = 0;i < m;i++)
cin >> a[i];
}
while(cin >> n)
{
int t = m;
for(int j = 0;j < n;j++)
{
cin >> b[j];
c[t] = b[j];
t++;
}
}
sort(c[0],c[m+n-1]);
for(int i = 0;i < m+n;i++)
cout<<c[i]<<" ";
cout<<endl;
}
题目要求合并数组并排序!不去重复的。。
不能运行,提示:两个重载中没有一个可以转换成所有参数的
要怎么解决啊????
求帮助,,谢谢各位!
...全文
289 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
懂了,,谢谢
苏叔叔 2015-05-14
  • 打赏
  • 举报
回复
这样可好:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
	int i, j, r;
	int m, n;
	int a[100], b[100], c[100];
	cin >> m;
	for (i = 0; i < m; i++) cin >> a[i];
	sort(a, a + m);
	cin >> n;
	for (i = 0; i < n; i++) cin >> b[i];
	sort(b, b + n);
	i = j = r = 0;
	while (i < m && j < n)
	{
		if (a[i] <= b[j]) c[r++] = a[i++];
		else c[r++] = b[j++];
	}
	while (i < m) c[r++] = a[i++];
	while (j < n) c[r++] = b[j++];
	for (int i = 0; i < m + n; i++) cout << c[i] << " ";
	cout << endl;
	return 0;
}
//3
//2 1 5
//4
//23 45 1 25
//1 1 2 5 23 25 45
fly_dragon_fly 2015-05-14
  • 打赏
  • 举报
回复
sort的参数是iterator,在这里应该是地址,比如c,c+m之类,这里a用来做什么的
sprawling 2015-05-13
  • 打赏
  • 举报
回复
像这样才行.
sprawling 2015-05-13
  • 打赏
  • 举报
回复
sort函数的参数是地址,你的参数搞错了.
  • 打赏
  • 举报
回复
好复杂,链表我太懂得,,而且C我好多都看不懂的‘’
707wk 2015-05-13
  • 打赏
  • 举报
回复
参考链表的二路合并算法 https://github.com/707wk/Senior-middle-school/blob/master/Filling%20in%20the%20gaps.c

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧