sorting a vector

Hello,

I am working on sorting a vector. Currently i have a 3d vector created each element consists of a string and 2 other numbers. I was planning on just loading all the strings into a vector, sort them alphabetically , then compare to the 3d to make the 3D vector sorted. Is this the best way or is there an alogorith set in one of the libraries that i could use that takes a 3D vector and let me sort the vecotor alphbetically.
I'm open to any ideas/suggestions

Thanks
If you have:

1
2
3
4
5
struct foo {
   std::string s;
   int x;
   int y;
};


you have a variety of ways of sorting by s. Perhaps the simplest is to write operator<()
for foo(), then just do:

1
2
std::vector<foo> v; // assume it is filled out
std::sort( v.begin(), v.end() );


std::sort will invoke operator< to perform comparisons. Of course your operator<() should
just compare strings.
so this is if i load my string elemnts from 3d into a regular vector (i guess u could say 1D)

Is there any way to sort my 3d vector and just specify what i want to sort it by?

Thanks
u can use sort();
i think...
True, vector<> has a specialized sort() method which would be preferred to std::sort(),
though you still need operator<().
so im using
sort ( people.begin (), people.end());

and its giving me a poop load of errors..any suggestions
Last edited on
You haven't defined an operator < for your person class. (or whatever is in there).
do u think you could explain or link me to a tutorial that expains what and how an "operator < " works

many Thankyous
1
2
3
4
5
struct foo {
    bool operator<( const foo& rhs ) const {
        // return "true" if *this is strictly less than rhs otherwise return false
    }
};
Last edited on
Topic archived. No new replies allowed.