C++ 如何在C+中对一个数组进行排序并获得相应的第二个数组+;?
我有两个相同大小的数组。并希望对其中一个数组进行正常排序,然后对下一个数组元素进行相应排序。 假设我们有两个数组C++ 如何在C+中对一个数组进行排序并获得相应的第二个数组+;?,c++,arrays,sorting,C++,Arrays,Sorting,我有两个相同大小的数组。并希望对其中一个数组进行正常排序,然后对下一个数组元素进行相应排序。 假设我们有两个数组 e[] = {4,2,3,7,6,8} s[] = {2,0,4,3,7,9} 首先我想对数组e进行正常排序,然后根据e元素位置的变化来改变s数组的元素位置 这应该是最终的结果 e[] = {2,3,4,6,7,8} s[] = {0,4,2,7,3,9} 我该怎么做?我应该使用一个类对象,将这两个对象作为私有成员,然后继续吗?如果是,怎么做?创建单个(或)对象数组,其中第一个来
e[] = {4,2,3,7,6,8}
s[] = {2,0,4,3,7,9}
首先我想对数组e
进行正常排序,然后根据e
元素位置的变化来改变s
数组的元素位置
这应该是最终的结果
e[] = {2,3,4,6,7,8}
s[] = {0,4,2,7,3,9}
我该怎么做?我应该使用一个类对象,将这两个对象作为私有成员,然后继续吗?如果是,怎么做?创建单个(或)对象数组,其中第一个
来自第一个数组,而第二个
来自第二个数组。然后只需与自定义比较器函数一起使用,该函数只使用对中的秒
进行比较。迭代排序的数组(或向量)并拆分为原始数组
注意:如果每个数组中的值是紧密耦合的,那么考虑将它们放入一个结构或类中,而不是使用两个(或更多个)不同的数组。
<代码>空隙BuffelRoT(int e[],int s[],int n){void bubbleSort(int e[], int s[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (e[i] > e[i + 1]) {
tmp = e[i];
e[i] = e[i + 1];
e[i + 1] = tmp;
tmp = s[i];
s[i] = s[i + 1];
s[i + 1] = tmp;
swapped = true;
}
}
}
}
bool swapped=true;
int j=0;
int tmp;
while(交换){
交换=假;
j++;
对于(int i=0;i- 我想的是使用
std::map
- 将数组
元素指定为键e
- 以及相应的数组
元素作为值s
- 然后按键对文件进行排序
- 浏览地图并逐个获取值
int compare(const void *pp0, const void *pp1)
{
int i0 = **(int **)pp0;
int i1 = **(int **)pp1;
if(i0 > i1)return -1;
if(i0 < i1)return 1;
return 0;
}
int main()
{
int e[...];
int s[...];
int *pe[...];
size_t i, j, k;
int te, ts;
/* ... */
/* create array of pointers to e[] */
for(i = 0; i < sizeof(e)/sizeof(e[0]); i++)
pe[i] = &e[i];
/* sort array of pointers */
qsort(pe, sizeof(e)/sizeof(e[0]), sizeof(pe[0]), compare);
/* reorder e[] and s[] according to the array of pointers */
for(i = 0; i < sizeof(e)/sizeof(e[0]); i++){
if(i != pe[i]-e){
te = e[i];
ts = s[i];
k = i;
while(i != (j = pe[k]-e)){
e[k] = e[j];
s[k] = s[j];
pe[k] = &e[k];
k = j;
}
e[k] = te;
s[k] = ts;
pe[k] = &e[k];
}
}
/* ... */
return 0;
}
int比较(常量无效*pp0,常量无效*pp1)
{
int i0=**(int**)pp0;
int i1=**(int**)pp1;
如果(i0>i1)返回-1;
如果(i0
如果不想使用其他数据结构,请坚持使用两个不同的整数数组
下面的代码片段将帮助您
int _tmain(int argc, _TCHAR* argv[])
{
int e[] = {4,2,3,7,6,8} ;
int s[] = {2,0,4,3,7,9} ;
int temp ;
int Array_Size = 6 ; // don't use hard coded value here, rather calculate from array size
for(int i = 0 ; i < Array_Size ; i++)
{
for(int j = i+1 ; j < Array_Size ; j++)
{
if(e[i] > e[j])
{
// Swap elements in first array
temp = e[j] ;
e[j] = e[i] ;
e[i] = temp ;
// As you want both arrays in sync
// Swap elements in second array here itself
temp = s[j] ;
s[j] = s[i] ;
s[i] = temp ;
}
}
}
return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
int e[]={4,2,3,7,6,8};
int s[]={2,0,4,3,7,9};
内部温度;
int Array_Size=6;//此处不使用硬编码值,而是根据数组大小计算
for(int i=0;ie[j])
{
//交换第一个数组中的元素
温度=e[j];
e[j]=e[i];
e[i]=温度;
//因为您希望两个阵列同步
//交换第二个数组中的元素
温度=s[j];
s[j]=s[i];
s[i]=温度;
}
}
}
返回0;
}
使用结构非常简单。您的基本任务是对两个数组进行排序。我想建议一种方法。使用具有两个变量x和y的结构,这两个变量在此处用于两个数组。制作此结构的两个对象数组
struct Point
{
int x,y;
};
struct Point arr[n];
在为结构对象数组提供条目后,使用STL函数
sort(arr,arr+n,myfun);
其中myfun是根据您的需要进行排序的功能,其定义为
bool myfun(struct Point a,struct Point b)
{
if(a.x<b.x)
return true;
else
return false;
}
bool myfun(结构点a、结构点b)
{
if(a.xn;
结构点arr[n];
//输入第一个数组
对于(int i=0;i>arr[i].x;
//输入第二个数组
对于(int i=0;i>arr[i].y;
//在myfun的帮助下进行分类
排序(arr、arr+n、myfun);
//现在打印阵列
对于(int i=0;i遇到问题时,请尝试更简单的方法。如果只有一个数组,您将如何排序?似乎您的数据结构错误。您应该尝试使用二维数组。您可能希望检查此问题,并且该问题答案中的链接也已按键排序,无需单独排序步骤。错误提示stion自己实现排序(而不是最好的开始)这不是C++处理序列的方法。为了使它成为正确答案,请提供好的解释方法,以及为什么它比 STD::/Cord>算法好。你认为我不知道如何用STD::排序?这个问题?你假设每个问题都符合C++ WA。
#include <bits/stdc++.h>
using namespace std;
struct Point{
int x,y;
};
bool myfun(struct Point a,struct Point b)
{
if(a.x<b.x)
return true;
else
return false;
}
int main()
{
int n; //n is the number of elements of array
cin>>n;
struct Point arr[n];
//Enter first array
for(int i=0;i<n;i++)
cin>>arr[i].x;
//Enter second array
for(int i=0;i<n;i++)
cin>>arr[i].y;
//sorting with the help of myfun
sort(arr,arr+n,myfun);
//now print the arrays
for(int i=0;i<n;i++)
cout<<arr[i].x<<" ";
cout<<"\n";
for(int i=0;i<n;i++)
cout<<arr[i].y<<" ";
return 0;
}