Updated March 28, 2023
Introduction to C++ Vector Sort
Vectors in C++ programming language are used as sequenced containers that help in representing an array that can be dynamically changed in size according to the input or the requirement. In general requirements we have to store data information in a sequential form and that’s why we use arrays to store them but arrays are static in nature, therefore, their size is fixed. Therefore, to store data in such a container that can automatically change size according to requirement vectors are introduced in C++. Vector arrays are dynamic in nature.
Syntax:
vector < data_type > variable_name;
In the above syntax vector is the mandatory keyword that has to be used before declaring a vector and data_type is the type of data you want to store it can be int, float, etc and variable_name is the name of the variable you want to define. To use vector in programming you have to include vector library in your code so that you can use all its functionalities.
You can do this by using the below code.
#include <vector>
int main ()
{
std :: vector < int > vector_user ;
}
How Does Vector Sorting Work in C++ Programming?
To do any type of vector sorting in C++ programming different iterators of vectors are used. There are usually 8 types of iterators that can be used to achieve sorting in C++. They are mentioned below:
Iterators | Functionality |
begin | It will return the iterator to the beginning of arrays. |
cbegin | It will return the constant iterator to the beginning. |
rbegin | It will return a reverse iterator to the reverse beginning of arrays. |
crbeign | It will return the constant reverse iterator to the reverse beginning of arrays. |
end | It will return the iterator to the end of the arrays. |
Cend | It will return the constant iterator to the end of arrays. |
Rend | It will return the reverse iterator to the reverse end of arrays. |
crend | It will return the constant reverse iterator to the reverse end of arrays. |
Examples of C++ Vector Sort
Let’s have a look at the examples and understand how actually a sorting can be done using vector arrays in C++.
Example #1
C++ code to demonstrate vector sorting in decreasing order.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector < int > v { 21, 74, 98, 64, 14, 8, 7, 38,19, 49 };
sort (v.begin (), v.end (), greater < int > () );
cout << " Here is the Sorted vector \n " ;
for ( auto i : v )
cout << i << " ";
return 0;
}
Output:
Here in the above code, you can see we have declared a vector array on integer data type and we have stored some values in the given vector. Also, we are using sort function to sort the vector array in decreasing order by using begin and end iterators in sort function. As a result, you can see the given vector array is sorted in decreasing order correctly.
Example #2
C++ code to demonstrate vector sorting in increasing order.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector < int > v { 21, 98, 77, 5, 19, 49, 32, 1, 27, 94 } ;
sort (v.begin () , v.end () ) ;
cout << " Here is the Sorted array \n " ;
for ( auto i : v )
cout << i << " " ;
return 0;
}
Output:
Here in the above code, you can see we have declared a vector array on integer data type and we have stored some values in the given vector. Also, we are using sort function to sort the vector array in increasing order by using begin and end iterators in sort function. As a result, you can see the given vector array is sorted in increasing order correctly. For loop is used to traverse through all the given values.
Example #3
C++ code to demonstrate vector sorting according to the start and the end time.
Code:
#include <bits/stdc++.h>
using namespace std ;
// Every interval has a start and end time.
struct TimeInterval {
int start , end ;
} ;
// Comparing 2 time intervals according to their starting times.
bool intervalCompare ( TimeInterval x1, TimeInterval x2 )
{
return ( x1.start < x2.start ) ;
}
int main ()
{
vector < TimeInterval > v { { 5, 9 }, { 2, 8 }, { 3, 7 }, { 4, 7 } } ;
// sort the intervals in increasing order of
// start time
sort ( v.begin () , v.end () , intervalCompare ) ;
cout << " Here are the time intervals sorted by the start time : \n " ;
for ( auto i : v )
cout << " [ " << i.start << " , " << i.end << " ] " ;
return 0 ;
}
Output:
Here in the above code, you can see we have declared a vector array of Time Interval in which we have defined 4 sets of values with their start and the end time interval. Also, we are using sort function to sort the vector array in a specific order by using begin and end iterators in sort function. As a result, you can see the given vector array is sorted in order correctly. As you can see we have also used one compare interval function where we have to compare all the 4-time slots values to each other. For loop is used to traverse through all the given values in the vector array.
Conclusion
vector sort in C++ programming language is one of the most used dynamic arrays. As it saves a huge amount of processing time and it is dynamic therefore a user doesn’t have to anything manually to change the size of the array which they have created earlier.
Recommended Articles
This is a guide to C++ Vector Sort. Here we discuss the introduction and how does vector sorting work in C++ programming along with different examples and its code implementation. You may also have a look at the following articles to learn more –