0

I have a set of arrays with cities, countries, latitudes, and latitudes. c++ language.

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]);
}

How do i sort the array of latitudes and longitudes at the same time to find top five lowest or highest to all the others on the list but at the same time do no lose the elements of cities and countries those lats n longs are associated with?

LihO
  • 41,190
  • 11
  • 99
  • 167
VKid
  • 38
  • 1
  • 11
  • 4
    It would be much easier if you combined city, country, lat, long into a structure or class, and then used a single array (or better, vector). – Paul R Oct 09 '13 at 19:12
  • how would i do that? haven't really worked with vector or structures yet. – VKid Oct 09 '13 at 19:13
  • 1
    The best solution is "don't use parallel arrays": aggregrate all the information into a list (or vector) of records instead. – paulsm4 Oct 09 '13 at 19:13
  • 2
    @user2770315 You google "c(++) struct tutorial" (or read a beginners' book), then you google "`std::vector` documentation", and you'll be good to do. –  Oct 09 '13 at 19:14

2 Answers2

5

"at the same time do not lose the elements of cities and countries those lats n longs are associated with"

When these are the values that belong together, why haven't you bundled them within a single object? i.e.:
struct Location {
    std::string city, country;
    double lng, lat;
};

and once you load all locations into the std::vector<Location> then you could just define own comparator and use std::sort.

This question might help you: How to use std::sort with a vector of structures and compare function?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • confused on how to load it into struct from cvs file and then intilizing the variables. – VKid Oct 09 '13 at 19:17
  • 1
    @user2770315: [This question](http://stackoverflow.com/q/16446665/1168156) was first hit when I was looking for "reading values from CSV file in C++". – LihO Oct 09 '13 at 19:20
0

If you create a class or a struct that holds your data together (instead of associating them via array indexes), you will find it much easier to manage:

struct Details
{
    std::string city;
    std::string country;
    double latitude;
    double longitude;
};

struct csv_reader : std::ctype<char>
{
    csv_reader() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
        rc[','] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space;
        return &rc[0];
    }
};

// in your program logic
std::ifstream fin("worldcities.csv");
std::vector<Details> vecDetails;
std::string line;
csv_reader reader;
while (std::getline(fin, line))
{
    std::istringstream iss(line);
    iss.imbue(std::locale(std::locale(), &csv_reader));
    Details d;
    iss >> d.city >> d.country >> d.latitude >> d.longitude;
    vecDetails.push_back(d);
}

// to sort by latitude
std::sort(vecDetails.begin(), vecDetails.end(), [](const Details& l, const Details& r)
{
    return l.latitude < r.latitude;
});
Zac Howland
  • 15,777
  • 1
  • 26
  • 42