C++ 在下面的程序中,如何在findLowest功能中显示事故次数最少的地区的名称?
GetNumAcidents函数允许用户输入每个地区报告的事故数量C++ 在下面的程序中,如何在findLowest功能中显示事故次数最少的地区的名称?,c++,C++,GetNumAcidents函数允许用户输入每个地区报告的事故数量 int getNumAccidents(string ); void findLowest(double , double , double , double , double); int main() { string regName; double north = 0; double south = 0; double east = 0; double west = 0; d
int getNumAccidents(string );
void findLowest(double , double , double , double , double);
int main()
{
string regName;
double north = 0;
double south = 0;
double east = 0;
double west = 0;
double central = 0;
double lowest;
regName = "North";
north = getNumAccidents(regName);
regName = "South";
south = getNumAccidents(regName);
regName = "East";
east = getNumAccidents(regName);
regName = "West";
west = getNumAccidents(regName);
regName = "Central";
central = getNumAccidents(regName);
findLowest(north, south, east, west, central);
return 0;
}
int getnumacidents(字符串regionName)
{
双数;
不能声明一个结构,其字段为double
和string
void findLowest(double n, double s, double e, double w, double c)
{
double lowest = n;
if (s < lowest)
{
lowest = s;
}
if (e < lowest)
{
lowest = e;
}
if (w < lowest)
{
lowest = w;
}
if (c < lowest)
{
lowest = c;
}
cout << "\nThe least number of accidents was " << lowest << "." << endl;
//cout << "The area was " << r << "." << endl;
}
存储每个区域名称及其对应的计数。findLowest
如下所示:
struct A
{
double count;
string region;
};
pair< string, double > P;
P=make_pair("north", 12.12);
cout<<P.first<<" "<<P.second<<endl; //prints north 12.12..
另一种方法是使用C++STL集合
。创建一个集合
。为此,您不需要显式查找最低值。集合
自动对插入其中的元素进行排序。示例代码如下所示:
struct A
{
double count;
string region;
};
pair< string, double > P;
P=make_pair("north", 12.12);
cout<<P.first<<" "<<P.second<<endl; //prints north 12.12..
setSt;
圣嵌件(成对(12.34,“北”);
圣嵌件(成对(34.56,“南”);
圣镶块(成对(10.12,“东”);
当我这样做时,函数会显示最后一个区域名称“Central”。它不会显示事故次数最少的区域的名称。有没有一种方法可以不使用结构来实现这一点?
set< double, string > St;
St.insert(make_pair(12.34, "north"));
St.insert(make_pair(34.56, "south"));
St.insert(make_pair(10.12, "east"));
cout<<St.begin()->second<<endl; //prints 'east'..