C-style arrays decay into pointers when they are passed to functions.
The traditional way to deal with this problem is to pass the the array dimension as one of the argument of the function.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <string>
void printArray(std::string * myarr, unsigned size);
int main()
{
std::string an_arr[] { "Sheet", "Duvet", "Pillow" };
unsigned arr_size = sizeof(an_arr) / sizeof(an_arr[0]);
printArray(an_arr, arr_size);
}
void printArray(std::string * myarr, unsigned size)
{
for(unsigned i {}; i < size; ++i) {
std::cout << myarr[i] << ' ';
}
std::cout << '\n';
}
|
To use
std::sort() on your array, now become a pointer, you don’t need iterators (it’s already a pointer!):
E.g.:
std::sort(myarr, myarr + size);
The problem is there are several errors in your code. For example,
std::sort() shouldn’t be inside a for loop, i.e. this block of code is conceptually wrong:
1 2 3 4 5 6 7 8
|
for(int i = 0; i<count; i++)
{
cout << word[i] << " " ; // This prints the file as it is read.
sort(begin(word), end(word)); // problem here with printing alphbtcly
for(auto& s: word) // Do I need to take this section out of the
cout << s << ' '; // same loop as the regular print loop?
}
|
(You want your array to be sorted just once, don’t you?)
And you can’t use range-for with pointers, too, so the last two rows in the above code won’t compile.
Please, let us know if that’s an assignment: I can’t give complete answers to assignments.