
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert Multiple Elements at Given Positions in a Vector
What is a Vector in C++?
A vector is a dynamic array as the size of the vector changes during the program's execution. If we insert more elements in the vector it expands its size and if we remove elements from the vector it shrinks in size. A vector is part of the Standard Template Library (STL).Problem Description
In this problem, we are given a vector and have to insert an element at a certain position. In this article, we are going to learn how to insert multiple positions in a vector in C++ using different approaches. Below are some examples to understand the problem clearly:Example 1
-
Input
array = {10, 20, 30, 40, 50}
Insert 15 at position 1
Insert 25 at position 3 -
Output
array = {10, 15, 20, 25, 30, 40, 50}
Explanation:
If we insert element 15 at position 1 it will be inserted at index 1 and lie between 10 and 20 and if we insert element 15 at position 3 it will be inserted at index 3 and lies between 10 and 20.Example 2
-
Input
array = {1, 3, 7, 9, 11}
Insert 4 at position 2
Insert 8 at position 4 -
Output
array = {1, 3, 4, 7, 8, 9, 11}
Explanation:
If we insert element 4 at position 2, it will be inserted at index 2 and lie between 3 and 7. If we insert element 8 at position 4, it will be inserted at index 4 and lies between 7 and 9.Example 3
-
Input
array = {2, 4, 6, 8, 10}
Insert 3 at position 2
Insert 7 at position 4 -
Output
array = {2, 3, 4, 6, 7, 8, 10}
Explanation:
If we insert element 3 at position 2, it will be inserted at index 2 and lie between 2 and 4. If we insert element 7 at position 4, it will be inserted at index 4 and lies between 6 and 8.
Approaches
We will see and use different approaches for inserting multiple elements at given positions in a vector. Below are given three different approaches to it.Using insert() Method
This is the simplest approach for inserting a single element at any given position in a vector or a range of elements at a specific position in the vector. In this approach, we can insert an element using the insert() method at any specific position where we want to insert.Syntax for Inserting Single Element
vector.insert(position, value);Where,
position is the index, where we want to insert the element.
value is the element we want to insert.
Syntax for Inserting Multiple Elements
vector.insert(position, start_iterator, end_iterator);Where,
position is the index, where we want to insert the element.
start_iterator is an iterator that points to the first element to be inserted.
end_iterator is an iterator that points to the last element to be inserted.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { vector < int > vec = { 10, 20, 30, 40, 50 }; vec.insert(vec.begin() + 1, 15); vec.insert(vec.begin() + 3, 25); for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } return 0; }
Output
10 15 20 25 30 40 50Time Complexity: O(n)
Space Complexity: O(1)
Using emplace() Method
Syntax for Inserting Single Element
vector.emplace(position, value);Where,
position is the index, where we want to insert the element.
value is the element we want to insert.
Syntax for Inserting Multiple Elements
vector.emplace(position, value1);We repeat the above for each element we want to insert.
vector.emplace(position, value2);
Where,
position is the index, where we want to insert the element.
value is the element we want to insert.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { vector < int > vec = { 10, 20, 30, 40, 50 }; vec.emplace(vec.begin() + 1, 15); vec.emplace(vec.begin() + 3, 25); for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } return 0; }
Output
10 15 20 25 30 40 50Time Complexity: O(n)
Space Complexity: O(1)
Using a Loop for Multiple Elements Insertions
Syntax
vector.insert(vector.begin() + position, value);Where,
position is the index where the value is to be inserted.
value is the element to be inserted at a specific position
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { vector < int > vec = { 10, 20, 30, 40, 50 }; vector < pair < int, int >> insertionvector = {{1,15},{3,25},{6,45}}; for (auto & insertion: insertionvector) { vec.insert(vec.begin() + insertion.first, insertion.second); } for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } return 0; }
Output
10 15 20 25 30 40 45 50Time Complexity: O(nÃk)
Space Complexity: O(1)
Advertisements