Use the std::vector::insert function accepting an iterator to the first element as a target position (iterator before which to insert the element):
#include <vector>
int main() {
std::vector<int> v{ 1, 2, 3, 4, 5 };
v.insert(v.begin(), 6);
}
Alternatively, append the element and perform the rotation to the right:
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v{ 1, 2, 3, 4, 5 };
v.push_back(6);
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…