sorting string array

Sorry I know there's lots of topics on this, i did internal search and was flooded, with hundred of pages. But my question will be brief and at least in my mind specific as I could not find the answer in the 20 odd posts I read after search.

On the page: http://www.cplusplus.com/reference/algorithm/sort/
it uses vectors, which I still have done little reading on yet. We haven't covered in class. the example uses:
sort (myvector.begin(), myvector.end(), myobject);

so an alternate to this using arrays would be:
1
2
3
4
const int arraySize=1000;
string myArray[];
fillArray(myArray);    // fills array with input file, 1 line = 1 string
sort (myArray[0], myArray[arraySize], myArray);

is this correct?

Thanks, g
Last edited on
sort (myArray[0], myArray[arraySize], myArray)
Change subscripting with +, the last argument should be a comparison function
sort (myArray, myArray+arraySize, SomeConversionFunction);
If it is an array of std::strings, you can omit the last argument and the default < operator would be used
Change subscripting with +, the last argument should be a comparison function


isn't that only in case you want to sort a small selection of the array for example the first 10 elements?
 
sort (myArray, myArray+10, SomeConversionFunction);

I want to sort the whole array.
Last edited on
I used
1
2
#inlcude <algorithm> 
using std::sort;
and both methods didn't work
EDIT: with +
49: error: no match for 'operator+' in 'sSArray[100] + StringSort::arraySize'

and just using sSArray[arraySize] gave me about 50 errors, about cyg_utils that didn't make much sense
Last edited on
If you want to sort the whole array, you'll need to add the array size.
What error are you getting?
using sort(sSArray[0],sSArray+arraySize);

(yea im using std::string)

49: error: no matching function for call to 'sort(std::string&, std::string*)'

using sort(sSArray[0],sSArray+arraySize,<);
error: expected primary-expression before <'
Last edited on
I told you to remove subscripting
sort(sSArray,sSArray+arraySize);

sSArray[x] is an object of std::string
sSArray+x is a pointer to std::string
woops sorry, missread... :D thank you very much
Topic archived. No new replies allowed.