How can I build a std::vector and then sort them?

前端 未结 7 2154
一向
一向 2021-02-06 22:09

I have a bunch of strings that I need to sort. I think a std::vector would be the easiest way to do this. However, I\'ve never used vectors before and so would like some help.

7条回答
  •  甜味超标
    2021-02-06 22:48

    For sort use:
    std::sort or std::vector< std::string>::sort(..) method.
    To check if it is sorted:
    use std::is_sorted for check is sorted - http://www.sgi.com/tech/stl/is_sorted.html
    or
    std::adjacent_find( v.begin(), v.end(), std::greater< std::string >() ) == v.end()

    for your case you could use default comparator

    EDITED:
    std::is_sorted is not standard stl function, it defined in sgi stl implementation.
    Thanks @Brian Neal for this note.

提交回复
热议问题