Usage instance resolution of vector in C++

  • 2020-04-02 02:41:15
  • OfStack

This article demonstrates the use of vector in C++ as an example. The details are as follows:

An overview,

Vector is part of the C++ standard template library. It is a versatile template class and function library that can manipulate a variety of data structures and algorithms. A vector is a container that can hold various types of objects. In short, a vector is a dynamic array that can hold any type and dynamically change its size.
Such as:


//C language style
int myHouse[100] ;
//Using the vector
vector<int> vecMyHouse(100);

When defined above, vecMyHouse can hold 100 ints of data.

1. It can be accessed like a normal array
Such as:


vecMyHouse[50] = 1024;

2. You can fill the container with data sequentially
Such as:


int i =0 ;
for(  ; i< 25; i++ )
{
vecMyHouse.push_back(1);
}

3. It can also dynamically change its size with the following statement
Change the size of the container to 400 so that the container can hold 400 ints
Such as:


vecMyHouse.resize(400);

You can also load custom data types in containers
Such as:


//Customize a class
class Cmyclass
{
} ; 
//Define a container for the class
vector<Cmyclass> vecMyHouse;

5. You can assign an initial value to a container when you define it


//Defines a container for 100 int data with an initial value of 0
vector<int> vecMyHouse(100,0);

You can assign objects from one container to another
Such as:


//Defines a container for 100 int data with an initial value of 0
vector<int> vecMyHouse(100,0);
//Define a new container with the same contents as above
vector<int> myVec ;
myVec = vecMyHouse;

2. The above is a brief introduction of the vector container. The following is a detailed introduction of its other functions:

1. In order to use vector, you must include the following code in your header file:


#include <vector>

2. Vector belongs to the STD naming domain, so it needs to be qualified by naming and can be added at the beginning of the file


using std::vector;

or


using namespace std;

Or simply prefix the code that USES vector
Such as:


std::vector<int> myHouse;

3. Vector provides the following functions or operations:
Some of the commonly used features are listed below


//Define a vector
std::vector<int> c;

Features available:
Arthur c. Lear ()                 Removes all data from the container.
C.e mpty ()                 Determines if the container is empty.
C.e rase (pos)               Delete the data of pos location
C.ase (beg,end) deletes data from [beg,end) interval
C.f ront ()                 Returns the first data.
C.i nsert (pos, elem)   Insert a copy of elem at the pos location
C.p op_back ()         Delete the last data.
C.paush_back (elem) adds a data to the tail.
C.r esize (num)         Resize the container
C.s. considering ()                 The number of actual data in the container.
Mount do v.begin ()                     Returns an iterator that points to the first element of the container
C.e nd ()                         Returns an iterator that points to the last element of the container

Iii. What is an iterator described below

Iterators are equivalent to Pointers, for example:


//For variables, use a pointer to the corresponding variable
//You can then use * plus a pointer to manipulate the variable
int a = 10;
int *p;
p = &a;

Use Pointers to manipulate the variable
For example: *p = 11; // operation a becomes 11
For containers, iterators are used to manipulate the value of the corresponding location in the container
When the iterator points to a location in the container, you can use the * plus iterator to manipulate that location


//Define a vector
std::vector<int> myVec;
//Add 10 elements
for(int j =0 ; j<10 ; j++)
{
myVec.push_back(j);
}


//Define an iterator
std::vector<int>::iterator p;
//Points to the first element of the container
p = myVec.begin();
//Move to the next element
p ++;
//Modify the element assignment
*p = 20 ; //<The second value in the myVec container is changed to 20
//Loop scan iterator, changing all values
p = myVec.begin();
for( ; p!= myVec.end(); p++ )
{
*p = 50;
}

The above briefly describes the use of vector, just for getting started, and continues with an example below.

1. Data storage and output of vector:


#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std;
void main()
{
 int i = 0;
  vector<int> v;
  for( i = 0; i < 10; i++ )
 {
      v.push_back( i );//Store the elements one by one into the vector
 }
  //Empty the stored data
 for( i = 0; i < v.size(); i++ )//V.ize () represents the number of elements stored in the vector
 {
     cout << v[ i ] << " "; //Display each element
 }
 cont << endl;
}

Note: you can also use v.egin () and v.nd () to get Pointers to the starting and ending element addresses of the vector.

You can also do this:


vector<int>::iterator iter;
for( iter = v.begin(); iter != v.end(); iter++ )
{
  cout << *iter << endl;
}

2. Definition of two-dimensional vector.
1) define a vector element of 10, and set the value of 1-10 for each vector.


#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std;
void main()
{
int i = 0, j = 0;
//Define a two-dimensional dynamic array with 10 rows, each of which is a vector to store the data in.
//So the length of each row can vary. Vector<Int> (0) is the initialization of the vector, otherwise the vector cannot be stored elements.
vector< vector<int> > Array( 10, vector<int>(0) );
for( j = 0; j < 10; j++ )
{
 for ( i = 0; i < 9; i++ )
 {
  Array[ j ].push_back( i );
 }
}
for( j = 0; j < 10; j++ )
{
 for( i = 0; i < Array[ j ].size(); i++ )
 {
  cout << Array[ j ][ i ] << " ";
 }
 cout<< endl;
}
}

2) define an array whose rows and columns are all variable.


#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std;
void main()
{
int i = 0, j = 0;
vector< vector<int> > Array;
vector< int > line;
for( j = 0; j < 10; j++ )
{
 Array.push_back( line );//Each vector must be initialized, otherwise the element cannot be stored.
 for ( i = 0; i < 9; i++ )
 {
  Array[ j ].push_back( i );
 }
}
for( j = 0; j < 10; j++ )
{
 for( i = 0; i < Array[ j ].size(); i++ )
 {
  cout << Array[ j ][ i ] << " ";
 }
 cout<< endl;
}
}
 

Use vettor erase to specify the element, as shown below:


#include "iostream"
#include "vector"
using namespace std;
int main()
{
  vector<int> arr;
  arr.push_back(6);
  arr.push_back(8);
  arr.push_back(3);
  arr.push_back(8);
  for(vector<int>::iterator it=arr.begin(); it!=arr.end(); )
  {
    if(* it == 8)
    {
      it = arr.erase(it);
    }
    else
    {
      ++it;
    }
  }
  cout << "After remove 8:n";
  for(vector<int>::iterator it = arr.begin(); it < arr.end(); ++it)
  {
    cout << * it << " ";
  }
  cout << endl;
}

I hope the examples described in this article will help you to master the use of vector in C++.


Related articles: