passing std::vector as a const reference to std::sort problem

Hi,

In my following code, I am passing a std::vector as a const reference to std::sort function but it shows compilation errors. It is only an example code. In my real code, I need to pass template to the vector (i.e. vector<T>). Therefore, I needed to write a function "sortt()".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sortt(const vector<int>& vect)
{
    vector<int>::const_iterator it; 
    sort(it = vect.begin(); it != vect.end(); it++);
//  for(it = vect.begin(); it != vect.end(); it++) cout << *it << endl;
}

int main()
{
    vector<int> vect;
    for(int i=0; i<10; i++) vect.push_back(rand());
    
    sortt(vect);
    for(int i=0; i<vect.size(); i++) cout << vect.at(i) << endl;
}


Errors:
1
2
3
4
5
6
tt.cxx: In function ‘void sortt(const std::vector<int>&)’:
tt.cxx:9:31: error: expected ‘)’ before ‘;’ token
         sort(it = vect.begin(); it != vect.end(); it++);
                               ^
tt.cxx:9:55: error: expected ‘;’ before ‘)’ token
         sort(it = vect.begin(); it != vect.end(); it++);


Any thought? Thanks.
Last edited on
The arguments to std::sort should be iterators to the beginning and end of the range that you want to sort. To sort vect all you need to do is sort(vect.begin(), vect.end());
I wanted to pass the vect as a const parameter to the function "sortt()". That is reason I used const_iterator.
If it's const, that means you should not modify it, so how could you sort it?
Ahh! I see, thanks a lot.
Topic archived. No new replies allowed.