Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append or insert std::array elements into a std::vector?

I'm relatively new to c++ and I've tried to do some research, but while searching online I've mainly come across C arrays rather than std::array. What are the most efficient ways to append std::array elements into a std::vector, and to insert std::array elements into a std::vector? Should I use STL functions such as std::copy? I'm currently using C++17, MinGW64.

like image 410
Fognam1n Avatar asked Feb 08 '21 07:02

Fognam1n


People also ask

How do you add an element to an array in vector?

Insertion: Insertion in array of vectors is done using push_back() function. Above pseudo-code inserts element 35 at every index of vector <int> A[n]. Traversal: Traversal in an array of vectors is perform using iterators.

How do you add elements to a vector array in C++?

To add elements to vector, you can use push_back() function. push_back() function adds the element at the end of this vector. Thus, it increases the size of vector by one.

How do you append to a std::vector?

Appending to a vector means adding one or more elements at the back of the vector. The C++ vector has member functions. The member functions that can be used for appending are: push_back(), insert() and emplace(). The official function to be used to append is push_back().

How to append array elements to a vector in C++?

I'm currently using C++17, MinGW64. To append the elements of an existing array (or other range in general) to a vector you can just use the vector's insert overload for an iterator range:

How to insert elements in a vector at a specific index?

STL Vector class provides the insert () function to insert elements in a vector at a specific index. In this example, we are going to use the insert () function to insert an element in a vector.

What is the use of insert method in vectors?

The insert method can be utilized even if the vector elements are allocated manually using new call; The insert method can also be applied when two vectors of strings need to be concatenated. The following example demonstrates the given case with almost the same syntax as used with the integer vectors.

How to append two vector in C++ STL?

In this article, we have explored Different ways to append two vector in C++ STL which include std::copy, insert, concat, overloaded + operator and other functions. 1. STL vs Standard Library STL stands from Standard Template Library and as the name suggests it is a library made of generic classes. The syntax to declare one of it is:


Video Answer


2 Answers

To append the elements of an existing array (or other range in general) to a vector you can just use the vector's insert overload for an iterator range:

   vector<int> vec{1, 2, 3};
   array<int, 3> arr{4, 5, 6};
   // arr could be some other container or bare array as well, for ex.:
   // int arr[] = {4, 5, 6};
   // vector<int> arr {4, 5, 6};
   // list<int> arr {4, 5, 6};
   // ...
   
   vec.insert(vec.end(), begin(arr), end(arr));  // insert at vec.end() = append
  //or  vec.insert(vec.end(), arr.begin(), arr.end());  // insert at vec.end() = append

Note that if you have some other type instead of int which is expensive to copy, and you want to move the elements from the source array, you can use move_iterator, for ex.

vec.insert(vec.end(), move_iterator(arr.begin()), move_iterator(arr.end()));

For operations on ranges in general, the container member functions are to be preferred instead of the same-named functions from the <algorithm> header.

So for example in this case the vec.insert will insert the range at once, as opposed if have had used the std::insert where the elements would be inserted one by one.

This is very nice explained in the Scott Meyers Effective STL.

Live

like image 128
StPiere Avatar answered Oct 20 '22 16:10

StPiere


"Appending" values can mean two things - you either want to copy/duplicate the elements in question, or you don't need them in the source container aftwards and might also move them into the destination container. Also, it makes sense to distinguish between insertion at construction time vs. appending to an existing, already constructed container: if you can, always construct a container with the elements that it's supposed to own.

  1. Copy-append std::array elements to an already constructed std::vector:

    std::vector<T> dest;
    std::array<T, N> source;
    
    // ...
    
    dest.insert(dest.end(), source.cbegin(), source.cend());
    
  2. Move-append std::arrayelements to an already constructed std::vector.

    std::vector<T> dest;
    std::array<T, N> source;
    
    // ...
    
    dest.insert(dest.end(), std::move_iterator(source.begin()),
                std::move_iterator(source.end()));
    
  3. Copy std::array elements into a std::vector at construction:

    std::array<T, N> source;
    
    // ...
    
    std::vector<T> dest(source.cbegin(), source.cend());
    
  4. Move std::array elements into a std::vector at construction:

    std::array<T, N> source;
    
    // ...
    
    std::vector<T> dest(std::move_iterator(source.begin()),
                        std::move_iterator(source.cend()));
    

There is not much to add here when talking about insertion into the middle - the only notable difference is that it will always be less efficient, as the remaining elements in the destination std::vector will be move-constructed (which is O(N)).

Note also that for appending elements, there is std::move and std::copy from the <algorithm> header (where std::copy can be used with std::move_iterators). However, these cannot be as efficient as a direct call to std::vector::insert, because the former operates on the iterator abstraction and processes the copy/move one element at a time without knowing about the storage details of the destination (this can result in multiple buffer resizings), while the latter is a std::vector member function and will resize the buffer only once (if required).

like image 37
lubgr Avatar answered Oct 20 '22 15:10

lubgr