1

I am trying to sort arrays (yes, not vectors) of strings, following some suggestions from sites such as this one. This is the code I wrote so far, but it always prints out this long error (which has no results on google). How can this be fixed?

#include <algorithm>
#include <cstring>
#include <iostream>
//qsort(names, n, 15, (int (*)(const void *, const void *))strcmp);



int main()
{
    std::cout << "hi";
    char arr[3][6];
    strcpy(arr[0], "hello"), strcpy(arr[1], "hillo"), strcpy(arr[2], "hallo");
    std::sort(arr, arr + 3, [](char const *lhs,
                           char const *rhs) { return strcmp(lhs, rhs) < 0; });
    //qsort(arr, 3, 20, (int (*)(const void *, const void *))strcmp);
    for (int i = 0; i < 3; ++i)
        std::cout << arr[i] << '\n';
}
12
  • 1
    Does this answer your question? C++ String array sorting Commented Sep 27, 2020 at 10:12
  • 1
    Consider the element type of the array you're sorting. Arrays are not assignable, and therefore the stock swap-mechanics for exchanging rows in your array of arrays (each element is, in fact, an array of char) isn't going to work. Commented Sep 27, 2020 at 10:12
  • @WhozCraig what you're trying to say is that I have an array of arrays (strings are arrays) and since arrays are not assignable we can't swap items which is what "sort" does? then how do people on other SO questions manage to do it? Commented Sep 27, 2020 at 10:17
  • Pretty much. Throw out your decl of arr, and the strcpy calls, and replace them with const char *arr[] = { "hello", "hillo", "hallo" }; and try again. Then consider what changed and why it now works. Commented Sep 27, 2020 at 10:18
  • @WhozCraig will using the type "string" work here? (instead of doing a 2d array doing a 1d array with each element having string type) Commented Sep 27, 2020 at 10:18

2 Answers 2

1

This is the relevant part of the error message:

error: array must be initialized with a brace-enclosed initializer

Which is not very clear unfortunately, but ot comes from within the sorting implementation. So, let us revise the preconditions of the algorithm:

std::sort is implemented by swapping elements. As such, it requires that the elements are swappable. The elements of your array are arrays. Arrays are not swappable. Therefore an array of arrays is not sortable.

will using the type "string" work here?

Using std::string will work. It is swappable. You won't even need a custom comparison function because it is even comparable.

what makes arrays unswappable and other types swappable

Swap is inplemented by move initialisation (and assignment). Language rules say that array is not move initialisable (nor is it assignable). Knowing this, see if the error messsge makes some sense.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. what makes arrays unswappable and other types swappable?
Very interesting, I will continue exploring the subject for a deeper understanding. thank you. I will mark your answer as correct and will metion that the comments of @WhozCraig are very explanatory.
0

You cannot sort array of arrays using sort inbuilt function. You can traverse through the array of strings and sort each string(str) using sort(str.begin(), str.end()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.