-
In C++, vector is used as a container, and its function is to store variables of struct type. Here's how vector might be used:
Tools Materials: Computer, C++ compiler.
1. First, construct an empty vector vector of type int.
2. You can see that the size of the vector is 0.
3. Then, six vector vectors with element values of 20 are constructed.
4. Successfully constructed 6 vectors with 20 elements.
5. Copy the object to another vector object as well.
6. The following is the result of the program.
Related information. C++ is the inheritance of the C language, which can not only carry out the process programming of the C language, but also carry out the object-based programming characterized by abstract data types, and can also carry out the object-oriented programming characterized by inheritance and polymorphism.
C++ excels at object-oriented programming as well as process-based programming, so C++ is based on the size of the problem it can adapt to.
C++ not only has the practical characteristics of efficient computer operation, but also strives to improve the programming quality of large-scale programs and the ability of programming languages to describe problems.
-
The resize operation changes the number of elements contained in the container. It is to open up the element space of type LINE vector(char), snakemap[i].resize(row) is to open up a space for each line to create a row of type char elements.
-
resize is to change the size of the container and create an object, so that after calling this function, you can reference the object in the container, so when adding a new element, use the operator operator, or use an iterator to reference the element object.
To give you an example, take a look:
std::vectorvec;
std::cout
-
You can create a struct a, assign 1 and 2 to a, and then insert it. At this time, these two statements can be used:
-
resize is to change the size of the container and create an object.
-
In C++, vector is used as a container, and its function is to store variables of struct type. Here's how vector might be used:
Tools Materials: Computer, C++ compiler.
1. First of all, open the C++ compiler and construct an empty vector vector of type int.
<>3. As shown in the red box, 6 vector vectors with element values of 20 are constructed.
4. The running results show that 6 vectors with elements of 20 are successfully constructed.
5. Take the range of the existing vector object as the parameter of the constructor, and copy the object to another vector object as well.
6. The following is the result of the program.
Basic function implementation.
1. Constructor.
vector(): Creates an empty vector
vector(int nsize): creates a vector with the number of elements set to nsize
vector(int nsize,const t& t): creates a vector with the number of elements set to nsize and the value of t
vector(const vector&): Copies the constructor.
vector(begin,end): Copies elements from another array in the [begin,end) interval to the vector.
2. Add functions.
void push back(const t& x): adds an element x to the tail of the vector
iterator insert(iterator it, const t t & x): The iterator in the vector adds an element x before pointing to the element
iterator insert(iterator it,int n,const t t&x): Adds n identical elements x before the iterator points to the element in the vector
iterator insert(iterator it,const iterator first,const iterator last): The iterator in the vector inserts data between [first,last] of another vector of the same type before pointing to the element.
instance: include include include using namespace std;
int main()
for(int i=0;i<5;i++) to remove the last data of the array.
cout<<"n"return 0;}
-
vectortest;Build a vector
Press 1 and 2 into the vector so that test[0] is 1 and test[1] is 2
1. Access with an iterator:
vector::iterator iter=;Define an iterator that can iterate over int vectors, pointing to the first position of test.
for(;iter!=;iter++)cout<<(iter);Iter++ refers to iterating one bit backward until the iter reaches beyond the end iterator and outputs the value that the iterator points to.
Second, we can also use AT access:
vectortest;Build a vector
Press 1 and 2 into the vector so that test[0] is 1 and test[1] is 2
int i =;i is 2
-
Vector is a very useful container in the C++ STL library, which is essentially a template class that can easily implement the function of dynamic arrays. By inserting elements into the tail and deleting the middle and clever elements, you can flexibly declare and use arrays without determining the size of the data.
Specifically, for the use of vector, the following basic operations are included:
1.Add Element: You can use the Sell button to add an element at the end of the vector using the Vector's push back() function.
2.Delete elements: You can use vector's erase() function to delete elements at a specific location.
3.Access elements: You can use vector's subscript operators, iterators, and other methods to access elements.
4.Traversing vectors: You can use for loops, iterators, algorithms, and other methods to traverse elements in vectors.
It is important to note that the elements stored in the vector must be of the same type, and the type can also be customized. In addition, vector also supports advanced operations such as dynamically changing size, sorting, searching, and modifying.
-
vector "Type Name" Variable name declares vector vi = ( 10 , 1 ) A vector initialized as 10 elements, each of which is 1
Here's a summary, very detailed:
-
(1) vector< type > identifier;
2) vector< type > identifier (maximum capacity);
3) vector< type > identifier (maximum capacity, initial all values);
4) int i[4] = ;
vector< type > vi(i, i+2); Get the value of the index value of i after 3;
5) vector< vector> vi define a 2-dimensional container; Remember to have spaces, otherwise you will get an error.
vector< int > line
When using it, be sure to initialize the vi lines first;
for(int i = 0 ; i < 10 ; i ++
Personally, I think it's good to use vector to define a two-dimensional array, because the length can not be predetermined. Very good.
6) C++ Vector sorting.
vector< int > vi ;sort( , small to large reverse(, small from avenue.
7) Sequential access.
vector < int > vi ; for( int i = 0 ; i < 10 ; i ++for(int i = 0 ; i < 10 ;i++ The first method of calling.
8) Seek. vector < int > vi ; for( int i = 0 ; i < 10 ; i ++vector < int >:interator it = find( ,3) ;cout <9) uses an array to initialize the C++ vector.
int i[10] = ;the first vectorvi(i+1,i+3); From the 2nd element to the 3rd element for(vector ::interator it = ; it != ; it++)
10) Structure type.
struct temp tmp int main()
The vector class provides an alternative representation to built-in arrays, and like the string class, the vector class is a standard class.
As part of the standard library introduced by C++, in order to use vector it must contain the relevant header file.
-
Vectors, maps, lists, queues, etc. are all C++ templates. The vector is equivalent to an array in c, and the length is variable. vectortest(100);Here a vector, test, and the predefined size is 100.
The best way to manipulate the numbers in a vector is to use the iterator.
2. 1, True 2, False (there is only one program) 3, False (not necessarily) 4, True. >>>More
This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call. >>>More
It's a semi-translation, because the reason for the error is already given in the dialog box. >>>More
1.When some software runs for the first time, you will be prompted to select an access point, either directly in the settings or in the settings of those networked software. >>>More
Define the struct:
typedef struct _legaladdress_{ >>>More