Use and difference between C++ push method and push_back method

  • 2020-06-19 11:26:16
  • OfStack

【 abstract 】

push and push_back are common methods in STL that add elements to data structures. With initial knowledge of STL, the two methods are compared for the confusion caused by adding elements. In addition, this paper will briefly introduce the stack and queue series corresponding to push, the introduction of common methods, and the introduction of common methods corresponding to push_back series corresponding to vector. See below.

list also USES push_back.

[text]

Introduction to the push_back method

vector::void push_back (const value_type & val);

vector::void push_back (value_type & & val);

This function adds a new element to the end of vector at the next element of the current last element and the value of the new element is a copy (or move copy) of val

Introduction to vector common methods

(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); // The i index value is the value after 3;
(5)vector < vector < int > > //vi defines a 2-dimensional container; Remember that 1 must have a space, otherwise you might get an error


vector< int > line //  When in use 1 Make sure you take it first vi Three rows are initialized ; 
for(int i = 0 ; i < 10 ; i ++) 
{ 
vector.push_back(line); 
} 

vector defines a 2-dimensional array whose length may not be predetermined.
(6) C + + vector sort


vector< int > vi ; 
vi.push_back(1); 
vi.push_back(3); 
vi.push_back(0); 
sort(vi.begin() , vi.end()); /// / Since the childhood  
reverse(vi.begin(),vi.end()); ///  From big to small  

(7) Sequential access


vector < int > vi ; 
for( int i = 0 ; i < 10 ; i ++) 
{ 
vi.push_back(i); 
} 
for(int i = 0 ; i < 10 ; i ++) ///  The first 1 Type call method  
{ 
cout <<vi[i] <<" " ; 
} 
for(vector<int>::iterator it = vi.begin() ;it !=vi.end() ; it++) /// The first 2 Type call method  
{ 
cout << *it << " " ; 
} 

(8)


vector < int > vi ; 
for( int i = 0 ; i < 10 ; 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 in the container. 

(9) Use the array to initialize C++ vector


int i[10] ={1,2,3,4,5,6,7,78,8} ; 
/// The first 1 Kind of  
vector<int> vi(i+1,i+3); /// From the first 2 The number of elements to the number one 3 An element  
for(vector <int>::interator it = vi.begin() ; it != vi.end() ; it++) 
{ 
cout << *it <<" " ; 
} 

(10) vector of the structure


struct temp 
{ 
 public : 
 string str ; 
 public : 
 int id ; 
}tmp;
int main() 
{ 
vector <temp> t ; 
temp w1 ; 
w1.str = "Hello world" ; 
w1.id = 1 ; 
t.push_back(t1); 
cout << w1.str << "," <<w1.id <<endl ; 
return 0 ; 
}

push function introduction


stack::push();// Add elements at the top of the stack 

queue::push();// will x  To the end of the queue. 

Common methods of stack and queue are introduced

1, stack

The stack template class is defined in < stack > In the header file.

The stack template class requires two template parameters, one element type and one container type, but only the element type is required, and the default container type is deque when no container type is specified.

The sample code defining the stack object is as follows:

stack < int > s1;

stack < string > s2;

The basic operations of stack include:

s.push(); Add elements at the top of the stack

s.pop(); Note that an out - of - stack operation simply deletes the top element and does not return it.

Access the top of the stack, for example: ES158en.top ()

Check that the stack is empty, for example: s.empty(), return true when the stack is empty.

Access the number of elements in the stack, for example: ES167en.size ().

2, queue

The queue template class is defined in < queue > In the header file.

Much like the stack template class, the queue template class also requires two template parameters, one for the element type and one for the container type. The element type is required, while the container type is optional and defaults to deque.

The sample code defining the queue object is as follows:

queue < int > q1;

queue < double > q2;

The basic operations of queue include:

Join the team, for example: q.push(x); Connect x to the end of the queue.

q.pop(); Eject the first element of the queue. Note that the value of the eject element is not returned.

Access to the first element of the queue, for example: q.front (), the first element to be pressed into the queue.

Access the last element of the queue, for example: q.back (), the last element to be pressed into the queue.

Determine that the queue is empty, for example q.empty (), and return true when the queue is empty.

The number of elements in the access queue, for example: q.size ()


Related articles: