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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:39:29+00:00 2026-06-16T00:39:29+00:00

There are examples of sorting vectors or dynamically allocated arrays but I couldn’t find

  • 0

There are examples of sorting vectors or dynamically allocated arrays but I couldn’t find any help regarding static arrays. Let’s say I have an array

int array[10][10];

and a compare function,

bool compare(const int (*a)[10], const int (*b)[10]);

When I call it like this,

std::sort(array, array + 10, compare);

I have compilation errors: error: cannot convert 'int*' to 'const int (*)[10]' in argument passing

I tried many ways, casting array to (void**) in sort function but then I have segmentation fault. My problem is using arrays as function parameters I guess but I couldn’t figure out how to use this std::sort. Otherwise, I will have to write my own sort function.

  • 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-16T00:39:32+00:00Added an answer on June 16, 2026 at 12:39 am

    When std::sort is called on a container of elements of type T, the comparison function needs to receive arguments of type T or const T&. In this case, you have a 2-dimensional array, so the type of elements is a 1-dimensional array int[10]. Since 1-dimensional arrays decay to pointers, compare can be:

    bool compare(int a[10], int b[10]);
    

    or equivalently:

    bool compare(int *a, int *b);
    

    This will fix the error you got, but your code still won’t work: std::sort needs the container elements to be assignable (or movable in C++11), but arrays are not assignable.

    You can use std::vector<std::vector<int> > instead as people have suggested. Note that your fear of performance problems is misguided: Even if sorting a two-dimensional array was possible, it would involve a lot of copying of one-dimensional arrays which would take a long time. Swapping vectors, on the other hand, is done by simply swapping pointers which is faster. In general, you should not make assumptions about performance if you haven’t tested it first.

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

Sidebar

Related Questions

Are there any examples of Store Locators using Google API v3 without using a
Are there any examples of successful commercial apps written with PhoneGap? If I were
Does the asp.net ReportViewer control work with LinqDataSource? If so are there any examples
There are a number of examples out there but yet I can't seem to
Are there any good examples of mvc routing wherein every 404 page not found
I know that there are lots of examples out there on this, but they
There are a lot of examples for sorting some JSON array by some property
I have a question regarding sorting in C#. Lets assume that there is List<Person>
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
Are there examples which show how Delphi invokes the Active Directory Kerberos server to

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.