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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:12:46+00:00 2026-05-28T06:12:46+00:00

There is a list of items and attribues. struct Item { double a, b;

  • 0

There is a list of items and attribues.

struct Item { double a, b; Item (double a_, double b_): a(a_), b(b_){}};

typedef std::vector <Item> TItems;
typedef std::vector <double> TAttributes;

I am trying to sort items by attributes using a list of pairs:

int main(int argc, char* argv[])
{
    TItems items;
    items.push_back(Item (1.0, 2.0 ));
    items.push_back(Item (3.0, 4.0 ));
    items.push_back(Item (5.0, 6.0 ));

    TAttributes attributes;
    attributes.push_back(8);
    attributes.push_back(7);
    attributes.push_back(9);

    std::pair <TAttributes, TItems> pairs;
    //No element has been coppied
    std::copy (pairs.first.begin(), pairs.first.end(), std::back_inserter (attributes));
     //No element has been coppied
    std::copy (pairs.second.begin(), pairs.second.end(), std::back_inserter (items)); 

    std::sort (pairs.first.begin(), pairs.first.end());
}

There are 2 problems:

A] unsuitable implementation of copying (no ellement has been coppied)

B] too “screwed” code.

Is there any more simple way how to sort list of items using another list of attributes?

How to implement copy operation correctly?

  • 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-28T06:12:46+00:00Added an answer on May 28, 2026 at 6:12 am

    Indeed you are not putting anything in pairs.
    See constructor of std::pair.

    std::pair <TAttributes, TItems> pairs(attributes_, items_);
    

    Also if you want to reorder Items based on attribute value, you probably want to call std::sort on a list of std::pair<double, Items> (not a pair of list) and provide the appropriate comparison function.

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

Sidebar

Related Questions

I have a list which is dynamically built, but there are empty list items
Is there any way to dequeue two or more items in list comprehension. I.e.
Is there any way to select items in a list that aren't contained in
In Python, is there a way to check for duplicate items in a list,
Is there a way to get all versions of list item(s) using a CAML
help me please. There is a list of nodes. <list> <item>1</item> <item>2</item> <item>3</item> <item>4</item>
I have a list, and each item is linked, is there a way I
There is a declaration of template class with implicit parameters: List.h template <typename Item,
I'm trying to show a list with shopping cart items in a row with
I'm lazy. There! That's out of the way. I have a list of items

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.