Note
: All initializations in this document are based on the following container
1 | // Create vector<int> container test and assign values to it |
Create an empty container
C<T> test;
C is the container type name, such as vector;
T is the element type, such as int or string;
Applicable toall containers
Initialize one container as a copy of another container
1 | // Create container testInitCopy and initialize it as a copy of container test |
- When copying one container to another, the types must match:
container type
andelement type
must be the same.- Applicable to
all containers
Initialize as a copy of a range of elements
Note: When initializing a container using iterators, the container types do not have to be the same. The element types within the containers can also be different, as long as they are compatible and can be converted to the element type of the new container being constructed.
1 | // The container testInitTwoIter has the same type as container test, while the iterators used are test.begin() and test.end() |
1 | // Container testchar and container testint have different types |
Output:
1 | A B C D E F G H I J |
Or initialize a copy of a range of elements:
1 | vector<char>::iterator charIter1=testchar.begin()+2,charIter2=testchar.end()-2; |
Output:
67
68
69
70
71
72
Allocate and initialize a specified number of elements (only applicable to sequence containers)
Create a container with a specified number of elements and provide an initialization expression
When creating a
sequence container
, you canexplicitly specify the size
of the container and a (optional) element initialization expression.Container size
can be aconstant
or anon-constant expression
, and the element initialization expression must be a value that can be used to initialize its element type.
1 | const vector<int>::size_type vectorSize=10; |
Output:
Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi
Note
: When creating a container, you can also choose whether or not to provide an element initialization expression. You can also just specify the container size.
Create a container with a specified number of elements but do not provide an initialization expression
1 | // Create a container with vectorSize elements, not explicitly providing an initialization expression (values initialized to 0) |
Or like this:
1 | extern unsigned get_word_count(const string &file_name); |
Rules for not providing an element initialization expression:
When an element initialization expression is not provided, the standard library implements
value initialization
for that container.
With this type of initialization, the element type must be a built-in or compound type, or a class type that provides a default constructor.
If the element type does not have a default constructor, you must explicitly specify its element initialization expression.
Additional Knowledge:
Value initialization
: If no element initialization expression is specified, the standard library will provide an initial value for elements through value initialization. This library-generated initial value will be used to initialize each element in the container, and the specific value depends on the data type of the elements stored in the container.
Element types may be class types that do not have any defined constructors. In this case, the standard library will still generate an object with initial values, in which all members have been value initialized.
Note: Constructors that accept container sizes as parameters only apply to sequence containers
, while associative containers do not support this initialization.