Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/constructor"

From cppreference.com
m (1 revision: import content)
(adding some examples)
Line 92: Line 92:
 
{{example cpp
 
{{example cpp
 
  | code=
 
  | code=
 +
#include <{{{1}}}>
 +
#include <string>
 +
 +
int main()
 +
{
 +
    // c++0x initializer list syntax:
 +
    std::{{{1}}}<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
 +
 +
    // words2 == words1
 +
    std::{{{1}}}<std::string> words2(words1.begin(), words1.end());
 +
 +
    // words3 == words1
 +
    std::{{{1}}}<std::string> words3(words1);
 +
 +
    // words4 is a bunch of "Mo"
 +
    std::{{{1}}}<std::string> words4(words1.size(), "Mo");
 +
 +
{{#switch:{{{1}}} | vector | deque=
 +
    // words5 == words1
 +
    std::{{{1}}}<std::string> words5(words1.size());
 +
    for (size_t i = 0; i < words1.size(); ++i) words5[i] = words1[i];
 +
}}
 +
    return 0;
 +
}
 
  | output=
 
  | output=
 
}}
 
}}

Revision as of 13:02, 4 July 2011

Template:cpp/container//sidebar Template:ddcl list begin <tr class="t-dcl ">

<td >
explicit {{{1}}}( const Allocator& alloc = Allocator() );
</td>

<td > (1) </td> <td > Template:cpp/container/mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
explicit {{{1}}}( size_type count,

                  const T& value = T(),
                  const Allocator& alloc = Allocator());
         {{{1}}}( size_type count,
                  const T& value,

                  const Allocator& alloc = Allocator());
</td>

<td > (2) </td> <td > Template:mark pre c++0x version


Template:mark c++0x version </td> </tr> <tr class="t-dcl ">

<td >
explicit {{{1}}}( size_type count );
</td>

<td > (3) </td> <td > Template:mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
template <class InputIterator>

{{{1}}}( InputIterator first, InputIterator last,

         const Allocator& alloc = Allocator() );
</td>

<td > (4) </td> <td > Template:cpp/container/mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
{{{1}}}( const {{{1}}}& other );
</td>

<td > (5) </td> <td > Template:cpp/container/mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
{{{1}}}( const {{{1}}}& other, const Allocator& alloc );
</td>

<td > (5) </td> <td > Template:mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
{{{1}}}( {{{1}}}&& other )
</td>

<td > (6) </td> <td > Template:mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
{{{1}}}( {{{1}}}&& other, const Allocator& alloc );
</td>

<td > (6) </td> <td > Template:mark c++0x feature </td> </tr> <tr class="t-dcl ">

<td >
{{{1}}}( std::initializer_list<T> init,
         const Allocator& alloc = Allocator() );
</td>

<td > (7) </td> <td > Template:mark c++0x feature </td> </tr> Template:ddcl list end

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 Template:cpp.

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.

Template:params

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

Template:complex

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 Template:cpp, then linear.

7) linear in size of init

Template:example cpp

Template:see also

Template:cpp/container/dcl list assignTemplate:cpp/container/dcl list operator=