如何同步两个数组
How to sync two arrays
我有两个大小相同的数组,它们有不同的类型需要同步。其中一个数组是我按降序排序的双打列表。另一个是与它们各自的ASCII值相对应的字符33-90的列表。在对doubles列表进行排序之前,2个数组排列得很好,但我不能用同样的方式对字符列表进行排序。我需要做的是同步位置,例如:y[i] = ch[i];
,但该命令不起作用。
void sortChars(int x[], double y[])
{
int i;
int largestIndex;
int location;
double temp;
char ch[58];
for (int i = 0; i < 58; i++)
ch[i] = 33 + i;
for (i = 0; i < 58; i++)
{
//Step a
largestIndex = i;
for (location = i + 1; location < 58; location++)
if (x[location] > x[largestIndex])
largestIndex = location;
//Step b
temp = x[largestIndex];
x[largestIndex] = x[i];
x[i] = temp;
}
for (i = 0; i < 58; i++)
{
//Step a
largestIndex = i;
for (location = i + 1; location < 58; location++)
if (y[location] > y[largestIndex])
largestIndex = location;
//Step b
temp = y[largestIndex];
y[largestIndex] = y[i];
y[i] = temp;
}
for (int i = 0; i < 58; i++)
{
cout << ch[i] << " times used: " << left << setw(20) << x[i];
cout << fixed << showpoint << setprecision(6);
cout << 't' << "% of file: " << y[i] << endl;
}
}
我尝试设置ch[i] = y[i]
,我尝试使用y[]
的选择排序,并将步骤b
中的所有y
部分更改为ch
。你将如何匹配这两个数组?请尽量不要使用std::
类型的命令,因为我的所有项目都使用using namespace std;
看起来您打算在第一个for循环中嵌套第二个和第三个for循环。像这样:
for (int i = 0; i < 58; i++)
{
ch[i] = 33 + i;
for (i = 0; i < 58; i++)
{
//Step a
largestIndex = i;
for (location = i + 1; location < 58; location++)
if (x[location] > x[largestIndex])
largestIndex = location;
//Step b
temp = x[largestIndex];
x[largestIndex] = x[i];
x[i] = temp;
}
for (i = 0; i < 58; i++)
{
//Step a
largestIndex = i;
for (location = i + 1; location < 58; location++)
if (y[location] > y[largestIndex])
largestIndex = location;
//Step b
temp = y[largestIndex];
y[largestIndex] = y[i];
y[i] = temp;
}
for (int i = 0; i < 58; i++)
{
cout << ch[i] << " times used: " << left << setw(20) << x[i];
cout << fixed << showpoint << setprecision(6);
cout << 't' << "% of file: " << y[i] << endl;
}
}
相关文章:
- 如何在C++中比较两个char数组
- 在c++中获取两个大int,并将它们存储在数组中
- 如何以优化的方式同时迭代两个间距不相等的数组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 可以将两个相同类型的连续数组视为一个数组吗?
- C++递归来决定数组中的两个值
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 给定一个大小为 N 的数组 S,检查是否可以将序列拆分为两个序列
- 在 char* 数组中添加两个索引
- 使用嵌套函数数组是个好主意吗?
- 链表数组(5 个队列)
- 是否有可能通过溢出 C 中的第一个元素来写入数组第二个元素
- 动态分配一维结构数组:两种方法
- 指向数组第一个元素的指针的地址
- 用 std::valarray 替换 POD C 样式数组是个坏主意吗?
- OpenGL:两个顶点数组 + 两个索引数组
- c++多维数组多个数据类型
- 使用C++AMP处理大型数组(65536+个元素)
- 在c++中使用MKL函数初始化数组是个好主意吗?