sort a pair using c++ stl
C++
// 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;
}
Also in C++:
- Title
- for loop in c++ hackerrank solution
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- accept the noun and the output of plural c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- what is time complexity of swap function
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- flutter margins
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- c++ create button
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- c++ read_ascii
- Category
- C++
- Title
- c++ cast char to string
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- c++ vector size
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- how to calculate trigonometric values in c++
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- basic cpp programs
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- C++ pointer arithmetic
- Category
- C++
- Title
- c++ string contains
- Category
- C++
- Title
- delete memory c++
- Category
- C++
- Title
- for each c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- convert to lowercase c++
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- arduino for command
- Category
- C++