I have a set of data imputed into a set of arrays as the following:

for (int i=0;getline(file,(cities[i]),',');i++)
            { 
            getline(file, countries[i], ',');
            getline(file, latitudes[i], ',') ;
            getline(file, longitudes[i]); 
            }   

How do I sort the array line of latitudes and longitudes without changing countries and city it is associated with?

What don't you understand? are you allowed to sort using std::sort are you sure that arrays are the best structure for this data?

Instead of loading the fields into seperate arrays why not load them into an array of structs?

I haven't work with struct yet, how would you do that?

Alternatively, if you're not familiar with structs you could use a different approach.
You'll need to code you're own sort, and when you do the comparison you'll need to swap all four of the array elements.

A struct is a way of combining variables into one structure. You can do so as such:

typedef struct NAMEOFSTRUCT { 
  int a; char b; float c; 
  } NAMEOFTYPEDEF;

Use typedef to keep your code neater, as you can now just use NAMEOFTYPEDEF struct_name; to create this struct in memory. To access individual data members, use struct_name.a, struct_name.b, etc. If you are using a pointer, eg:

NAMEOFTYPEDEF *struct_ptr = malloc(sizeof(NAMEOFTYPEDEF));

You can access each member by using the -> operator (for example, struct_name->a).

Edit: Additionally, I noticed that this was posted in the C++ board. You can use a class instead of a struct, if you so choose. A class may have members that are functions (also known as methods), whereas a struct may only contain data (you can emulate methods inside of structs using function pointers, but a function pointer is still considered data).

Edited 3 Years Ago by linux: C++ update

This article has been dead for over six months. Start a new discussion instead.