Constructors of containers in STL

STL中容器的构造函数

Note: All initializations in this document are based on the following container

1
2
3
4
// Create vector<int> container test and assign values to it
vector<int> test;
for(int i=0;i<10;i++)
test.push_back(i);

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 to all containers

Initialize one container as a copy of another container

1
2
3
4
5
6
7
// Create container testInitCopy and initialize it as a copy of container test
vector<int> testInitCopy(test);
cout<<"vector<int> testInitCopy(test):"<<endl
<<"C<T> testInitCopy(test)"<<endl;
for(vector<int>::iterator textInitCopyIter=testInitCopy.begin();
textInitCopyIter!=testInitCopy.end();textInitCopyIter++)
cout<<*textInitCopyIter<<" ";
  • When copying one container to another, the types must match: container type and element 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
2
3
4
5
6
7
8
// The container testInitTwoIter has the same type as container test, while the iterators used are test.begin() and test.end()
// Thus in this example, the container test is completely copied to the container testInitTwoIter
// (result is consistent with vector<int> testInitCopy(test), differing implementation)
vector<int> testInitTwoIter(test.begin(),test.end());
cout<<"vector<int> testInitTwoIter(test.begin(),test.end()):"<<endl;
for(vector<int>::iterator testInitTwoIterBegin=testInitTwoIter.begin();
testInitTwoIterBegin!=testInitTwoIter.end();testInitTwoIterBegin++)
cout<<*testInitTwoIterBegin<<" ";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Container testchar and container testint have different types
// Element assignment uses type conversion char->int (ASCII values of character type)
vector<char> testchar;
for(char i='A';i!='K';i++){
testchar.push_back(i);
}
// Traverse and output testchar
for(vector<char>::iterator testiter=testchar.begin();
testiter!=testchar.end();testiter++)
cout<<*testiter<<" ";
cout<<endl;
// Initialize testint as a copy of testchar (type conversion)
vector<int> testint(testchar.begin(),testchar.end());
// Traverse and output testint
for(vector<int>::iterator testintiter=testint.begin();
testintiter!=testint.end();testintiter++){
cout<<*testintiter<<" ";
}

Output:

1
2
A  B  C  D  E  F  G  H  I  J
65 66 67 68 69 70 71 72 73 74

Or initialize a copy of a range of elements:

1
2
3
4
5
6
7
vector<char>::iterator charIter1=testchar.begin()+2,charIter2=testchar.end()-2;
vector<int> testint(charIter1,charIter2);
// Traverse and output testint
for(vector<int>::iterator testintiter=testint.begin();
testintiter!=testint.end();testintiter++){
cout<<*testintiter<<" ";
}

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 can explicitly specify the size of the container and a (optional) element initialization expression.
Container size can be a constant or a non-constant expression, and the element initialization expression must be a value that can be used to initialize its element type.

1
2
3
4
5
6
7
const vector<int>::size_type vectorSize=10;
// Create a vector<string> container with vectorSize elements, each initialized to the string "Hi"
vector<string> test(vectorSize,"Hi");
for(vector<string>::iterator testiter=test.begin();
testiter!=test.end();testiter++){
cout<<*testiter<<" ";
}

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
2
// Create a container with vectorSize elements, not explicitly providing an initialization expression (values initialized to 0)
vector<int> testint(vectorSize);

Or like this:

1
2
extern unsigned get_word_count(const string &file_name);
vector<string> test(get_word_count("Chimera"))

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.

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:Constructors of containers in STL
Author:LIPENGZHA
Publish Date:2015/08/18 12:44
Word Count:2.4k Words
Link:https://en.imzlp.com/posts/42105/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!