Original: Https://software.intel.com/zh-cn/blogs/2011/08/10/c-vector
Standard library vector type
Use the required header files:
#include <vector>
Vector:vector is a class template. is not a data type. Vector<int> is a data type.
I. Definition and initialization
Vector<t> v1; Default constructor v1 is empty
Vector<t> v2 (v1);//v2 is a copy of V1
Vector<t> v3 (n,i);//v3 contains n elements with a value of I
Vector<t> v4 (n); V4 contains n elements with a value of 0
Second, value initialization
1> If no element initialization is specified, the standard library provides itself with an initialization value for value initialization.
2> If you save an element of a class type that contains constructors, the standard library initializes with that type's constructor.
3> If you save an element of a class type that does not have a constructor, the standard library produces an object with an initial value initialized with this object.
Iii. the most important operations of vector objects
1. V.push_back (t) add data at the end of the array with a value of t
2. V.size () The size of the data currently in use
3. V.empty () Determine if the vector is empty
4. V[n] Returns the element with position N in V
5. V1=v2 Replace the V1 element with a copy of the V2 element
6. V1==V2 judge whether V1 and V2 are equal
7.! =, <, <=, >, >= keep these operators in the habit of meaning
Vector container Type
A vector container is a template class that can hold objects of any type (but must be of the same class object). Vector objects can add elements efficiently at run time, and elements in the vector are stored continuously.
The structure of vectors
Function Prototypes:
Template<typename t>
explicit vector (); Default constructor, vector object is empty
Explicit vector (size_type n, const t& v = T ()); Creating a Vector object with n elements
Vector (const vector& x);
Vector (const_iterator first, const_iterator last);
Note: All objects stored in the vector container are initialized. If you do not specify an initial value for the storage object, the built-in type will be initialized with 0, and the default constructor will be called for the class type to initialize (if there are other constructors without a default constructor, this time
The element initial value must be provided in order to be placed in the container).
Example:
Vector<string> v1; Creates an empty container whose object type is the String class
Vector<string> v2 (10); Create a container of 10 string class objects with an initial value (that is, an empty string)
Vector<string> v3 (5, "Hello"); Create a container for a string class object with 5 values of "Hello"
Vector<string> v4 (V3.begin (), V3.end ()); V4 is the same container as the V3 (full replication)
Vector operation (The following function is a member function)
BOOL empty () const; Returns true if the container is empty, otherwise false
Size_type max_size () const; Returns the maximum number of elements the container can hold
Size_type size () const; Returns the number of elements in a container
Size_type capacity () const; The number of elements the container can store: capacity () >= size ()
void Reserve (Size_type n); Make sure Capacity () >= n
void Resize (size_type n, t x = t ()); Ensure that after return, there is: size () = = N; if the size () is <n, then the value of element x is complete.
Reference Front (); Returns a reference to the first element in the container (the container must be non-empty)
Const_reference Front () const;
Reference back (); Returns a reference to the last element in the container (the container must be non-empty)
Const_reference back () const;
Reference operator[] (size_type POS); Returns a reference to the element that is labeled POS (the subscript starts at 0; if the subscript is incorrect, it is undefined behavior.)
Const_reference operator[] (size_type pos) const;
Reference at (size_type POS); Returns a reference to the element labeled POS, or an exception if the subscript is incorrect Out_of_range
Const_reference at (size_type pos) const;
void push_back (const t& x); Add an element to the end of the container
void Pop_back (); POPs the last element in the container (the container must be non-empty)
Note: The following insert and delete operations will take place element movement (in order to maintain the nature of continuous storage), so the previous iterator may fail
Iterator insert (iterator it, const t& x = T ()); Insert element before insertion point element (or insert element at insertion point)
void Insert (iterator it, Size_type N, const t& x); Note iterators may no longer be valid (may reallocate space)
void Insert (iterator it, Const_iterator first, const_iterator last);
Iterator Erase (iterator it); Deletes the specified element and returns the position of an element after the element is deleted (if no element, returns end ())
Iterator Erase (iterator first, iterator last); Note: After deleting an element, the iterator corresponding to the element after the delete point is no longer valid.
void clear () const; Empties the container, which is equivalent to calling erase (begin (), End ())
void Assign (size_type n, const t& x = T ()); assignment, replacing all elements within the container with the specified sequence of elements
void Assign (Const_iterator first, const_iterator last);
Const_iterator begin () const; Iterative sequences
Iterator begin ();
Const_iterator end () const;
Iterator End ();
Const_reverse_iterator rbegin () const;
Reverse_iterator Rbegin ();
Const_reverse_iterator rend () const;
Reverse_iterator rend ();
Comparison of Vector objects (non-member functions)
There are six comparison operators for vector objects: operator==, operator!=, operator<, operator<=, operator>, operator>=.
For operator== and operator!=, if the vector object has the same number of elements, and the corresponding position elements are all equal, then two vector objects are equal;
For operator<, operator<=, operator>, operator>=, a dictionary sorting strategy is used.
Note: In fact, only need to implement operator== and operator!= can be, the other can be based on these two implementations. Because, operator!= (LHS, RHS) is! (LHS = = RHS), operator<= (LHS, RHS) is! (RHS < LHS),operator> (LHS, RHS) is (RHS < LHS),
Operator>= (LHS, RHS) is! (LHS, RHS).
An iterator to the vector class
The vector class iterator supports arithmetic operations in addition to the universal prefix increment operator: it + N, it-n, it2-it1. Note The It2-it1 return value is Difference_type (signed type).
Note that any operation that alters the size of the container can cause the previous iterator to fail.
Application examples
#include <iostream>
#include <cassert>
#include <vector>
using namespace Std;
int main ()
{
Vector<string> V (5, "Hello");
Vector<string> v2 (V.begin (), V.end ());
assert (v = = v2);
cout<< "> Before operation" <<endl;
for (Vector<string>::const_iterator it = V.begin (); it < V.end (); ++it)
cout<<*it<<endl;
V.insert (V.begin () + 3, 4, "Hello, World");
cout<< "> After Insert" <<endl;
for (Vector<string>::size_type i = 0; i < v.size (); ++i)
cout<<v[i]<<endl;
Vector<string>::iterator it = v.erase (V.begin () + 3, V.begin () + 6);
ASSERT (*it = = "Hello, world");
cout<< "> After Erase" <<endl;
for (Vector<string>::size_type i = 0; I! = V.size (); ++i)
cout<<v[i]<<endl;
ASSERT (V.begin () + v.size () = = V.end ());
ASSERT (V.end ()-v.size () = = V.begin ());
ASSERT (V.begin ()-v.end () = =-vector<string>::d Ifference_type (V.size ()));
return 0;
}
Program Description: The above program uses three loops to output the elements in the container, each loop traversal way is not the same. It is particularly necessary to note that the second loop uses the size () function in conditional judgment instead of saving it in a variable before the loop. The reason for doing this is that there are two
Reason: First, if in the future when the program is modified, the number of container elements in the loop is modified, the loop still works well, and if you save the size () function value is not correct, and because these small functions (its implementation only requires a return statement) is basically declared as inline, the
No need to consider the issue of efficiency.
---------------------------------
In the C + + programming language, there is an application method called Vector, which plays an important role in practical programming. Here we will give you a detailed introduction of C + + vector related application skills and basic content, I hope to bring you some help.
(1) vector< type > identifier;
(2) vector< type > identifier (maximum capacity);
(3) vector< type > identifier (maximum capacity, initial all values);
(4) int i[4] = {12,3,4,5};
vector< type > VI (i, i+2); Get the value of I index value after 3;
(5) vector< vector<int> >//vi define 2-dimensional containers; Remember that you must have a space, or you will get an error.
vector< int > Line
At the time of use, we must first initialize the VI lines;
for (int i = 0; i <; i + +)
{
Vector.push_back (line);
}
Personally think it's good to use vectors to define a two-dimensional array,
Because is the length can not be predetermined. Very good.
(6) C + + vector sorting
vector< int > VI;
Vi.push_back (1);
Vi.push_back (3);
Vi.push_back (0);
Sort (Vi.begin (), Vi.end ()); /Small to large
Reverse (Vi.begin (), Vi.end ())///From the Boulevard small
(7) Sequential access
Vector < int > vi;
for (int i = 0; i <; i + +)
{
Vector.push_back (i);
}
for (int i = 0; i <; i + +)///The first method of invocation
{
cout <<vector[i] << "";
}
for (Vector<int>::iterator it = Vi.begin ();
it!=vi.end (); it++)///second method of invocation
{
cout << *it << "";
}
(8) Find
Vector < int > vi;
for (int i = 0; i <; i + +)
{
Vector.push_back (i);
}
Vector < int >::interator it = find (Vi.begin (), vi.end,3);
cout << *it << Endl; Returns the location of the value found within the container.
(9) using arrays to initialize C + + vectors
int i[10] ={1,2,3,4,5,6,7,78,8};
First Kind
Vector<int> VI (I+1,I+3); From a 2nd element to a third element
for (vector <int>::interator it = vi.begin ();
It! = Vi.end (); it++)
{
cout << *it << "";
}
(10) Structure type
struct TEMP
{
Public:
String str;
Public:
int id;
}tmp
int main ()
{
Vector <temp> T;
Temp W1;
W1.str = "Hellowor";
W1.id = 1;
T.push_back (t1);
cout << w1.str << "," <<w1.id <<endl;
return 0;
}
Yunnan University Assassin Shiangyang [email protected]
Reproduced C + + Vector usage tips