Most useful or amazing STL short liners

后端 未结 8 1077
余生分开走
余生分开走 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:26

    My favorite is copying containers to the output: And copying the input stream into a container.

    #include 
    #include  
    #include 
    #include 
    
    int main()
    {
        std::vector   data;
        std::copy(std::istream_iterator(std::cin),
                  std::istream_iterator(),
                  std::back_inserter(data)
                 );
    
        std::copy(data.begin(),data.end(),
                  std::ostream_iterator(std::cout,"\n")
                 );
    }
    

提交回复
热议问题