Answers for "c++ sort() stl character"

C++
9

how to sort a string in c++

#include<bits/stdc++.h>
using namespace std;
int main()
{
	srting str; // First, declare a string.
  	sort(str.begin() , str.end()); // Then sort it by using this method. It is much more convenient.
  	cout << str << endl; // Last of all, print out the string.
}
Posted by: Guest on May-26-2021
14

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

how to sort a string in c++

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  for(int i=0;i<n-1;i++){
    for(int j=0;j<n-i-1;j++){
      if(s[j]>s[j+1]) swap(s[j],s[j+1]);
    }
  }
    cout<<s<<endl;
  return 0;
}
Posted by: Guest on February-26-2022

Browse Popular Code Answers by Language