From 23806a9de7c06286fbf66ce0358ae7592cec0a6d Mon Sep 17 00:00:00 2001 From: "Coo.King" <39448470+Pega-Stellar@users.noreply.github.com> Date: Tue, 16 Oct 2018 11:08:37 +0700 Subject: [PATCH] Add sort using lamda function. (#19048) * Update index.md * Update index.md --- .../guide/english/cplusplus/vector/index.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client/src/pages/guide/english/cplusplus/vector/index.md b/client/src/pages/guide/english/cplusplus/vector/index.md index 5ad881cdd2..40959e9f66 100644 --- a/client/src/pages/guide/english/cplusplus/vector/index.md +++ b/client/src/pages/guide/english/cplusplus/vector/index.md @@ -193,6 +193,23 @@ int main(){ return 0; } ``` +In C++11, you can also sort with lambda function, which can be useful. +```cpp11 +#include +using namespace std; + +int main() +{ + vector v {3, 1, 2}; + sort(v.begin(), v.end(), [] (int i, int j) -> bool { + return i < j; + } ); + cout << "Vector Contents Sorted In Ascending Order:\n"; + for (int e : v) + cout << e << " "; + return 0; +} +``` ### Sorting Vector In Descending Order Sorting Vector in descending order can be done with the help of third argument namely greater() in Sort() in C++. ``` cpp @@ -214,3 +231,5 @@ int main(){ return 0; } ``` + +You can also sort in descending using lamda like the one above.