-
- /* Conclusion:
- The existence of vector frees developers from having to worry about memory application and release. However, the memory allocated behind the vector is allocated in multiples of 2. When the current capacity is insufficient for the inserted element, allocate a new memory, which is twice the size of the original vector capacity, and then copy the old memory to release the old memory, copying constructor and destructor may be involved multiple times, which is also the disadvantage of vector. */
When using the cocos2d-x engine, the program will collapse when the cclayer, ccsprite destructor are called multiple times.
- # Include <iostream>
- # Include <vector>
- Using namespace STD;
- Int main ()
- {
- Vector <int> ivec;
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // 1 element, container capacity is 1
- Ivec. push_back (1 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // two elements, container capacity 2
- Ivec. push_back (2 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // The container capacity is 4
- Ivec. push_back (3 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // four elements, container capacity: 4
- Ivec. push_back (4 );
- Ivec. push_back (5 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // five elements, with a container capacity of 8
- Ivec. push_back (6 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // six elements, with a container capacity of 8
- Ivec. push_back (7 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // seven elements, container capacity 8
- Ivec. push_back (8 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // 8 elements, container capacity is 8
- Ivec. push_back (9 );
- Cout <"Size:" <ivec. Size () <Endl;
- Cout <"capacity" <ivec. Capacity () <Endl; // nine elements, container capacity: 16
- /* Conclusion:
- The existence of vector frees developers from having to worry about memory application and release. However, the memory allocated behind the vector is allocated in multiples of 2. When the current capacity is insufficient for the inserted element, allocate a new memory, which is twice the size of the original vector capacity, and then copy the old memory to release the old memory, copying constructor and destructor may be involved multiple times, which is also the disadvantage of vector. */
- Return 0;
- }