C ++ vector container type

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>

Vector has two different forms:Array habitsAndSTL habits.

I. array usage

1. Define a vector with a known length:

Vector <int> ivec(10 );// Define int Ia like an array[10];

You can use ivec [Index Number] To access elements

UseIf (ivec. Empty ())Judge whether it is empty,Ivec. Size ()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.You 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 initialize the vector explicitly using the same method.But the vector can be initialized to 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 range of elements marked 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 cout cout but the more typical method is to use the begin () and end () in the vector operation set () the returned iterator iterator
pair:
cout <"Words read are: \ n";
for (vector :: iterator it = text. Begin ();
it! = Text. end (); ++ it)
cout <* It <'';
cout iterator is a class in the standard library and has pointer


* 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, rather than overwrite an existing element. This seems obvious, however, the following errors are not uncommon for beginners:
const int size = 7;
int Ia [size] = {0, 1, 1, 2, 3, 5, 8};
vector ivec (size );
for (int ix = 0; ix ivec. push_back (IA [ix]);
when the program ends , ivec contains 14 elements, and IA elements are inserted from the eighth element.

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.