how to sort an array according to another array c++
C++
// Sort an array according to
// other using pair in STL.
#include <bits/stdc++.h>
using namespace std;
// Function to sort character array b[]
// according to the order defined by a[]
void pairsort(int a[], char b[], int n)
{
pair<int, char> pairt[n];
// Storing the respective array
// elements in pairs.
for (int i = 0; i < n; i++)
{
pairt[i].first = a[i];
pairt[i].second = b[i];
}
// Sorting the pair array.
sort(pairt, pairt + n);
// Modifying original arrays
for (int i = 0; i < n; i++)
{
a[i] = pairt[i].first;
b[i] = pairt[i].second;
}
}
// Driver function
int main()
{
int a[] = {2, 1, 5, 4, 9, 3, 6, 7, 10, 8};
char b[] = {'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J'};
int n = sizeof(a) / sizeof(a[0]);
// Function calling
pairsort(a, b, n);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
for (int i = 0; i < n; i++)
cout << b[i] << " ";
return 0;
}
Also in C++:
- Title
- google pdf iframe viwer
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- All palindromic substrings
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- gfg bottom view of tree
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- length of 2d array c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- hashing in competitive programming
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- cheap hosting
- Category
- C++
- Title
- factorial in c++
- Category
- C++
- Title
- C++ while
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- -> cpp
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- sqrt cpp
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- tokenize string c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- c++ write new file
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- c++ round to int
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- how to make a n*n 2d dynamic array in c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++