Vector vs. Array idioms

Source: Internet
Author: User

The vector class provides an alternative representation of the built-in array, which, like the string class, is part of the standard library introduced with standard C + +, in order to use the vector we must include the associated header file:

#include <vector>

There are two different forms of using vectors, known as array habits and stl habits .

first, the array of customary usage

1. Define a vector of a known length:

vector< int > Ivec(Ten); An array-like definition of int ia[ten];

element can be accessed by ivec[ index number ]

Use if (Ivec.empty ()) to determine whether it is empty,ivec.size () determines the number of elements.

2. The element of the vector is initialized to the default value associated with its type: the default value for arithmetic and pointer types is 0, and for class type, the default value can be obtained by invoking the default constructor for this class , and we can also provide an explicit initial value for each element to complete the initialization, for example
vector< int > Ivec (10,-1);
Defines a ivec that contains 10 elements of type int each element is initialized to-1

For built-in arrays we can explicitly initialize the elements of an array to a set of constant values, for example:
int ia[6] = {-2,-1, 0, 1, 2, 1024};


We cannot explicitly initialize the vector in the same way , but you can initialize the vector to all or part of an existing array by specifying the start address of the array you wish to initialize the vector to and the next position of the array's last element, for example:
Copy the 6 elements of IA into the Ivec
vector< int > Ivec (ia, ia+6);


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

Copy 3 elements ia[2], ia[3], ia[4]
vector< int > Ivec (&ia[2], &ia[5]);


3. Unlike built-in arrays vectors can be initialized by another vector or assigned to another vector such as
vector< string > Svec;
void Init_and_assign ()
{
Initializes a vector with another vector
vector< string > User_names (SVEC);
// ...

Copy a vector to another vector
Svec = User_names;
}

Second, STL customary usage

The customary usage of vectors in STL9 is completely different. Instead of defining a vector of a known size, we define an empty vector
vector< string > text;


1. We insert elements into the vector, Instead of an indexed element, and assigning a value to an element, such as a push_back () operation, a while loop that inserts an element after the vector reads a sequence of strings from the standard input and inserts a string into the vector each time   
string word; 
while (cin >> word) { 
Text.push_back (word); 
//...  
}

Although we can still use subscript operators to iterate over access to elements    
cout << "words read is: \ n";  
 
for (int ix = 0; IX < text.size (); ++ix)   ;
      cout << text[IX] << ";  
 
cout << endl; < br> However, it is more typical to use the begin () and end () of the vector operation set to return the iterator   iterator   
Pair:
cout << "words read is: \ n";  
 
for (Vector<string>::iterator it = Text.begin ();  
    It! = Text.end (); ++it) & nbsp
           cout << *it << ';  
 
cout << endl 
Iterator is a class in the standard library that has  


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

Move the iterator forward to point it to the next element

2. Be careful not to mix these two idioms, for example, the following definitions
vector< int > Ivec;
Define an empty vector and write such a statement.
ivec[0] = 1024;
is wrong, because Ivec has no first element , we can only index the element that already exists in the vector the size () operation returns the number of elements that the vector contains.

3. Similarly, when we define a vector with a given size, for example:
vector<int> IA (ten);
Any insertion will increase the size of the vector, rather than overwriting an existing element, which may seem obvious, but the following errors are not uncommon among 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, Ivec contains 14 elements, and the elements of IA are inserted from the eighth element.

Vector vs. Array idioms

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.