Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在下面的程序中,如何在findLowest功能中显示事故次数最少的地区的名称?_C++ - Fatal编程技术网

C++ 在下面的程序中,如何在findLowest功能中显示事故次数最少的地区的名称?

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

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;
    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'..