C++填充2个数组

C++ Filling 2 arrays

本文关键字:数组 2个 填充 C++      更新时间:2023-10-16

程序需要从用户那里收集字符串"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;
}