Answers for "c++ stl sort"

C++
12

stl for sorting IN C++

// STL IN C++ FOR SORING
#include <bits/stdc++.h> 
#include <iostream> 
using namespace std; 
int main() 
{ 
    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    sort(arr, arr+n);  // ASCENDING SORT
    reverse(arr,arr+n);   //REVERESE ARRAY 
    sort(arr, arr + n, greater<int>());// DESCENDING SORT
  }
Posted by: Guest on October-18-2020
2

sort c++

sort(arr, arr+length); //increase
sort(arr, arr+length, greater<int>()); //decrease
Posted by: Guest on April-01-2021
5

c++ sort

int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
    int n = sizeof(arr)/sizeof(int);  
    sort(arr,arr+n);

    for(int i: arr)
    {
        cout << i << " ";
    }
Posted by: Guest on December-09-2019
5

stl sort in c++

sort(arr, arr+n); // sorts in ascending order
Posted by: Guest on June-01-2020
0

c++ sort

std::vector s = {5, 1, 3, 6, 2,};
std::sort(s.begin(), s.end());
Posted by: Guest on February-20-2021
0

c++ stl sort

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
Posted by: Guest on January-10-2021

Browse Popular Code Answers by Language