Printing vector in reverse order

后端 未结 5 1270
粉色の甜心
粉色の甜心 2021-01-21 22:34

Is there a better way of printing a vector in reverse order then this:

#include
#include
#include
using namespace          


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-21 23:29

    In C++20 you can utilize views::reverse found in ranges library and supported by gcc 10.

    #include 
    #include 
    #include 
    
    int main()
    {
    
            std::vector V{0, 1, 2, 3, 4, 5, 6, 7};
            for (auto v : V | std::views::reverse)
            {
                    std::cout << v << " ";
            }
            return 0;
    }
    

    And the output is:

    7 6 5 4 3 2 1 0
    

提交回复
热议问题