Do I need to sort my vector if the items were read in from a file sequentially that was already sorted? I don’t want to incur the performance penalty if it’s unnecessary. For reference, this is what I’m using to build it, and each item is in order as it’s being read in.
Edited after @dribeas’ comment
std::vector<int> split(const std::string &s, char delim) {
std::vector<int> elems;
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(static_cast<int>(atoi(item.c_str())));
}
return elems;
}
No, you don’t need to sort the vector.
push_backadds elements to the end explicitly.Note that your API is taking in
elemsand also returning it (by reference). What happens to any content the vector already contains?