Vector Operator = in C++ STL Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the vector operator = is used to assign the contents of one vector to another. It allows you to copy elements from one vector to another or initialize a vector with another vector's contents.Let’s take a look at a simple code example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3, 5, 4}; vector<int> v2; // Assign contents of v1 to v2 v2 = v1; for (int i : v2) cout << i << " "; return 0; } Output1 2 3 5 4 Explanation: The contents of vector v1 are copied to vector v2. After the assignment, v2 contains the same elements as v1.This article covers the syntax, usage, and common examples of vector operator = in C++:Table of ContentSyntax of Vector operator =Examples of Vector operator =Assign One Vector to AnotherReplace a VectorAssign an Empty VectorSyntax of Vector operator =v2 = v1;The destination vector (left operand) will be modified to contain the same elements as the source vector (right operand).Note: The containers should be of same types, an error is thrown. Examples of Vector operator =The below examples demonstrate the common usage of the vector operator = in C++.Assign One Vector to Another C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3, 5, 4}; vector<int> v2; // Assigning v1 to v2 v2 = v1; for (int i : v2) cout << i << " "; return 0; } Output1 2 3 5 4 Replace a Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3}; vector<int> v2 = {9, 8, 7}; // Reassign v1 to v2 v2 = v1; for (int i : v2) cout << i << " "; return 0; } Output1 2 3 Assign an Empty Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 2, 3}; vector<int> v2; // Assign an empty vector to v1 v1 = v2; if (v1.empty()) cout << "v1 is now empty."; return 0; } Outputv1 is now empty. Comment More infoAdvertise with us Next Article Vector Operator = in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Practice Tags : CPPSTL Similar Reads Vector operator[ ] in C++ STL In C++, the vector operator [] is used to randomly access or update the elements of vector using their indexes. It is similar to the vector at() function but it doesn't check whether the given index lies in the vector or not.Letâs take a look at a simple code example:C++#include <bits/stdc++.h 3 min read Vector insert() in C++ STL In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 min read set operator= in C++ STL The â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set 2 min read Vector size() in C++ STL In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example:C++#include <bits/stdc 3 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Like