According to cplusplus.com, the std::set::insert()
overload that takes a hint iterator of where to insert an item changes from C++98 to C++11. In C++98 the hint should be:
The function optimizes its insertion time if position points to the element that will precede the inserted element.
However, in C++11 the hint is changed and should now be:
The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).
But, in both C++98 or C++11, the return value is the same:
An iterator pointing to either the newly inserted element or to the element that already had its same value in the set.
For C++98, I have code to insert a sequence of adjacent items as follows:
void example98(std::set &_sent, int beginOffset, int lastOffset) {
std::set<int>::iterator itr = _sent.end();
for (int offset = beginOffset; offset <= lastOffset; ++offset) {
itr = _sent.insert(itr, offset);
}
}
I could change this in C++11 to be:
void example11(std::set &_sent, int beginOffset, int lastOffset) {
std::set<int>::iterator itr = _sent.end();
for (int offset = lastOffset; offset >= beginOffset; --offset) {
itr = _sent.insert(itr, offset);
}
}
But am bothered by the need to refactor the code to go from C++98 to C++11, am I doing something wrong here, or if not, what was the motivation for this change, and why was the argument to insert()
changed but not the return value?
Aucun commentaire:
Enregistrer un commentaire