How to Insert an Element in a Sorted Vector in C++?
Last Updated :
06 Dec, 2024
In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.
The recommended way to insert an element is to first find the position of insertion using lower_bound() function and then insert the element using vector insert(). Let's take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 5};
// Element to insert
int x = 2;
// Finding the correct position
auto pos = lower_bound(v.begin(), v.end(), x);
// Inserting the element
v.insert(pos, x);
for (int i : v)
cout << i << " ";
return 0;
}
Explanation: The lower_bound() function returns the position where the given element should be present in the sorted vector. The new element is then inserted at this position using the vector insert() function.
We can also use the vector emplace() method in place of vector insert().
C++ also provides a few more methods to insert an element in a sorted vector. Some of them are given below:
Using upper_bound() and Vector emplace()
Just like lower_bound(), the upper_bound() function can also be used to find the correct position to insert an element in the sorted vector using vector emplace().
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 5};
// Element to insert
int x = 2;
// Find the position to insert
auto pos = upper_bound(v.begin(), v.end(), x);
// Insert element at correct position
v.insert(pos, x);
for (int i : v) cout << i << " ";
return 0;
}
Using Temporary Vector and merge()
The merge() function can be used to insert the given element in sorted vector by adding the element to a temporary vector and then merging them.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 5};
int x = 2;
// Temporary vector that only contains x
vector<int> t = {x};
vector<int> res;
merge(v.begin(), v.end(), t.begin(), t.end(), back_inserter(res));
// Assigning the resultant vector to v
v = res;
for (int i : v) cout << i << " ";
return 0;
}
Using push_back() and sort()
The simplest method is to add the element at the end of the vector using vector push_back() and then sort the vector using sort() function.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 4, 5};
int x = 2;
// Insert x at the end
v.push_back(x);
// sort again
sort(v.begin(), v.end());
for (int i : v) cout << i << " ";
return 0;
}
This method is generally not very efficient when the number of elements to be inserted is much less than the total number of elements.
Manually Using Binary Search
As the vector is sorted, custom binary search operation can be applied to find the position of where to insert the element. Then the element can be inserted at this position using vector insert().
C++
#include <bits/stdc++.h>
using namespace std;
// Binary search implementation
int findPos(vector<int> v, int x) {
int l = 0, r = v.size();
while (l < r) {
int m = l + (r - l) / 2;
if (v[m] < x)
l = m + 1;
else
r = m;
}
return l;
}
int main() {
vector<int> v = {1, 3, 4, 5};
int x = 2;
int pos = findPos(v, x);
// Insert at the correct position
v.insert(v.begin() + pos, x);
for (int i : v) cout << i << " ";
return 0;
}
Similar Reads
How to Insert an Element at a Specific Index in a Vector in C++? In C++, vector allows access to its elements using indexes. In this article, we will learn how to insert an element at a specific index in a vector in C++.The simplest method to insert an element at a specific index in a vector is by using vector insert() method. Letâs take a look at a simple exampl
3 min read
How to Insert Elements into 2D Vector in C++? In C++, 2D vectors provide several built-in methods to insert elements. The efficiency of the insertion depends on where the insertion occurs. In this article, we will explore different ways to insert elements into a 2D vector in C++ and their specific use cases.The simplest way to add elements to a
3 min read
How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to
3 min read
How to Find Index of a Given Element in a Vector in C++? Vectors stores elements in contiguous memory and these elements can be accessed by their indexes. In this article, we will learn the reverse process, i.e., finding the index of the given element in a vector in C++.The simplest way to find the index of the given element in the vector is by using find
3 min read
How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S
2 min read