Initializing an arrary of strings ?

I have been studying sorting algorithms for strings.
I have a question about initializing arrays.


With c++ we can initialize and array as follows:
const int arraylength = 20000;
string wordlist[arraylength];


and we can also initialize as follows using (#include <array>):
array<string,arraylength>arraysorted;

then define a return function:
array<string,arraylength> selectSort(array<string,arraylength>list){
...}

What are the advantages of using this syntax?

array<string,arraylength>arraysorted;
It means you don't have to worry about also passing the length of the array. And you can pass the array to std::sort().
then define a return function:
1
2
array<string,arraylength> selectSort(array<string,arraylength>list){
...}

That's inefficient because you make a copy of the array going in, and possibly another copy going out. I think it would be better to sort the array in place:
void selectSort(array<string,arraylength> &list);
If the user wants to sort into a copy, they can make the copy themselves easily enough:
1
2
3
4
array<string,20000> list;
...
array<string,20000> mycopy(list);
selectSort(mycopy);


This way at least the user has the option of being efficient.
Thank you!
This makes sense.
Being a beginner what does &list do?
Is it a pointer ?
void selectSort(array<string,arraylength> &list);

The & is technically part of the type. T& means "reference to T". So it's a reference to array<string, arrayLength> in your code. Passing by reference avoids a copy and allows you to modify the same object that is passed to the function.
http://www.cplusplus.com/doc/tutorial/functions/ has a section about arguments passed by value and by reference.
Thank you Ganada and kesklverto,

This is a good reminder.
This toggled by brain cells back to 89
I did this with Pascal back in 1989.
Pass by value and by reference.
Topic archived. No new replies allowed.