C++填充2个数组
C++ Filling 2 arrays
程序需要从用户那里收集字符串"name"和双倍的"speed"。用户还定义要输入的数据量。然后,它必须将数据从最快到最慢进行排序,并列出相应的"名称"及其速度。我已经成功地管理了该程序,以"获取"数据量(数组大小(的用户输入,并获取"名称"answers"速度"的用户输入(并使用exp名称[2]和速度[2]的打印输出进行测试(
但是,我一直遇到一个问题,我的程序无法工作,即使我撤消了对它工作时间的更改,等等。有时,将程序复制并粘贴到新的cpp中会解决这个问题。我确信这段代码不是最漂亮的,但是否有根本性的错误导致了这种反复出现的故障,或者是代码块的问题(一直都很好(?
int main()
{
int i,number;
cout<<"How many do you have? ";
cin>> number;
string name[number];
double speed[number];
cout<<"Enter the names and speeds (name 'space' speed): ";
cout<<endl;
for (i = 0; i < number; i++)
{
cin>>name[number]
>>speed[number];
}
cout<<endl;
cout<<name[2]<<" "<<speed[2];
return 0;
}
注意,错误发生的时间各不相同(在输入第一个int、数组中的第一个条目或根本不输入后(:
你有多少?3
进程返回-1073741571(0xC00000FD(执行时间:3.663秒按任意键继续。
我将介绍两个解决方案,一个没有高级C++,第二个将使用STL和structs
解决方案1(老派方式(:
不使用高级C++或STL的解决方案(代码中的注释将解释每个步骤(:
#include <iostream>
using namespace std;
// utility function to swap two elements
template<typename T>
void swap(T *xp, T *yp)
{
T temp = *xp;
*xp = *yp;
*yp = temp;
}
// we can do better sorting algorithm than this but its the simplest
void bubbleSort(double speeds[], string names[], int n)
{
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (speeds[j] < speeds[j+1]){ // if current elements is lower than the next one
swap(&speeds[j], &speeds[j+1]); // swap speeds
swap(&names[j], &names[j+1]); // swap names
}
}
}
}
int main()
{
int number;
cout << "How many do you have? ";
cin >> number;
string* names = new string[number];
double* speeds = new double[number];
cout << "Enter the names and speeds (name 'space' speed): " << endl;
for (int i = 0; i < number; i++)
{
cin >> names[i] >> speeds[i]; // here its i not number (number is out of range)
// this array goes from 0 to number - 1
}
bubbleSort(speeds, names, number); // call sorting algorithm O(n*n)
cout << "resutls = " << endl;
for (int i = 0; i < number; i++) // print resutls out
cout << "Name : " << names[i] << " Speed: " << speeds[i] << endl;
// free the memory we allocated from the heap (Never forget this)
delete[] names;
delete[] speeds;
return 0;
}
解决方案2(STL+结构(:
首先,由于名称和速度是耦合在一起的,您可能需要将它们放在
所谓的结构中,该结构是一个将您的(名称、速度(数据放在一起的包
还有其他选择,您可能希望使用std::pair,但由于您希望稍后按升序对数据进行排序,我们将坚持使用结构并定义运算符<这将在以后用于排序。
// We pack our data inside one struct that contain the name and the speed
struct NameSpeed
{
string name;
double speed;
bool operator < (const NameSpeed& other) const // we need to define this operator for sorting
{
return (speed < other.speed); // sort based on speed field
}
};
现在,由于我们从用户那里获得的输入意味着在编译时没有定义数组的大小,我们可能需要使用动态内存
STL(C++标准库(提供std::vector,它是动态分配的数组,并且能够调整大小,因此它比新操作符和删除操作符更灵活、更安全。
最后,这里是您需要的带有注释的代码,以澄清每个步骤:
#include <iostream>
#include <vector> // we need this for the vector class which is like an array
#include <algorithm> // we need this to sort the array later
using namespace std;
// We pack our data inside one struct that contain the name and the speed
struct NameSpeed
{
string name;
double speed;
bool operator < (const NameSpeed& other) const // we need to define this operator for sorting
{
return (speed < other.speed);
}
};
int main()
{
int i,number;
cout<<"How many do you have? ";
cin>> number;
vector<NameSpeed> items(number); // resize our array to have number as
// size ( better than NameSpeed items[number];
// which is allocated on the stack and we need dynamic memory
// and vector manage this for us)
cout<<"Enter the names and speeds (name 'space' speed): ";
cout<<endl;
// Please note that vector class have the operator[]
// which allow us to use it just like a normal array
for (i = 0; i < number; i++) // iterate
{
cin >> items[i].name >> items[i].speed; // get the data from the user name, speed respectively
}
cout<<endl;
sort(items.begin(), items.end()); // sort our array based on speed
for (const NameSpeed& ns : items) // Iterate through our array
cout << "Name :" << ns.name << "Speed :" << ns.speed << endl; // print name speed respectively
return 0;
}
- 如何在C++中比较两个char数组
- 在c++中获取两个大int,并将它们存储在数组中
- 如何以优化的方式同时迭代两个间距不相等的数组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 如何从txt文件中读取多个不同长度的数组?
- C++,数组有多少个地址?
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 为什么 2 个相同数组的元素彼此相等
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 可以将两个相同类型的连续数组视为一个数组吗?
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 使用嵌套函数数组是个好主意吗?
- 链表数组(5 个队列)
- 是否有可能通过溢出 C 中的第一个元素来写入数组第二个元素
- 指向数组第一个元素的指针的地址
- 用 std::valarray 替换 POD C 样式数组是个坏主意吗?
- OpenGL:两个顶点数组 + 两个索引数组
- c++多维数组多个数据类型
- 使用C++AMP处理大型数组(65536+个元素)
- 在c++中使用MKL函数初始化数组是个好主意吗?