c++ vector
C++
#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;
}// CPP program to initialize a vector from
// another vector.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vect1{ 10, 20, 30 };
vector<int> vect2(vect1.begin(), vect1.end());
for (int x : vect2)
cout << x << " ";
return 0;
}
// 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
- initialize 3d vector c++
- Category
- C++
- Title
- what is c_str()
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- array search c++
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- newline in c++
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- find_if c++
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- C++ and endl
- Category
- C++