Sort List Using Stl Sort Function

Sort list using STL sort function

The standard algorithm std::sort requires random access iterators, which std::list<>::iterators are not (list iterators are bidirectional iterators).

You should use the std::list<>::sort member function.

sorting std::lists using std::sort

You can't use std::sort to sort std::list, because std::sort requires iterators to be random access, and std::list iterators are only bidirectional.

However, std::list has a member function sort that will sort it:

list.sort();
// if you want to use a comparator different from the default one:
// list.sort(comparator);

sort linked list in C++ by using std::sort

You can't, because std::sort requires random access iterators, and std::list provides only bidirectional iterators. Use std::list::sort instead.

using sort() in STL to sort an array

As (like in C) array can be casted to pointer to the first element (but please, do not confuse array with pointer) you can use pointers to determine begin and end, so you write:

sort(strarr, strarr + len, compare);

or if you use C++11 (or Boost) you can use array class:

template<std::size_t N>
int sortStrarr(std:array<string, N>& strarr, int len){
sort(strarr.begin(), strarr.end(), compare);
}

when I use the c++ STL sort function in string sort,there is some problem

Your usage of std::sort is C-ish. You have to supply iterators to begin/end, i.e., write something like

sort(elem.begin(), elem.end());

Besides, your program will crash if the test.txt file is more than 4 lines (trying to read into non-existent elem[4]).

Be careful though, check that the default comparison does what you want. Here it does.

How do you sort a STL list without mutating the original?

I want to do it with a function that is passed by value so that changes will be temporary.

Sure you can do that:

#include <iostream> 
#include <list>
using namespace std;

void foo(list<int> l) { // pass by copy !
l.sort();
// printing the list after sort
for (auto it = l.begin(); it != l.end(); ++it)
cout << ' ' << *it;
}


int main()
{
list<int> mylist{ 1, 5, 3, 2, 4 };

// sort
foo(mylist);


return 0;
}

Sorting a list of a custom type

You can specify a custom sort predicate. In C++11 this is best done with a lambda:

typedef std::pair<int, int> ipair;
std::list<ipair> thelist;

thelist.sort([](const ipair & a, const ipair & b) { return a.first < b.first; });

In older versions of C++ you have to write an appropriate function:

bool compFirst(const ipair & a, const ipair & b) { return a.first < b.first; }

thelist.sort(compFirst);

(Instead if ipair you can of course have your own data structure; just modify the comparison function accordingly to access the relevant data member.)

Finally, if this makes sense, you can also equip your custom class with an operator<. That allows you to use the class freely in any ordered context, but be sure to understand the consequences of that.

Sorting an std::list that contains structs

You should give a look to std::sort. (https://en.cppreference.com/w/cpp/algorithm/sort) There is multiple definitions of that function, and one where you can specify on what you want to sort.

Also, give a look to that post, I think it's what you need : https://stackoverflow.com/a/21234017/6663947

Edit :

thats an exemple of comparator :

sList.sort([](const student & a, const student & b) { return a.id < b.id; });

I did not tried it, but it should look like it. Also, this is for c++11

Hope it helps!



Related Topics



Leave a reply



Submit