How to Insert an Element in a Sorted Vector in C++?
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:
#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;
}
Output
1 2 3 4 5
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:
Table of Content
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().
#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;
}
Output
1 2 3 4 5
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.
#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;
}
Output
1 2 3 4 5
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.
#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;
}
Output
1 2 3 4 5
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().
#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;
}
Output
1 2 3 4 5