cant spot嵌套for循环错误

时间:2018-04-15 07:45:47

标签: c++ sorting

尝试对州内的县人口进行排序。按数字排序很容易,它试图将名称与导致我悲伤的排序人群相匹配。我很确定我的问题是在我的最后一个嵌套for循环中,我的县按pop函数排序。

struct county { 
   string name;  //name of county 
   string *city;  // array of city names in county 
   int cities;  //number of cities in county 
   int population;  //total population of county 
   float avg_income;  //avg household income 
   float avg_house;  //avg household price 
}; 
struct state { 
  string name;  //name of state 
  struct county *c;  //array of counties 
  int counties;  //number of counties in state 
  int population;  //total population of state 
 };


void counties_sorted_by_pop(state * states_array, int num_states){
        vector<int> counties;
        int num_counties_total = 0;
        for(int i = 0; i < num_states; i++){
                        num_counties_total += states_array[i].counties;
        }
        cout << "COUNTIES SORTED BY POPULATION\n";
        for(int i = 0; i < num_states; i++){
                for(int j = 0; j < states_array[i].counties; j++){
                        counties.push_back(states_array[i].c[j].population);
                }
        }
        sort(counties.begin(), counties.end());
        for(int i = 0; i < num_counties_total; i++){
                cout << counties[i] << endl;
        }
        for(int i = 0; i < num_states; i++){
                cout << "in first loop\n";
                for(int j = 0; j < states_array[i].counties; j++){
                        if(counties[j] == states_array[i].c[j].population){
                                cout << states_array[i].c[j].name << endl;
                        }
                }
        }
        cout << endl;
}

这是我得到的输出

按人口分类的县

50000
53000个
65000个
65000个
75000个
75000个
80000个
80000个
在第一个循环中 在第一个循环中 在第一个循环中 在第一个循环中

我遇到的错误是将正确的县名与已排序的人口矢量匹配,然后将其打印出来。有人能发现我的错误吗?

3 个答案:

答案 0 :(得分:0)

我想你想要实现的是打印按人口排序的县名。

for(int i = 0; i < num_states; i++) {
  cout << "in first loop\n";
  for(int j = 0; j < states_array[i].counties; j++) {
    if(counties[] == states_array[i].c[j].population) {
      cout << states_array[i].c[j].name << endl;
    }
  }
}

第一个问题出在if(counties[] == states_array[i].c[j].population)。您没有索引县(counties[])中的任何元素。因此,您无法将此与您从states_array[i].c[j].population获得的int值进行比较。
要解决这个问题,你必须在一个循环遍历排序县的循环中包裹最后一个循环:

for(int k = 0; k < num_counties_total; k++) {
  for(int i = 0; i < num_states; i++) {
    cout << "in first loop\n";
    for(int j = 0; j < states_array[i].counties; j++) {
      if(counties[k] == states_array[i].c[j].population) {
        cout << states_array[i].c[j].name << endl;
      }
    }
  }
}


尽管这个想法是正确的(只有每个县都有不同的人口),这种方法非常缓慢(排序具有O(nlogn)+匹配县的复杂性,人口具有复杂度O(n ^ 2),其中n是总数县数)。为了加快速度,您可以制作新的vector<county>。将所有州的所有县放入此向量中,根据其人口对其进行排序并打印已排序县的名称。要根据人口进行排序,您必须使用自己的比较功能。您可以在此处找到更多信息:http://www.cplusplus.com/reference/algorithm/sort/

编辑: 即使在两个县拥有相同人口的情况下,要有一个正确的程序,你应该将所有县放在县的载体中并对它们进行排序。

bool my_cmp(county a, county b) {
  return a.population < b.population;
}

void counties_sorted_by_pop(state * states_array, int num_states) {
  vector<county> counties;
  for(int i = 0; i < num_states; i++){
    for(int j = 0; j < states_array[i].counties; j++){
      //put all the counties to the vector<county>
      counties.push_back(states_array[i].c[j]);
    }
  }

  //sort counties with custom compare function
  sort(counties.begin(), counties.end(), my_cmp);

  //print sorted vector
  for(int i = 0; i < counties.size(); i++) {
    cout << counties[i].name << endl;
  }
}

答案 1 :(得分:0)

我不确定我是否正确理解了您的问题,但我认为std::multimap可以帮助您,这样的事情:

#include <map>

void counties_sorted_by_pop(state * states_array, int num_states)
{
    multimap<int, string> sorted_counties_name;

    int num_counties_total = 0;
    for(int i = 0; i < num_states; i++)
    {
        num_counties_total += states_array[i].counties;
    }
    cout << "COUNTIES SORTED BY POPULATION\n";
    for(int i = 0; i < num_states; i++)
    {
        for(int j = 0; j < states_array[i].counties; j++)
        {
            sorted_counties_name.insert(std::pair<int, string>(states_array[i].c[j].population, states_array[i].c[j].name));
        }
    }

    for(auto iter = sorted_counties_name.begin(); iter != sorted_counties_name.end(); ++iter)
    {
        cout << iter->first << "   " << iter->second << endl;
    }
}

我认为这种方式的复杂性为O(nlog n)

答案 2 :(得分:0)

由于您counties独立于states_array排序,因此两个容器的序列将不匹配。一种解决方法是使用

std::map<std::string, int> countyNamePopulation

使用std::sort对其进行排序并在循环中输出

for (const auto& e : countyNamePopulation)
{
    std::cout << "County name: " << e.first << ", Population: " << e.second;
}

下次请添加一个带有示例域代码的主函数(即州和县对象),以便想要帮助的人可以立即调试。此外,还需要对任务进行解释。欢迎来到SO!

相关问题