Standard library type (II) -- vector type
Introduction:
Vector is a set of objects of the same type. Each object has a corresponding integer index value. Like a string object, the standard library manages memory related to storage elements.
We call vector a container. All objects in a container must be of the same type!
#include <vector>using std::vector;
[TEMPLATE]
Vector isClass template,You can use a template to write a class definition or function definition, and use it for multiple different data types!
However, to declare a type of object generated from a class template, additional information must be provided. For example:
Vector is not a data type, while vector <string> and vector <int> are all data types!
Body:
1. Definition and initialization of a vector object
// Four initialization methods of vector: vector <T> v1; vector <T> v2 (v1); vector <T> v3 (n, I ); vector <T> v4 (n );
Example:
vector<int> ivec1; vector<int> ivec2(ivec1); /* *ERROR *vector<string> strVec(ivec2); */ vector<int> ivec3(10,-1); vector<string> strVec(10,"HI");
2. vector object Value Initialization
1) if the vector object stores built-in data types (such as: int), the standard library will create the element initialization type with 0 values.
2) If the vector stores elements of the class type containing the constructor, the standard library will use the default constructor of this type to create the element initialization type.
* 3) if the class type elements saved by the vector do not have Default constructors, the programmer cannot provide only the number of elements, but also the initial value.
3. dynamic growth of vector objects
Because the growth efficiency of vector is very high, when the element value is known, it is best to dynamically add elements to it to let it "grow ^_^ ".
[Key Concept of P79: the dynamic growth of vector objects is very high. C/Java and other programmers read it. Recommended]
// P80 exercise 3.11 is the following statement correct or incorrect? Vector <int> ivec; // It is correct in C ++ 11. In C ++ 98/03, the vector <int> ivec; // correct vector <string> svec (10, "NULL"); // correct
4. The size of the vector object
The member function size returns the size_type value defined by the corresponding vector class.
Vector <int>: size_type length = st. size (); // correct vector: size_type lenth; // Error
5. The push_back () operation accepts an element value.
Int main () {string word; unsigned count = 0; vector <string> strVec; while (cin> word) {++ count; strVec. push_back (word);} if (count = strVec. size () {cout <"Equal! "<Endl;} // C ++ programmers should be used to it! = To limit the constraints of the loop for (vector <string >:: size_type index = 0; index! = StrVec. size (); ++ index) {cout <strVec [index] <endl;} return 0 ;}
[Key Concepts of P82: Secure generic programming is recommended !]
6. No element is added for the subscript operation!
Vector <int> ivec; for (vector <int>: size_type index = 0; index! = 10; ++ index) {/** must be an existing element to use the subscript operator to index * When the subscript operator is used to assign values, no element is added * ivec [index] = index + 1; ERROR */ivec. push_back (index + 1);} for (vector <int>: size_type index = 0; index! = Ivec. size (); ++ index) {cout <ivec [index] <endl;} for (vector <int>: size_type index = 0; index! = Ivec. size (); ++ index) {// for an existing element ivec [index] = 333;} for (vector <int >:: size_type index = 0; index! = Ivec. size (); ++ index) {cout <ivec [index] <endl ;}
7. An attempt to obtain nonexistent elements will inevitably result in a running error. However, it cannot be ensured that such errors can be caught during execution!
When the program is running, it will always fail in some interesting way @_@
// P83 exercise 3.13 (1) int main () {freopen ("input.txt", "r", stdin); vector <int> ivec; int value; while (cin> value) {ivec. push_back (value) ;}for (vector <int >:: size_type index = 0; index <ivec. size ()-1; index + = 2) {cout <ivec [index] + ivec [index + 1] <endl;} if (ivec. size () % 2) {cout <"The last element" <ivec [ivec. size ()-1] <"is not been summed! "<Endl;} return 0 ;}
//(2)int main(){ freopen("input.txt","r",stdin); vector<int> ivec; int value; while (cin >> value) { ivec.push_back(value); } vector<int>::size_type length = ivec.size(); cout << "Length is: " << length << endl; for (vector<int>::size_type index = 0; index < ivec.size()/2; ++index) { cout << ivec[index] + ivec[ivec.size() - 1 - index] << endl; } if (ivec.size() % 2) { cout << "The center element " << ivec[ivec.size() / 2] << " is not been summed!" << endl; } return 0;}
// Exercise 3.14int main () {freopen ("input.txt", "r", stdin); string word; vector <string> strVec; while (cin> word) {strVec. push_back (word) ;}for (vector <string >:: size_type I = 0; I! = StrVec. size (); ++ I) {for (string: size_type j = 0; j! = StrVec [I]. size (); ++ j) {strVec [I] [j] = toupper (strVec [I] [j]) ;}} for (vector <string> :: size_type index = 0; index! = StrVec. size (); ++ index) {cout <strVec [index] <''; if (! (Index + 1) % 8) {cout <endl ;}} return 0 ;}