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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:08+00:00 2026-06-11T18:44:08+00:00

Suppose I have the following snippets: int compareFoo(std::string** a, std::string** b) { return (**a).compare(**b);

  • 0

Suppose I have the following snippets:

int compareFoo(std::string** a, std::string** b) {
    return (**a).compare(**b);
}

int main() {
    std::string** foo = new std::string*[3];
    foo[0] = new std::string("a");
    foo[1] = new std::string("c");
    foo[2] = new std::string("b");
    sort(foo,foo+3,compareFoo);
    for (int i = 0; i < 3; i++) {
        std::cout << *foo[i] << std::endl; // print acb
    }
}

If I’d left out the third parameter(compare) for sort, it’d have given me the sorted 3 strings in terms of their memory addresses, that’s not how I intended it. But how do I parameterize the compareFoo function so that it won’t compare the memory addresses.

void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

The description of Sort on cplusplus.com is quite vague and the given example is simple. Since it takes an Iterator, does it mean that i only work with stand containers? Thank you

  • 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-06-11T18:44:09+00:00Added an answer on June 11, 2026 at 6:44 pm

    The comparison function takes two items to compare and returns true if the first one is less than the second one. In your case, it would work like this:

    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using std::sort;
    
    bool compareFoo(std::string* a,std::string* b){
      return *a < *b;
    }
    
    int main(){
      std::string** foo = new std::string*[3];
      foo[0] = new std::string("a");
      foo[1] = new std::string("c");
      foo[2] = new std::string("b");
      sort(foo,foo+3,compareFoo);
      for(int i=0;i<3;i++){
        std::cout << *foo[i] << std::endl; 
      }
    
      // Remember to delete things that you dynamically allocate.
      delete foo[0];
      delete foo[1];
      delete foo[2];
      delete [] foo;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following code snippets, i.e. goo call foo, DataTable created by
suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following method signature int f (int[] values) and I call
Suppose I have the following senario: Trunk is my main development line where programmers
Suppose I have following string: String asd = this is test ass this is
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following js code: r='d|daa|dd'; reg = /\|.*/; result = reg.exec(r);
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3

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.