C + + Vector

Source: Internet
Author: User

1.vector:

The standard library type vector represents a collection of objects in which all objects have the same type, and each object in the collection has a corresponding index that is used to access the object. Because the vector "holds" other objects, it is often called a container (container).

In detail: (1) Vector: The quantity with directionality. (2) programming language aspect: Vector is part of C + + Standard Template Library, Chinese occasionally translated "container", but not accurate. It is a multi-functional template class and function library that can operate multiple data structures and algorithms. Vector is considered to be a container because it can store various types of objects like a container, in short, a vector is a dynamic array that can hold any type and can add and compress data. 2. iterators

Iterators provide access to objects in a container, and define the scope of the objects in the container. An iterator is like a pointer. In fact, C + + pointers are also an iterator. However, iterators are not just pointers, so you can't think they must have address values. An array index, for example, can also be thought of as an iterator.

Iterators have a variety of different ways to create them. The program may create an iterator as a variable. An STL container class might create an iterator to use a particular type of data. As a pointer, you must be able to obtain data using the * operator class. You can also use other math operators such as + +. Typically, the + + operator is used to increment an iterator to access the next object in the container. If the iterator reaches the back of the last element in the container, the iterator becomes the Past-the-end value. It is illegal to use a past-the-end-worthy pointer to access an object, as if using null or as an initialized pointer.

The standard library type for the collection of objects vector
#include <iostream>
#include <vector>
#include <string>
using namespace Std; 6 int Main () 7 {8 vector <int> ivec1; 9 vector <int> ivec2 (IVEC1);//copy ivec1 to IVEC2;
Vector <int> ivec3=ivec1;//the same as the same meaning;

Vector <int> IVEC4 (10,1);//element is 10 1;
Vector <string> ivec5 (x, "a");//Element 10 "a";

Vector<int> vec;16 for (auto i=0; i<10; i++)

{

Vec.push_back (i);//Press I into the VEC to make vec[i] = i;
}


for (unsigned int i=0; i<vec.size (); i++)

{

cout<< "Initialize traversal:" <<vec[i]<<endl;//traverse with subscript form
}


Vector<int>::iterator it;//it initialized to an iterator
for (it = Vec.begin (); It!=vec.end (); it++)

{

cout<< "iterative traversal:" <<*it<<endl;//traversal with an iterator
}


Vec.insert (Vec.begin () +4,0),//vector insert element, Fifth Element inserted as 0;
for (unsigned int i=0; i<vec.size (); i++)

{

cout<< "Insert traversal:" <<vec[i]<<endl;

}
Vec.erase (Vec.begin () +2);//vector Delete the element, delete the third element;
for (unsigned int i=0; i<vec.size (); i++)

{cout<< "Erase traversal:" <<vec[i]<<endl;

}

Vec.erase (Vec.begin () +3,vec.begin () +7)//vector Delete element, delete from fourth element to 8th element, 8th element not deleted

for (Vector<int>::iterator it = Vec.begin (); It!=vec.end (); it++)

{cout<< "iterative traversal:" <<*it<<endl;

}
System ("pause");

return 0;

}

C + + Vector

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.