C++ std::reverse的替代方案是保留向量中的所有元素吗?

C++ std::reverse的替代方案是保留向量中的所有元素吗?,c++,C++,我尝试了这个方法,但在连接到内存分配时出现了一个异常。用于按相反顺序迭代 如果尝试点1 temp.end,temp.begin;,它从开始到结束 另外,您在提供的代码中第二次声明了点1,只需一行代码即可。使用构建一个临时向量,然后将其内容返回到原始向量中 std::vector<int> temp; temp.insert(temp.begin(), points1.begin(), points1.end()); points1.clear(); std::vector<in

我尝试了这个方法,但在连接到内存分配时出现了一个异常。

用于按相反顺序迭代

如果尝试点1 temp.end,temp.begin;,它从开始到结束


另外,您在提供的代码中第二次声明了点1,只需一行代码即可。使用构建一个临时向量,然后将其内容返回到原始向量中

std::vector<int> temp;
temp.insert(temp.begin(), points1.begin(), points1.end());
points1.clear();
std::vector<int> points1(temp.end(), temp.begin());
temp.clear();

什么样的例外?
// original vector
std::vector<int> points1;

// create temporary using reverse iterators and swap with original vector
std::vector<int>(points1.rbegin(), points1.rend()).swap(points1);

// unnamed temporary is destroyed automatically