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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:40:12+00:00 2026-05-30T13:40:12+00:00

In the following situation, how does the parameter-list for sortMyVectors have to look like,

  • 0

In the following situation, how does the parameter-list for sortMyVectors have to look like, and how do I call it from within myFunction ?

std::vector<Vector2> myFunction ( MyObj * myObj ) {

    std::vector<Vector2> myVectors;
    // fill with lots of Vector2(double x, double y) objects

    sortMyVectors( ???-myVectors, ???-myObj);

    return myVectors;
} 


void sortMyVectors( vector<Vector2> &myVectors, const MyObj &myObj) {

    // sort the std::vector using myObj
    // modifies order of Vector2s within the passed-in myVectors
    // does not modify myObj
    // need to be able to access with myVectors[0].x here...
}

The way this is called is myFunction(&(*myObj));, where myObj is a list<MyObj>::iterator. Is there a cleaner way to write 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-30T13:40:13+00:00Added an answer on May 30, 2026 at 1:40 pm
    sortMyVectors( ???-myVectors, ???-myObj);
    

    Answer:

    sortMyVectors(myVectors, *myObj);
    

    As it seems you are having some basic trouble with pointers at the moment (and I suspect this is practice for some kind of homework), it would probably be a bit too advanced to suggest you learn iterators, but you should look into that later along with std::sort which would be quite hard to beat with any handrolled sort.

    As an attempt to improve your understanding of pointers:

    MyObj* myObjPtr = ...;
    

    This is a pointer. It points to a memory address. Assuming it points to a valid one, we can do this:

    MyObj& myObjRef = *myObjPtr;
    

    Now we dereferenced the pointer to get the pointee, and assigned it to a reference which is also like a pointer but hides the pointer/pointee distinction (not requiring operator* to access the pointee/referenced object).

    In response to a comment you made:

    The “sorting” basically sorts these vectors by their distance from
    another vector, but only in a particular case, so I don’t know if
    there is a standard way of doing that..

    std::sort accepts a strict weak ordering predicate (basically a function or function object which returns true/false based on whether element1 < element2). Example:

    bool length_is_less(const string& str1, const string& str2)
    {
        return str1.size() < str2.size();
    }
    
    vector<string> my_strings = ...;
    sort(my_strings.begin(), my_strings.end(), length_is_less);
    

    The above code sorts strings based on their length from shortest string to longest string, rather than the default behavior (default operator< implementation for strings) of sorting them by string order (alphabetical order, crudely speaking).

    Given this ability to define your own predicate for comparing objects, you can basically sort anything by any condition using std::sort.

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

Sidebar

Related Questions

Following situation: I have a JFrame and call JOptionPane.showInputDialog(test) . The modal dialog will
I have the following situation. Im getting data from an external system in the
I've got a following situation: Got a method def f(lst: List[Any]) , which does
I have the following situation: I have a work sheet which contains a list
I have the following situation: 1) 1 X PhotoTableViewController to display a list of
I have the following situation. I have a Java Class that inherits from another
I have following situation: $('selector').live('click', openSite); and open site takes the event like this
I have the following situation where an Exception thrown in a ViewModel does not
I have the following situation: There is a tool that gets an XSLT from
Possible Duplicate: How does Copy-local work? I have following situation: there's a project named

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.