-1

I am trying to pass an array of strings off to a function so that it will sort it along with a Class Template array object of strings which I don't even know what that would look like. But as it is right now. When I use size() inside of main it works just fine but when I use size() inside a function I am typing up it throws out this error.

#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm> // contains sort and binary_search
using namespace std;

void sortArrays(string arr[]);
int main()
{
    array<string, 5> hello = {"Hello", "world", "How", "Are", "You"};

    sortArrays(&hello[0]);
}

void sortArrays(string array[])
{
    string *arrPtr = array;

    sort(*arrPtr.begin(), *arrPtr.end());
    cout << endl << endl;
}
  • 1
    I think you need to pass an `array` or `vector` reference rather than a C-style array of `string`. As it is, there's no way for `sortArrays` to know how many elements are in the array – Tim Randall Sep 01 '21 at 18:14
  • 1
    try syntax : void sortArrays(std::array& arr) – Pepijn Kramer Sep 01 '21 at 18:14
  • 1
    As the function `sortArrays` acknowledges, `string array[]` is a pointer to `string`. It typically points at the first element of a C-style array, which doesn't know anything about `begin()` or `end()`. Also, `*arrPtr.begin()` wouldn't be right; if `arrPtr` pointed at an `array` object, you'd access its members with `(*arrPtr).begin()` or, more typically, with `arrPtr->begin()`. `*arrPtr.begin()` would call `begin()` and dereference the result; if it helps, think of it as `*(arrPtr.begin())`. – Pete Becker Sep 01 '21 at 18:18
  • Will your `sortArrays()` function only sort arrays of 5 items or do you plan to sort different sized arrays? If so you may want to use std::vector – drescherjm Sep 01 '21 at 18:21
  • 2
    [What is Array to Pointer Decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Drew Dormann Sep 01 '21 at 18:24
  • Also to me it seems sortArrays() is not really needed. By adding this function instead of just calling std::sort() you are adding additional code and additional setup. – drescherjm Sep 01 '21 at 18:25
  • 1
    @drescherjm You're not incorrect both on not needing the function and using std::vector. I tried to focus my answer on the c++ syntax for std::arrays. But yes I would have used std::vector in this case too – Pepijn Kramer Sep 01 '21 at 18:46
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 05 '21 at 00:27

1 Answers1

-1

Like this, std::array it's easy to pass by reference.

#include <algorithm> 
#include <array>
#include <iostream>
#include <vector>
#include <string>

void sortArray(std::array<std::string,5>& arr)
{
    std::sort(arr.begin(),arr.end());
}

int main()
{

    std::array<std::string, 5> hello = { "Hello", "world", "How", "Are", "You" };
    sortArray(hello);

    for (const auto& str : hello)
    {
        std::cout << str << " ";
    }
    std::cout << std::endl;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19