我有一组包含城市、国家、纬度和纬度的数组。 c++语言。
ifstream file("worldcities.csv");
getline(file, temporay);
//inputs the file into 4 arrays for each catergories
for (i=0;getline(file,(cities[i]),',');i++)
{
getline(file, countries[i], ',');
getline(file, latitude[i], ',') ;
getline(file, longitude[i]);
}
我如何同时对纬度和经度数组进行排序,以找到列表中所有其他最低或最高的前五名,但同时又不丢失城市和国家/地区的元素,这些元素是经纬度与?
最佳答案
“同时不要丢失与经纬度相关的城市和国家/地区的元素”
当这些是属于一起的值时,为什么不将它们捆绑在一个对象中? IE。:struct Location {
std::string city, country;
double lng, lat;
};
一旦你将所有位置加载到 std::vector<Location>
中那么你可以定义自己的比较器并使用 std::sort
.
这个问题可能对您有帮助:How to use std::sort with a vector of structures and compare function?
关于c++ - 如何对包含通过索引相互关联的数据的多个数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280566/