analogue for .push_back()?

Issue #452 new
Edward Tang created an issue

hello, new to the blaze library, currently translating over some cfd code to utilize it.

is there an equivalent to the std::vector .push_back() / pop_back() operation? I understand that blaze::DynamicVector v[i] = elem is similar, but you need to specify the index. Is there an appending operation that does not need an index?

Comments (1)

  1. Klaus Iglberger

    Hi Edward!

    Thanks for creating this proposal. No, currently blaze::DynamicVector does not provide anything similar to std::vector<T>::push_back() or std::vector<T>::insert(). The rational behind that is that blaze::DynamicVector is not considered a container, but a numerical vector, which usually doesn’t grow element-wise. However, you can implement a push_back() function yourself:

    template< typename Type, bool TF, typename Alloc, typename Tag, typename T >
    void push_back( blaze::DynamicVector<Type,TF,Alloc,Tag>& vec, T&& value )
    {
       size_t const size = vec.size();
    
       vec.extend( 1, true );
       vec[size] = std::forward<T>(value);
    }
    
    int main()
    {
       blaze::DynamicVector<int> vec{ 1, 2, 3, 4 };
       vec.reserve( 10 );  // Reserve more memory for future push_back() calls
    
       push_back( vec, 5 );  // vec = ( 1, 2, 3, 4, 5 )
       push_back( vec, 6 );  // vec = ( 1, 2, 3, 4, 5, 6 )
       // ...
    
       return EXIT_SUCCESS;
    }
    

    I hope this helps,

    Best regards,

    Klaus!

  2. Log in to comment