Answers for "vector method c++"

C++
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
1

declare vectors c++

vector<int> vec;
//Creates an empty (size 0) vector
 

vector<int> vec(4);
//Creates a vector with 4 elements.

/*Each element is initialised to zero.
If this were a vector of strings, each
string would be empty. */

vector<int> vec(4, 42);

/*Creates a vector with 4 elements.
Each element is initialised to 42. */


vector<int> vec(4, 42);
vector<int> vec2(vec);

/*The second line creates a new vector, copying each element from the
vec into vec2. */
Posted by: Guest on May-25-2020

Browse Popular Code Answers by Language