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 7613079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:04:45+00:00 2026-05-31T02:04:45+00:00

I have two vector objects that contain different types of data that are ordered

  • 0

I have two vector objects that contain different types of data that are ordered in the same way. My situation looks something like this:

struct Info
{
    double opaque_data_not_relevant_to_this_problem[6];
    int data_len;

    bool operator<(const Info &rhs) const
    {
        return (bool) irrelevant_operation_on_opaque_data;
    }
};

vector<Info> vec1;
vector<double> vec2;

For each Info entry in vec1, vec2 contains a sequence of double values, equal in length to the value of data_len in the corresponding element in vec1. For example:

vec1[0].data_len == 100 ==> vec2[0:99] correspond to vec1[0]
vec1[1].data_len == 150 ==> vec2[100:249] correspond to vec1[1]
// and so on

I understand that this arrangement is not very OO and there is probably a “more C++” way to do this. However, other constraints in my environment force me toward this type of data packing, so I need to work around it. Unfortunately, the length of each data record in vec2 (specified by its data_len counterpart in vec1) are not known until runtime, and the length varies from record to record.

My problem: I would like to sort the two vectors by some criteria. Sorting vec1 is simple, as I can just use std::sort. At the same time, however, I need to sort vec2 such that the ordering described above is still maintained (i.e. the first block of values in vec2 corresponds to vec1[0] after it is sorted). It would be nice if I could get some kind of “index vector” out of the sorting process that I could then use to reorder vec2 (in-place or out-of-place operation would be fine), but I’m not sure of a good way to do that with the standard library (if there is one).

I could do the sort by defining a second intermediate structure that bundles the two together:

struct SortableInfo
{
    Info info;
    vector<double> data;

    bool operator<(const SortabelInfo &rhs) const { return info < rhs.info; }
};
vector<SortableInfo> vec3;

I would then populate vec3 appropriately based on the contents of vec1 and vec2, sort it, then fan the data back out to separate vectors again. However, this doesn’t seem particularly efficient. Any suggestions on a better way to execute this?

  • 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. Editorial Team
    Editorial Team
    2026-05-31T02:04:46+00:00Added an answer on May 31, 2026 at 2:04 am

    You could store in your SortableInfo pointers to the start positions in the corresponding vec2

    struct SortableInfo {
        Info info;
        double *start_pos;
    
        bool operator<(const SortabelInfo &rhs) const { return info < rhs.info; }
    }
    

    populate your vec3, sort it and then at the end make an ordered copy of your vec2 using the sorted pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
I have written a bit of code to match two vector of objects that
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I've got a two vectors in class A that contain other class objects B
I have a vector-like class that contains an array of objects of type T
I have the following two Vector objects in java. Vector<SomeClass> obj1; Vector<SomeClass> obj2; The
Say I have two 2D vectors, one for an objects current position and one
I have two applications written in Java that communicate with each other using XML
I have two vector<T> in my program, called active and non_active respectively. This refers
Let's say we have a class that looks like this: class A { public:

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.