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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:49:59+00:00 2026-05-15T23:49:59+00:00

Suppose I have some data stored in a container of unique_ptr s: struct MyData

  • 0

Suppose I have some data stored in a container of unique_ptrs:

struct MyData {
    int id;  // a unique id for this particular instance
    data some_data; // arbitrary additional data
};

// ...

std::vector<std::unique_ptr<MyData>> my_data_vec;

The ordering of my_data_vec is important. Suppose now I have another vector of IDs of MyDatas:

std::vector<int> my_data_ids;

I now want to rearrange my_data_vec such that the elements are in the sequence specified by my_data_ids. (Don’t forget moving a unique_ptr requires move-semantics with std::move().)

What’s the most algorithmically efficient way to achieve this, and do any of the STL algorithms lend themselves well to achieving this? I can’t see that std::sort would be any help.

Edit: I can use O(n) memory space (not too worried about memory), but the IDs are arbitrary (in my specific case they are actually randomly generated).

  • 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-15T23:50:00+00:00Added an answer on May 15, 2026 at 11:50 pm
    1. Create a map that maps ids to their index in my_data_ids.
    2. Create a function object that compares std::unique_ptr<MyData> based on their ID’s index in that map.
    3. Use std::sort to sort the my_data_vec using that function object.

    Here’s a sketch of this:

    // Beware, brain-compiled code ahead!
    typedef std::vector<int> my_data_ids_type;
    typedef std::map<int,my_data_ids_type::size_type> my_data_ids_map_type;
    
    class my_id_comparator : public std::binary_function< bool
                                                        , std::unique_ptr<MyData>
                                                        , std::unique_ptr<MyData> > {
    public:
      my_id_comparator(const my_data_ids_map_type& my_data_ids_map)
        : my_data_ids_map_(my_data_ids_map) {}
    
      bool operator()( const std::unique_ptr<MyData>& lhs
                     , const std::unique_ptr<MyData>& rhs ) const
      {
         my_data_ids_map_type::const_iterator it_lhs = my_data_ids_map_.find(lhs.id);
         my_data_ids_map_type::const_iterator it_rhs = my_data_ids_map_.find(rhs.id);
         if( it_lhs == my_data_ids_map_.end() || it_rhs == my_data_ids_map_.end() )
           throw "dammit!"; // whatever
         return it_lhs->second < it_rhs->second;
      }
    private
      my_data_ids_map_type& my_data_ids_map_;
    };
    
    //...
    
    my_data_ids_map_type my_data_ids_map;
    // ...
    // populate my_data_ids_map with the IDs and their indexes from my_data_ids
    // ...
    std::sort( my_data_vec.begin(), my_data_vec.end(), my_id_comparator(my_data_ids_map) );
    

    If memory is scarce, but time doesn’t matter, you could do away with the map and search the IDs in the my_data_ids vector for each comparison. However, you would have to be really desperate for memory to do that, since two linearly complex operations per comparison are going to be quite expensive.

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

Sidebar

Related Questions

I have some data stored in a database like this: TableName: faults Table: +------------+--------------+
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have some XAML like this: <Window.Resources> <v:MyClass x:Key=whatever Text=foo\nbar /> </Window.Resources> Obviously
Suppose I have some XML like this: <section name=SampleSection> <item name=ScoredItem1> <attributes> <scored data_type=boolean
Suppose I have this database table (some sample code below) that stores the relationship
Suppose I have any data stored in bytes. For example: 0110001100010101100101110101101 How can I
Suppose I have some code that would, in theory, compile against any version of
Suppose we have some named enums: enum MyEnum { FOO, BAR = 0x50 };
Suppose I have some code: let listB = [ 1; 2; 3 ] Using

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.