Write your question here.
Hello
I want to sort an array using algorithm library,I wrote this code but it doesn't work, whats the problem? can anyone help me?!!
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
#include<algorithm>
usingnamespace std;
int main(){
int a[10];
for(int i=0; i<10; i++){
cin>>a[i];
}
sort(a[0], a[9]);
cout<<a[0];
cin.get();
}
std::sort() is expecting iterators (pointers, kind of) to elements but by a[0] and a[9] you're passing the actual elements.
The first iterator should be to the first element.
The second iterator should be to the imaginary element after the last element.
Here are some ideas how to make it work (as also explained in mutexe's link):
1 2 3 4 5 6
sort(&a[0], &a[9] + 1); // [address of first] and [address of last] + 1
sort(a, a + 10); // same as above
#include <iterator>
// ...
sort(begin(a), end(a)); // works in C++11 compilers, and for static arrays