Sort Method

Hi guys, is there a pre-defined sort method I can use in C++ instead of writing my own. All I want for now is to sort a list of ints.

Example...

int list[] = {5,3,15,12,5};
sort(list, 5)

Then it prints out 3,5,5,12,15.

I heard there was one in #include<algorithm> but when I write it, it gives me error. Thanks guys.
std::sort works with iterators, you need to call it like this:
sort(list, list+5)
http://www.cplusplus.com/reference/algorithm/sort/
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>

using namespace std;

int main() 
{
    int a[7] = {23, 1, 33, -20, 6, 6, 9};
    
    sort(a, a+7);
    
    for (int i=0; i<7; i++) 
    {
        cout << a[i] << " ";
    }
    
    return 0;
}
Also, std::multiset is a container that will give you this ordering automatically, without the need for an explicit
call to std::sort.

1
2
3
4
5
6
7
8
9
std::multiset<int> s;
s.insert( 5 );
s.insert( 3 );
s.insert( 15 );
s.insert( 12 );
s.insert( 5 );

// This just outputs the container:
std::for_each( s.begin(), s.end(), std::cout << boost::lambda::_1 << ' ' );


will output

3 5 5 12 15
Topic archived. No new replies allowed.