C++ Vector容器如何逆序

   2024-10-02 2530
核心提示:要将C++中的Vector容器逆序,可以使用其提供的reverse()函数。示例如下:#include iostream#include vectorint main() {std

要将C++中的Vector容器逆序,可以使用其提供的reverse()函数。示例如下:

#include <iostream>#include <vector>int main() {    std::vector<int> vec = {1, 2, 3, 4, 5};    // 将Vector容器逆序    std::reverse(vec.begin(), vec.end());    // 输出逆序后的Vector容器    for(int i : vec) {        std::cout << i << " ";    }    return 0;}

运行以上代码将输出:5 4 3 2 1,即将原始的{1, 2, 3, 4, 5}逆序为{5, 4, 3, 2, 1}。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号