Printing vector in reverse order

后端 未结 5 1273
粉色の甜心
粉色の甜心 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 23:26

    Use reverse_iterator instead of iterator

    int main()
    {
        int ia[4]={1, 2, 3, 4};
        vector vec(ia,ia+4);
        for(vector::reverse_iterator it = vec.rbegin; it != vec.rend(); ++it)
        {
            std::cout << *it << std::endl;
        }
    }
    

    The output will be: 4, 3, 2, 1

提交回复
热议问题