首页主机资讯c++ arraylist如何反转列表

c++ arraylist如何反转列表

时间2024-07-30 17:36:04发布访客分类主机资讯浏览1321
导读:要反转C++中的数组列表,可以使用标准库中的std::reverse函数。下面是一个示例代码: #include <iostream> #include <vector> #include <algorithm...

要反转C++中的数组列表,可以使用标准库中的std::reverse函数。下面是一个示例代码:

#include <
    iostream>
    
#include <
    vector>
    
#include <
    algorithm>


int main() {
    
    std::vector<
    int>
 nums = {
1, 2, 3, 4, 5}
    ;
    

    std::reverse(nums.begin(), nums.end());


    for (int num : nums) {
    
        std::cout <
    <
     num <
    <
     " ";

    }
    

    return 0;

}
    

在上面的代码中,我们首先创建了一个包含数字1到5的std::vector列表。然后,我们使用std::reverse函数来反转该列表。最后,我们遍历列表并打印出结果。输出将会是 5 4 3 2 1

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c++ arraylist如何反转列表
本文地址: https://pptw.com/jishu/691146.html
c++ arraylist能否动态增长 c++ arraylist的查找效率高吗

游客 回复需填写必要信息