views:

290

answers:

4

Hi,

Does STL and Vector provide the default sorting option?.

+6  A: 

You probably want std::sort.

#include <algorithm>
#include <vector>

int
main()
{
    std::vector<int> foo;

    std::sort( foo.begin(), foo.end() );

    return 0;
}

a similar example using two boost libraries is below.

#include <boost/assign/list_of.hpp>

#include <boost/foreach.hpp>

#include <algorithm>
#include <iostream>
#include <vector>

int
main()
{
    std::vector<int> foo = boost::assign::list_of(1)(4)(5)(10)(3)(2);

    std::cout << "unsorted" << std::endl;
    BOOST_FOREACH( const int i, foo ) {
        std::cout << i << std::endl;
    }

    std::sort( foo.begin(), foo.end() );

    std::cout << "sorted" << std::endl;
    BOOST_FOREACH( const int i, foo ) {
        std::cout << i << std::endl;
    }

    return 0;
}
Sam Miller
I think your code example would be better if you stuck to the basics. Requiring the reader to be familiar with two different Boost libraries just to understand a `std::sort` example might be a bit much to ask.
jalf
For simple case, like sorting using boost is most likely not justified.
Wojtek
I think you are over reacting - the two Boost constructs used here are extremely easy to understand, even for a complete beginner to Boost (which I am). Their use was not necessary - in fact simply having the `std::sort( beginIndex, endIndex )` line would have been enough - but they hardly "require the reader to be familiar with two Boost libraries".
jhominal
@jhominal: I'm not overreacting, I'm just saying the example is less clear than it could have been. I didn't say it is unreadable as it is now, just suggesting an improvement. :)
jalf
list_of and foreach are just scratching the surface of boost, you need not be familiar with the entire library to understand what they accomplish. A C++ programmer completely unfamiliar with boost should be able to easily grasp the concepts presented in my example.Unilaterally declaring any use of boost as unnecessary in such a trivial example is why some beginners shy away from using it. Yes it can be intimidating, but the example here shows otherwise.
Sam Miller
@samm: when I look for an example of how to do something, I prefer to see a *minimal* example: something with as little noise as possible, which makes no assumptions about the surrounding code. Yes, I can easily read your code, and I am familiar with both the libs you use. But there is a lot more code for me to read through and mentally parse in order to see how much of this is actually *necessary*. If you want more people to use Boost (which is a good thing), I don't think turning what could be a one-liner into 18 lines of code is the right way to do it.
jalf
I've added an example without using boost.
Sam Miller
+11  A: 

Yes, there is sort() in stl algorithms. You should look at http://www.cplusplus.com/reference/algorithm/sort/

Mewp
i can sort ascending as well as descending right?
Shadow
@Shadow: Sure. For example: `std::sort(vec.begin(),vec.end(),std::greater<int>());`, see `<algorithm>` and `<functional>`
sellibitze
oh, thanks lot..
Shadow
+23  A: 

The vector class doesn't have a sort function.

But there is a sort which works on all iterator ranges. And vector does expose iterators.

To sort a vector vec:

#include <algorithm>

std::sort(vec.begin(), vec.end());
jalf
This is what you'll want to do, Boost is absolutely NOT necessary for this whatsoever.
rubenvb
Not all containers - only those with random access. `list` has a member function `sort`, ordered associative containers are already sorted, and unordered associative containers can't be sorted.
Mike Seymour
For completeness, you could mention that it is possible to specify _how_ it should be sorted as well.
Mattias Nilsson
@Mike: true, of course. I edited my answer. I just didn't want to go into too much detail about iterators since the OP might not be familiar with them.
jalf
@Mattias: true, but the OP has said nothing to indicate that he needs this. If he does, I'll edit it in.
jalf
Thanks.. lot...
Shadow
+1  A: 

As other's have mentioned in their answers, there is std::sort function.

However, by "the default sorting option?." do you mean you want to sort a vector where T is a class you have defined. In that case, you have to implement "operator<" in your class.

For example,

class Foo 
{
    public : 
      Foo();
      ~Foo();
    private :
        int c;
};

std::vector<Foo> foovec;
std::sort(foovec.begin(), foovec.end());

To make sure that above "std::sort" line works, you need to define "operator<" in Foo.

 class Foo 
 {
     public : 
       Foo();
       ~Foo();
       bool operator<(const Foo& rFoo) const;
     private :
       int c;
 };

bool Foo::operator<(const Foo& rFoo) const
{
    return(c < rFoo.c);
}
Nitin Bhide