std::vector::vector

From Cppreference

Jump to: navigation, search
explicit vector( const Allocator& alloc = Allocator() );
(1)
explicit vector( size_type count,

                 const T& value = T(),
                 const Allocator& alloc = Allocator());
         vector( size_type count,
                 const T& value,

                 const Allocator& alloc = Allocator());
(2) (pre-C++11 version)


(C++11 version)

explicit vector( size_type count );
(3) (C++11 feature)
template <class InputIterator>

vector( InputIterator first, InputIterator last,

        const Allocator& alloc = Allocator() );
(4)
vector( const vector& other );
(5)
vector( const vector& other, const Allocator& alloc );
(5) (C++11 feature)
vector( vector&& other )
(6) (C++11 feature)
vector( vector&& other, const Allocator& alloc );
(6) (C++11 feature)
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
(7) (C++11 feature)

Constructs new container from a variety of data sources and optionally using user supplied allocator alloc.

1) default constructor. Constructs empty container.

2) constructs the container with count copies of elements with value value.

3) constructs the container with count copies of elements with value T().

4) constructs the container with the contents of the range [first, last).

5) copy constructor. Constructs the container with the copy of the contents of other.

6) move constructor. Constructs the container with the contents of other using move semantics.

7) constructs the container with the contents of the initializer list init.

Contents

[edit] Parameters

alloc - allocator to use for all memory allocations of this container
count - the size of the container
value - the value to initialize elements of the container with
first, last - the range to copy the elements from
other - another container to be used as source to initialize the elements of the container with
init - initializer list to initialize the elements of the container with

[edit] Complexity

1) constant

2-3) linear in count

4) linear in distance between first and last

5) linear in size of other

6) constant. If alloc is given and alloc != other.get_allocator(), then linear.

7) linear in size of init

[edit] Example

#include <vector>
#include <string>
 
int main() 
{
    // c++0x initializer list syntax:
    std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
 
    // words2 == words1
    std::vector<std::string> words2(words1.begin(), words1.end());
 
    // words3 == words1
    std::vector<std::string> words3(words1);
 
    // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::vector<std::string> words4(words1.size(), "Mo");
 
    return 0;
}

[edit] See also

assign
assigns values to the container
(public member function)
operator=
assigns values to the container
(public member function)