This article summarizes the common vector operation, is the continuation of the previous article! Only the code, see the comments in the code for details.
[CPP] View plaincopy/*
* File_name:vector_test.cpp
*
* Created on:2014 June 28 3:34:23
* Author:the_third_wave,
* Last modified:2014 June 28 3:34:23
#include
#include
#include "headers/myfunc.h"
#include "Headers/person.h"
void output (const std::vector &vec)
Because it is the output instead of the modification, the definition parameter is a constant reference, increasing reliability (const) and efficiency (&)!
{
Std::cout << "Size:" << vec.size () << ", Capacity:" <
}
int main ()
{
Std::vector Vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Pr_vector (VEC);
Access to the first element, native methods of the Vec.front () and vec.back () are generally best done before using the element check Vec.size ()
Std::cout << Vec.front () << std::ends << vec.back () << Std::endl;
Through iterators "NOTE: * (--vec.end ())", before use generally best do element check Vec.size ()
Std::cout << *vec.begin () << std::ends << * (--vec.end ()) << Std::endl;
Auto A = Vec.size (); Let the compiler automatically parse the type that the expression belongs toAct Answers
Auto B = vec.capacity (); How many elements can a vector hold without reallocating memory?
Std::cout << "Size:" << a << std::ends << "Capacity:" <
Vec.reserve (50); Allocates a memory space that can hold at least n elements
Output (VEC);
Vec.shrink_to_fit (); Reduce capacity () to size ()
Output (VEC);
Vec.reserve (50); Allocates a memory space that can hold at least 50 elements
The following add elements
For (Decltype (Vec.size ()) IX = 0; IX! =), ++ix)//decltype derive variable type from expression type
{
Vec.push_back (Ix*ix);
}
Pr_vector (VEC);
Output (VEC); The S output proves that no excess space will be allocated without exceeding Size,vector
We don't allocate space, see Automatic management
For (Decltype (Vec.size ()) IX = 0; IX! =), ++ix)//decltype derive variable type from expression type
{
Vec.push_back (Ix*ix);
}
Pr_vector (VEC);
Output (VEC); The output indicates that the excess space is allocated, "the test shows that the allocated space is twice times the current, which means that the larger the more wasted"
The following reinitialization begins the insert operation of learning "and learn several ways to assign values"
Std::vector vec2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Re-initialize
Pr_vector (VEC); Pr_vector (VEC2);
Std::swap (VEC, VEC2); "Fast" swaps data swap for two containers (A, B)
Pr_vector (VEC); Pr_vector (VEC2);
Vec2.assign (Vec.begin (), Vec.end ()); An iterator with another vector of the same type as the parameter
Pr_vector (VEC); Pr_vector (VEC2);
Vec.assign ({0, 0, 0, 0, 0}); parameter is a list of initialization
Pr_vector (VEC);
Vec.assign (10, 1); Replace with 10 x 1
Pr_vector (VEC);
Vectors do not have native methods for inserting data into the head, only the Insert () method, and the insertion position is the previous position pointed to by the pointer
There are 4 specific types of
The first type of Vec.insert (P, t); P is its own iterator, T is the value that needs to be inserted, and the return value is an iterator pointing to the newly added element
Auto P = Vec.insert (Vec.begin () + vec.size ()/2, 6688);
The second type of Vec.insert (p, N, T); P is its own iterator, inserting N T, the return value is the iterator pointing to the newly added first element, and if n is 0, the P is returned
Vec.insert (P, 3, 4);
The Third Kind of Vec.insert (p, B, E); P is its own iterator, B, E is an iterator to the other VEC object of the same type, and the return value is an iterator to the newly added first element. The range is empty, then the P is returned
Vec.insert (P, Vec2.crbegin (), Vec2.crend ()); Const Reverse IteratorSat Answer
The fourth kind of vec.insert (p, IL); P is its own iterator, IL is a list of element values, the return value is an iterator pointing to the newly added first element, the list is empty, and the P is returned
Vec.insert (Vec.begin (), {9,8,7,6,5,4,3,2,1,0});
Pr_vector (VEC);
With the use of Insert return values, you can implement repeated insertions at a specific location, as shown above
Emplace operation, "c++11" Emplace_front "Vector not", emplace, emplace_back corresponding push_front "vector not", insert, push_back
Std::vector per = {"The_third_wave", 100,}}; Class initialization +vector, so {{}, {}} must beTOEFL Answers www.jszdsy.com
Per.emplace_back ("The_third_wave", 188,);
Per.emplace (Per.begin () + 1, person ("The_third_wave", 168,));
for (auto &p:per)
{
Print (Std::cout, p);
}
Delete operation, note that the compiler does not check whether the element exists, so pit itself fills
Vec.pop_back (), Vector no Pop_front ()
VEC = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Vec.pop_back ();
Pr_vector (VEC);
Vec.erase (P) "Removes the element referred to as the iterator p, and the return value is: The iterator after the deleted element. P point to the tail, back to the end of the iterator, if P is after the end of the iterator, then you are dead, congratulations! "
Vec.erase (Vec.begin () + 8);
Pr_vector (VEC);
Vec.erase (b, E) "Removes the range element that the iterator b,e refers to, the return value is: The iterator after the deleted element. If E is the post-tail iterator, the return is the end of the iterator, no pit, congratulations! "
Vec.erase (Vec.begin () + 3, Vec.end ()-1);
Pr_vector (VEC);
Vec.clear () Delete all, the return value is void
Vec.clear ();
Pr_vector (VEC);
Std::cout << "There's a blank line on it! Otherwise it's a mistake." ";
}
Size:10 capacity:10
Size:10, capacity:50
Size:10, Capacity:10
0 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521
SIZE:50, capacity:50
0 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521
Size:90, capacity:100
0 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 0 0) 0 0
1 1 1 1 1 1 1 1 1 1
9 8 7 6 5 4 3 2 1 0 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0 4 4 4 6688 1 1 1 1 1
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 7
There's a blank line on it! Otherwise it's a mistake.
How to use container vectors in C + +