I have a vector of strings: vectorElements
I’d like to create a vector of *char to point to the beginning of each string. My objective is to be able to traverse through each string, character by character. Ultimately, I’d like to sort the vector of strings.
Note: Strings may contain integer values. In which case, I’ll be sorting based on their numeric value.
I have a vector of strings: vectorElements I’d like to create a vector of
Share
If you are writing in C++, it is better to use C++
stringinstead of the C style array ofchar. You can still iterate through each character with by obtaining the iterator withbegin()and use overloaded operator++on the iterator to traverse to next character (check with iterator returned byend()to know whether you reached the end of the string or not). You can also refer to character in the string in C style with overloaded operator[].Therefore, a
vector<string>may be what you need.To sort the strings, you may want to use
sortfunction inalgorithmheader. Since you are not sorting them lexically all the time, you have to define your own function that compares between 2 strings.Pseudocode for the comparison: