Answers for "c++ function vector"

C++
36

c++ vector

#include <vector>

int main() {
  std::vector<int> v;
  v.push_back(10); // v = [10];
  v.push_back(20); // v = [10, 20];
  
  v.pop_back(); // v = [10];
  v.push_back(30); // v = [10, 30];
  
  auto it = v.begin();
  int x = *it; // x = 10;
  ++it;
  int y = *it; // y = 30
  ++it;
  bool is_end = it == v.end(); // is_end = true
  
  return 0;
}
Posted by: Guest on March-17-2020
3

vector functions c++

/*Member functions*/
                 Iterators
 -----------------------------------------
 begin | Returns an iterator to the beginning
 end   | Returns an iterator to the end
  
                 Capacity
 -----------------------------------------
 empty    | Checks whether the container is empty
 size     | Returns the number of elements
 reserve  | Reserves storage
 capacity | Returns the number of elements that can be held in currently allocated storage
     
              Element access
 -----------------------------------------
 at	     	| Access specified element with bounds checking
 front  	| Access the first element
 back   	| Access the last element
 operator[] | Access specified element
    
                 Modifiers
 -----------------------------------------
 clear        | Clears the contents
 insert       | Inserts elements
 emplace      | Constructs element in-place
 erase        | Erases elements
 push_back    | Adds an element to the end
 emplace_back | Constructs an element in-place at the end  
 pop_back     | Removes the last element        
 resize       | Changes the number of elements stored     
 swap         | Swaps the contents
  
  *Notes*
  - https://en.cppreference.com/w/cpp/container/vector
  - https://www.geeksforgeeks.org/vector-in-cpp-stl/
  - https://www.tutorialspoint.com/cpp_standard_library/vector.htm
Posted by: Guest on May-13-2021

Browse Popular Code Answers by Language