vector stl c++
C++
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
// C++ program to illustrate the
// Modifiers in vector
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main()
{
// Assign vector
vector<int> v;
// fill the array with 10 five times
v.assign(5, 10);
cout << "The vector elements are: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
// inserts 15 to the last position
v.push_back(15);
int n = v.size();
cout << "\nThe last element is: " << v[n - 1];
// removes last element
v.pop_back();
// prints the vector
cout << "\nThe vector elements are: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
// inserts 5 at the beginning
v.insert(v.begin(), 5);
cout << "\nThe first element is: " << v[0];
// removes the first element
v.erase(v.begin());
cout << "\nThe first element is: " << v[0];
// inserts at the beginning
v.emplace(v.begin(), 5);
cout << "\nThe first element is: " << v[0];
// Inserts 20 at the end
v.emplace_back(20);
n = v.size();
cout << "\nThe last element is: " << v[n - 1];
// erases the vector
v.clear();
cout << "\nVector size after erase(): " << v.size();
// two vector to perform swap
vector<int> v1, v2;
v1.push_back(1);
v1.push_back(2);
v2.push_back(3);
v2.push_back(4);
cout << "\n\nVector 1: ";
for (int i = 0; i < v1.size(); i++)
cout << v1[i] << " ";
cout << "\nVector 2: ";
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
// Swaps v1 and v2
v1.swap(v2);
cout << "\nAfter Swap \nVector 1: ";
for (int i = 0; i < v1.size(); i++)
cout << v1[i] << " ";
cout << "\nVector 2: ";
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
}
Also in C++:
- Title
- loop through words in string c++
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- stack c++
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- variabvles in c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- if esle in c++
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- All palindromic substrings
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++