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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 273k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This question might be a duplicate of Areas over multiple… May 13, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer According to your comment to the other answer, you want… May 13, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer Register it like this instead: container.RegisterType<Person>(new InjectionConstructor()); You can add… May 13, 2026 at 2:03 pm

Related Questions

No doubt some of you have seen my recent posting, all regarding the same
I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A
I have a class TContainer that is an aggregate of several stl collections pointers
I`m writing a chat using WinSock2 and WinAPI functions. And I have a little

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.