HiruneDiver
36
Q:

sort vector struct c++

struct data{
    string word;
    int number;
};


bool my_cmp(const data& a, const data& b)
{
    // smallest comes first
    return a.number < b.number;
}

std::sort(A.begin(), A.end(), my_cmp);
2
// C++ program to sort a vector in non-decreasing 
// order. 
#include <bits/stdc++.h> // Vector 
#include <algorithm>  // Sort
using namespace std; 
  
int main() 
{ 
// Initalizing the vector v with these values
    vector<int> v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; 
// Vector is sorted in ascending order   
    sort(v.begin(), v.end()); 
  
    return 0; 
} 
11
sort(v.begin(), v.end());
6
// C++ program to sort a vector in non-decreasing 
// order. 
#include <algorithm> //Sort
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    vector<int> v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; 
  
    sort(v.begin(), v.end()); 
  
    return 0; 
} 
3
sort(v.begin(), v.end(), greater<int>()); 
0
// C++ program to demonstrate sorting in vector 
// of pair according to 2nd element of pair 
#include<bits/stdc++.h> 
using namespace std; 
  
// Driver function to sort the vector elements 
// by second element of pairs 
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second < b.second); 
} 
  
int main() 
{ 
    // declaring vector of pairs 
    vector< pair <int, int> > vect; 
  
    // Initialising 1st and 2nd element of pairs 
    // with array values 
    int arr[] = {10, 20, 5, 40 }; 
    int arr1[] = {30, 60, 20, 50}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    // Entering values in vector of pairs 
    for (int i=0; i<n; i++) 
        vect.push_back( make_pair(arr[i],arr1[i]) ); 
  
    // Printing the original vector(before sort()) 
    cout << "The vector before sort operation is:\n" ; 
    for (int i=0; i<n; i++) 
    { 
        // "first" and "second" are used to access 
        // 1st and 2nd element of pair respectively 
        cout << vect[i].first << " "
             << vect[i].second << endl; 
  
    } 
  
    // Using sort() function to sort by 2nd element 
    // of pair 
    sort(vect.begin(), vect.end(), sortbysec); 
  
    // Printing the sorted vector(after using sort()) 
    cout << "The vector after sort operation is:\n" ; 
    for (int i=0; i<n; i++) 
    { 
        // "first" and "second" are used to access 
        // 1st and 2nd element of pair respectively 
        cout << vect[i].first << " "
             << vect[i].second << endl; 
    } 
    return 0; 
} 
3
sort(a.begin(), a.end());
0
// C++ program to demonstrate sorting in 
// vector of pair according to 1st element 
// of pair 
#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Declaring vector of pairs 
    vector< pair <int,int> > vect; 
  
    // Initializing 1st and 2nd element of 
    // pairs with array values 
    int arr[] = {10, 20, 5, 40 }; 
    int arr1[] = {30, 60, 20, 50}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    // Entering values in vector of pairs 
    for (int i=0; i<n; i++) 
        vect.push_back( make_pair(arr[i],arr1[i]) ); 
  
    // Printing the original vector(before sort()) 
    cout << "The vector before sort operation is:\n" ; 
    for (int i=0; i<n; i++) 
    { 
        // "first" and "second" are used to access 
        // 1st and 2nd element of pair respectively 
        cout << vect[i].first << " "
             << vect[i].second << endl; 
  
    } 
  
    // Using simple sort() function to sort 
    sort(vect.begin(), vect.end()); 
  
     // Printing the sorted vector(after using sort()) 
    cout << "The vector after sort operation is:\n" ; 
    for (int i=0; i<n; i++) 
    { 
        // "first" and "second" are used to access 
        // 1st and 2nd element of pair respectively 
        cout << vect[i].first << " "
             << vect[i].second << endl; 
    } 
  
    return 0; 
} 
0

struct data{
    string word;
    int number;
};


bool my_cmp(const data& a, const data& b)
{
    // smallest comes first
    return a.number() < b.number();
}

std::sort(A.begin(), A.end(), my_cmp);
0

New to Communities?

Join the community