I have a vector of strings, where all of the strings have been converted to upper case so that they can be compared. I need to use for loops to alphabetize my vector. I need to alphabetize the existing vector rather than create a new one. This is my function definition:
void alpha(vector <string>& words){
int minPos;
int i = 0;
for (i = 0; i < words.size(); i++) {
minPos = i;
for (int k = i + 1; k < words.size(); k++) {
if (words.at(i) < words.at(k)) {
minPos = k;
}
}
}
string temp = words.at(minPos);
words.at(minPos) = words.at(i);
words.at(i) = temp;
}
Here is the error that I am currently getting in main.cpp when this function is called:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
I understand that this means that I am probably trying to reach an index that is out of range, but I'm not sure where this would be, or whether I am even on the right track with this to begin with. I would appreciate any guidance.