Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort one vector using another in nonincreasing order

Tags:

c++

c++11

stl

c++14

This mean that while I am sorting the v2 in the nonincreasing order, the v1 should looks like this:

Vectors look as following.

v1  = {0, 5, 5, 2,  10};
v2  = {0 ,2, 6, 20, 5};

The output:

v1 = {2,  5, 10, 5, 0};
v2 = {20, 6,  5, 2, 0};

I was trying to solve that problem mixing std::sort and lambdas. That is why I have read several questions about std::sort, there were no answers which solve the problem similar to mine so that is why I am asking.

I am especially interested in the answers which contains it's usage or other features of C++11 and C++14.

It is not a question like:

"I completely do not know what to do."

I know how to achieve the output using C++98, but I am wondering if there is an more efficient and prettier way to do it.

Big thanks for your help :)

like image 919
FieryCod Avatar asked May 03 '16 22:05

FieryCod


People also ask

How do you sort a vector according to another vector?

If you cannot merge the data into a vector of pairs or struct with both, you could create a vector of iterators, or the indexes from 0 to size-1. Then sort this using a custom comparator. Finally, create a new vector, populating it using the iterators or indexes.

In what order do you sort vectors?

Sorting a Vector in C++ in Ascending order A vector in C++ can be easily sorted in ascending order using the sort() function defined in the algorithm header file. The sort() function sorts a given data structure and does not return anything. The sorting takes place between the two passed iterators or positions.

How do you sort two vectors simultaneously?

For example to sort two vectors i would use descending bubble sort method and vector pairs. For descending bubble sort, i would create a function that requires a vector pair. After that i would put your 2 vector values into one vector pair.


1 Answers

You could zip, sort, and unzip.

#include <iostream>
#include <vector>
#include <algorithm>

//converts two vectors into vector of pairs
template <typename T, typename U>
auto zip(T t, U u) {
  std::vector<std::pair<typename T::value_type,typename U::value_type>> pairs;
  for (size_t i = 0; i < t.size(); ++i){
    pairs.emplace_back(u[i],t[i]);
  }
  return pairs;
}

//converts a vector of pairs, back into two two vectors
template <typename T, typename U, typename V>
void unzip(V pairs, T & t, U & u) {
  for (auto const& it: pairs){
    u.emplace_back(it.first);
    t.emplace_back(it.second);
  }
}


int main(){

  //vectors
  std::vector<int> v1  = {0, 5, 5, 2,  10};
  std::vector<int> v2  = {0 ,2, 6, 20, 5};

  //zip vectors
  auto pairs = zip(v1,v2);

  //sort them
  std::sort(pairs.begin(),pairs.end(),std::greater<>());

  //unzip them
  v1.clear();
  v2.clear();
  unzip(pairs,v1,v2);

  //print
  std::cout << '\n';
  for (auto i: v1) std::cout << i << ' ';
  std::cout << '\n';
  for (auto i: v2) std::cout << i << ' ';
  std::cout << '\n';

} 
like image 173
Trevor Hickey Avatar answered Sep 20 '22 00:09

Trevor Hickey