tags:

views:

185

answers:

7

I'm writing a program where the user inputs names and then ages. The program then sorts the list alphabetically and outputs the pairs. However, I'm not sure how to keep the ages matched up with the names after sorting them alphabetically. All I've got so far is...

Edit: Changed the code to this -

#include "std_lib_facilities.h"

struct People{
       string name;
       int age;
};

int main()
{
    vector<People>nameage;
    cout << "Enter name then age until done. Press enter, 0, enter to continue.:\n";
    People name;
    People age;
    while(name != "0"){
                 cin >> name;
                 nameage.push_back(name);
                 cin >> age;
                 nameage.push_back(age);}
    vector<People>::iterator i = (nameage.end()-1);
    nameage.erase(i);    
}

I get compiler errors for the != operator and the cin operators. Not sure what to do.

+3  A: 

Hi,

You should consider putting names and ages together in structured record.

Then sort the records.

J.

jeje
+10  A: 

Rather than two vectors (one for names, and one for ages), have a vector of a new type that contains both:

struct Person
{
    string name;
    double age;
};

vector<Person> people;


edit for comments:

Keep in mind what you're now pushing onto the vector. You must push something of type Person. You can do this in a couple of ways:

Push back a default constructed person and then set the name and age fields:

people.push_back(Person());
people.back().name = name;
people.back().age = age;

Give Person a constructor that takes a name and an age, and push a Person with some values:

struct Person
{
    Person(const string& name_, double age_) : name(name_), age(age_) {}
    string name;
    double age;
};

people.push_back(Person(name, age));

Create a Person, give it some values, and push that into the vector:

Person person;
person.name = name;
person.age = age;

people.push_back(person);

Or more simply:

Person person = { name, age };
people.push_back(person);

(thanks avakar)

luke
I tried this, but for some reason I can't use push_back for the values.
Alex
You can do `Person person = { name, age }; people.push_back(person);`
avakar
+1  A: 

You either need to swap elements in both vectors at the same time (the FORTRAN way), or store a vector of structs or pairs. The later approach is more idiomatic for c-like languages.

dmckee
FORTRAN has structures now too: http://en.wikipedia.org/wiki/Fortran_language_features#Derived_data_types
PTBNL
@PTBNL: Depends of the flavor of FORTRAN: before Fortran 90 there were no structures in the standards, and only a few compilers supported them at all. I used the old (all caps) spelling as a hint.
dmckee
+1  A: 

You should use the pair<> utility template. Reference here.

McWafflestix
And loose all sense of meaning? Unless it's for a _very_ temporary hack, yes. Otherwise - thank you, no.
xtofl
You wouldn't want to lose all sense of meaning; compounding that with your lack of spelling could be fatal!
McWafflestix
+2  A: 

You could have a vector of structs/classes, where each one has both a name and an age. When sorting, use a custom comparator that only looks at the name field.

Alternately, build an additional vector of integers [0,names.size()-1]. Sort that, with a custom comparator that instead of comparing a < b compares names[a] < names[b]. After sorting, the integer vector will give you the permutation that you can apply to both the names and ages vectors.

Jesse Hall
+5  A: 

In addition to the solution posted by jeje and luke, you can also insert the pairs into a map (or multimap, in case duplicate names are allowed).

assert(names.size() == ages.size());

map<string, double> people;
for (size_t i = 0; i < names.size(); ++i)
    people[names[i]] = ages[i];

// The sequence [people.begin(), people.end()) is now sorted

Note that using vector<person> will be faster if you fill it up only once in advance. map will be faster if you decide to add/remove people dynamically.

avakar
A: 

G'day,

Given how you're trying to model this, my gut feeling is that you haven't approached the problem from an OO perspective. Try using a class instead of a struct.

struct's are soooo K&R! (-:

Think of a Person as an object and they have attributes that are tightly coupled, e.g. Name and Age. Maybe even address, email, Twitter, weight, height, etc.

Then add to your objects the functions that are meaningful, e.g. comparing ages, weights, etc. Writing a < operator for email addresses or Twitter id's is a bit bizarre though.

OOA is just looking at what attributes your "objects" have in real life and that gives you a good starting point for designing your objects.

To get a better idea of OOA have a look at the excellent book "Object Oriented Systems Analysis: Modeling the World in Data" by Sally Shlaer and Stephen Mellor (sanitised Amazon link). Don't faint at the Amazon price though $83.33 indeed! At least it's $0.01 second hand... (-:

HTH

cheers,

Rob Wells