Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 64953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:52:14+00:00 2026-05-10T18:52:14+00:00

I have several std::vector , all of the same length. I want to sort

  • 0

I have several std::vector, all of the same length. I want to sort one of these vectors, and apply the same transformation to all of the other vectors. Is there a neat way of doing this? (preferably using the STL or Boost)? Some of the vectors hold ints and some of them std::strings.

Pseudo code:

std::vector<int> Index = { 3, 1, 2 }; std::vector<std::string> Values = { 'Third', 'First', 'Second' };  Transformation = sort(Index); Index is now { 1, 2, 3};  ... magic happens as Transformation is applied to Values ... Values are now { 'First', 'Second', 'Third' }; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:52:14+00:00Added an answer on May 10, 2026 at 6:52 pm

    friol’s approach is good when coupled with yours. First, build a vector consisting of the numbers 1…n, along with the elements from the vector dictating the sorting order:

    typedef vector<int>::const_iterator myiter;  vector<pair<size_t, myiter> > order(Index.size());  size_t n = 0; for (myiter it = Index.begin(); it != Index.end(); ++it, ++n)     order[n] = make_pair(n, it); 

    Now you can sort this array using a custom sorter:

    struct ordering {     bool operator ()(pair<size_t, myiter> const& a, pair<size_t, myiter> const& b) {         return *(a.second) < *(b.second);     } };  sort(order.begin(), order.end(), ordering()); 

    Now you’ve captured the order of rearrangement inside order (more precisely, in the first component of the items). You can now use this ordering to sort your other vectors. There’s probably a very clever in-place variant running in the same time, but until someone else comes up with it, here’s one variant that isn’t in-place. It uses order as a look-up table for the new index of each element.

    template <typename T> vector<T> sort_from_ref(     vector<T> const& in,     vector<pair<size_t, myiter> > const& reference ) {     vector<T> ret(in.size());      size_t const size = in.size();     for (size_t i = 0; i < size; ++i)         ret[i] = in[reference[i].first];      return ret; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of Student objects which I want to sort using #include
I have several xml files with different node structure. I want to extract xml
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
We have several offices around the US and one in India. Our IT department
I'm trying to write a class which contains several std::vectors as data members, and
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
I have written a class (AbcdBase) which holds several static objects, these include maps
How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several
I have a std::vector<std::string> that I need to use for a C function's argument
No doubt some of you have seen my recent posting, all regarding the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.