sorting using algorithm

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
shiggy
New poster
Posts: 3
Joined: Sat Jan 19, 2008 5:28 am

sorting using algorithm

Post by shiggy »

Is this the correct way to use sort?

Code: Select all

//include files (iostream, algorithm, vector, etc.)
class C
{
   //...stuff
   bool operator < (const C& r) const
   {
       if (some_attribute < r.some_attribute)
          return true;
       else
          return false;
   }
}

int main()
{
  ///
  vector<C> units;
  load_units(units);
  sort(units.begin(), units.end());
}
I ask this becuase many times I will implement a program using sort() and get a WA, but when I use a sorting method of my own (bubble sort), the program gets accepted.

Any ideas?
Thanks.

maxdiver
Learning poster
Posts: 51
Joined: Tue Sep 04, 2007 2:12 pm
Location: Russia, Saratov
Contact:

Post by maxdiver »

Correct.
I use standard sort() always, and I never had any problems.

Post Reply

Return to “C++”