Most useful or amazing STL short liners

后端 未结 8 1168
余生分开走
余生分开走 2021-01-29 22:55

I\'m looking for practical and educational samples of C++ / STL code fitting in few lines. My actual favorites are:

  1. Empty a vector freeing its reserved memory:<

8条回答
  •  旧巷少年郎
    2021-01-29 23:24

    // std::back_inserter usage ( std::inserter for map )
    std::copy( source.begin(), source.end(), std::back_inserter( container ) );  
    

    -

    // mem_fun and bind usage (but boost better)
    std::some_algorithm(..., std::mem_fun( func ) );  
    

    not so useful, but powerful:

    check is container sorted

    std::adjacent_find( container.begin(), container.end(), greater() ) == container.end()
    

    also examples mentioned by you and dirkgently.

提交回复
热议问题