Copy every other element using standard algorithms (downsampling)

前端 未结 4 1343
长情又很酷
长情又很酷 2021-01-22 01:52

say I have a std::vector with N elements. I would like to copy every n-th element of it to a new vector, or average up to that element then copy it (downsample the

4条回答
  •  半阙折子戏
    2021-01-22 02:16

    If to use only standard library features and algorithms and if it is not allowed to use loops then the code can look the following way. Take into account that the code is based on the C++ 2014. If you need a code that will be compiled by a compiler that supports only C++ 2011 then you have to make some minor changes.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        const size_t N = 4;
        std::vector src = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
        size_t n = src.size() / N;
        std::vector dst( n );
    
        std::copy_if( src.begin(), std::next( src.begin(), n * N ), dst.begin(),
                      [i = 0] ( auto ) mutable { return ( i = ( i + 1 ) % N ) == 0; } );
    
        for ( double x : dst ) std::cout << x << ' ';
        std::cout << std::endl;
    
        dst.assign( n, 0.0 );
    
        std::accumulate( src.begin(), std::next( src.begin(), n * N ), dst.begin(),
                         [i = 0] ( auto acc, auto x ) mutable
                         {
                             *acc += x; 
                             if ( ( i = ( i + 1 ) % N ) == 0 )  *acc++ /= N;
                             return acc;
                         } );
    
        for ( double x : dst ) std::cout << x << ' ';
        std::cout << std::endl;
    }    
    

    The program output is

    4.4 8.8 
    2.75 7.15 
    

    This compound expression in the if condition

    if ( ( i = ( i + 1 ) % N ) == 0 )  *acc++ /= N;
    

    you can replace with more simpler one

    if ( ++i % N == 0 )  *acc++ /= N;
    

提交回复
热议问题