C ++ vector

Source: Internet
Author: User

The vector class provides an alternative representation for built-in arrays. Like the string class, the vector class is part of the standard library introduced with standard C ++, to use vector, we must include the relevant header file:

# Include

Vector has two different forms: array habits and STL habits.

I. array usage

1. Define a vector with a known length:

Vector <int> ivec (10); // defines int ia [10] Like an array;

You can use ivec [index number] to access elements.

If (ivec. empty () is used to determine whether it is null, and ivec. size () is used to determine the number of elements.

2. the element of the vector is initialized to the default value related to its type: the default value of the arithmetic and pointer types is 0. For the class type, the default value can be obtained by calling such Default constructors, we can also provide an explicit initial value for each element to complete initialization, for example
Vector <int> ivec (10,-1 );
Defines ivec. It contains ten int elements. Each element is initialized to-1.

For built-in arrays, We can explicitly initialize the elements of the array into a group of constant values, for example:
Int ia [6] = {-2,-1, 0, 1, 2, 1024 };


We cannot explicitly initialize a vector using the same method, but we can initialize a vector into all or part of an existing array, you only need to specify the starting address of the array to be used to initialize the vector and the next position of the last element of the array. For example:
// Copy the six elements of ia to ivec.
Vector <int> ivec (ia, ia + 6 );


The two pointers passed to ivec mark the range of values used to initialize the object. The second pointer always points to the next position of the last element to be copied, the marked Element range can also be a subset of the array, for example:

// Copy the three elements, ia [2], ia [3], and ia [4].
Vector <int> ivec (& ia [2], & ia [5]);


3. Unlike the built-in array, a vector can be initialized by another vector or assigned to another vector. For example:
Vector <string> svec;
Void init_and_assign ()
{
// Use another vector to initialize a vector
Vector <string> user_names (svec );
//...

// Copy a vector to another vector
Svec = user_names;
}

Ii. STL usage

The usage of vector in st93. Instead of defining a known-size vector, we define an empty vector.
Vector <string> text;


1. we insert elements into the vector, instead of index elements, and assign values to elements, such as push_back () operations, insert a while loop under an element after the vector to read a string sequence from the standard input and insert a string to the vector each time.
String word;
While (cin> word ){
Text. push_back (word );
//...
}

Although we can still use subscript operators to iterate access elements
Cout <"words read are: \ n ";

For (int ix = 0; ix <text. size (); ++ ix)
Cout <text [ix] <'';

Cout <endl;
However, a typical practice is to use the iterator returned by begin () and end () in the vector operation set.
Right:
Cout <"words read are: \ n ";

For (vector : Iterator it = text. begin ();
It! = Text. end (); ++ it)
Cout <* it <'';

Cout <endl
Iterator is a class in the standard library and has the pointer function.


* It;
Dereference the iterator and access the actual object to which it points
++ It;

Move the iterator forward to point it to the next element

2. Do not mix these two habits. For example, the following definition
Vector <int> ivec;
Define an empty vector and then write such a statement.
Ivec [0] = 1024;
It is wrong. Because ivec does not have the first element, we can only index the existing element size () in the vector to return the number of elements contained in the vector.

3. Similarly, when we define a vector with a given size, for example:
Vector Ia (10 );
Any insert operation will increase the vector size, instead of overwriting an existing element. This seems obvious, but the following errors are not uncommon for beginners:
Const int size = 7;
Int ia [size] = {0, 1, 1, 2, 3, 5, 8 };
Vector <int> ivec (size );

For (int ix = 0; ix <size; ++ ix)
Ivec. push_back (ia [ix]);
At the end of the program, the ivec contains 14 elements, and the ia element is inserted from the eighth element.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.