0

for example I have a string data with the following contents

std :: string words {"armando dippet"};

if using

words.begin () / end () then it works,

but if

std :: string words [] {"gandalf", '"harry", "malfoy", "ron"}

using

std::sort(words.begin () / end ())

does not work, but must be changed to

std :: sort (std :: begin (words), std :: end (words))

or

std :: sort (words, words + sizeof ( words) / sizeof (words [0]))

in order to run, then I switched to dynamic strings

std :: string * words {new std :: string [2] {"hermione", "aziz"}}

using

std::sort(std :: begin (words) / end (words))

didn't work, using

std::sort(words-> begin () words -> end ())

is successful but the result is not suitable, using

std :: sort (words, words + sizeof (words) / sizeof (words [0]))

is successful

To be honest, I'm confused about the use of sort

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Sam Smith
  • 11
  • 3
  • 1
    It works when you give it proper iterators and doesn't when you don't. Where's the confusion? – Retired Ninja Dec 03 '20 at 10:10
  • 7
    Your problems are not really about `std::sort`. You don't seem to understand arrays and memory allocation, so you are calling the wrong combinations of functions for different things. You could look at the SO [book list](https://stackoverflow.com/q/388242/1171191) or find some [online tutorials](https://www.learncpp.com/cpp-tutorial/61-arrays-part-i/). – BoBTFish Dec 03 '20 at 10:11
  • I don't think your final example does what you think it does. On most platforms `sizeof (std::string *) / sizeof (std::string)` results in 0 – Caleth Dec 03 '20 at 10:17
  • 1
    what do you actaully expect `words.begin () / end ()` to mean? There is some similarity to `sizeof (words) / sizeof (words [0])` (both have a `/`) but it isnt really clear what you think it does – 463035818_is_not_an_ai Dec 03 '20 at 10:20
  • Please format your code so it's all colorful like in the answers. We are not used to read code that is just 2 colors. – nada Dec 03 '20 at 10:42

2 Answers2

2

Going backwards

std::string * dynamic_length {new std :: string [2] {"hermione", "aziz"}};

Pointers are not arrays. They don't have a length, so you will have to record that elsewhere.

std::sort(dynamic_length, dynamic_length + 2);

Arrays lack methods. They are only an contiguous sequence of values. The array type holds the length.

std::string static_length[] {"gandalf", '"harry", "malfoy", "ron"}; // declares a std::string[4]

There is an implicit conversion from array to pointer.

std::sort(static_length, static_length + 4);

std has a pair of function templates that operate on arrays (and have other overloads)

std::sort(std::begin(static_length), std::end(static_length));

begin simply does the array to pointer conversion. end additionally adds the length, taken from the type.

template <typename T, size_t N>
T * end(T(&arr)[N]) // this is the syntax for passing an array reference
{ return arr + N; }

What you should be using is an actual container, such as std::vector<std::string>

std::vector<std::string> words {"gandalf", '"harry", "malfoy", "ron"};

This has members begin and end, overloads for the free function begin and end, and others

std::sort(words.begin(), words.end()); // member
std::sort(std::begin(words), std::end(words)); // free function
std::sort(words.data(), words.data() + words.size()); // vector converts to pointer too
Caleth
  • 52,200
  • 2
  • 44
  • 75
1
std :: string * words {new std :: string [2] {"hermione", "aziz"}}
std :: sort (words, words + sizeof (words) / sizeof (words [0]))

is successful

It is "successful" in the sense that the types of the arguments are iterators, and therefore well-formed.

But the pointer arithmetic on your end pointer is wrong and depending on the size of std::string it could go beyond the array resulting in undefined behaviour (in general case; not necessarily in this one), or it could result to the beginning and nothing is sorted, or anything in between. In short, the calculation is no related to the length of the array in any way.

Here is a correct version:

std::sort(words, words + 2);

P.S. Avoid using owning bare pointers.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • here's my resource https://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/comment-page-9/#comment-484071, your solution std::sort(words, words + 2) is very enlightening – Sam Smith Dec 03 '20 at 14:45