How to Sort a Vector in C++

rajneesh

rajneesh

5 min read

  • cpp
cpp sort vector

are you looking for a method by which you can sort vector in cpp. When you're working with data in C++, you might often find yourself needing to organize it. For instance, imagine you have a list of numbers or names, and you want to sort them in a specific order. One of the most common ways to store such lists of data in C++ is by using a vector. in this post i am going to show you most common and easy method to do that. when you are doing DSA you need to sorted data sometime you can try this method i have also use some that for sorting vector in cpp. i have get this method on github i will explain this method in very easy manner. follow my instruction step-by-step.

What is a Vector?

A vector in C++ is like an array, but more powerful and flexible due to its inbuild functions. It is part of C++ Standard Library and allows you to store a collection of elements, such as numbers or objects, that can grow or shrink their size dynamically according to the input size.

Sorting a Vector

To sort a vector in C++, you can use sort function that provided by the standard library. here is the simple example how can you sort vector step by step.

  1. Include the Necessary Headers: first and important part include the necessary relevant headers at the beginning of your program to use vector and sort function or some optional header to make code simple and easy to read.

    need to include the relevant headers at the beginning of your program.

#include <iostream>
#include <bits/stdc++.h> //for sorting and using vector its a universal header for all the important librarys
using namespace std; 

  1. Create and Populate the Vector: Next create your vector and add some elements to it.

int main() 
{ 
    vector<int> numbers{ 1, 3, 8, 96, 9, 17, 11, 4, 12, 10 }; 
    return 0; 
} 

  1. Sort the Vector: know sort the vector using the sort function from the bits/stdc++ library to sort the elements in ascending order you need to pass vector begin and vector end to sort the vector shown below.

sort(numbers.begin(), numbers.end());

  1. Display the Sorted Vector: Finally, you can print the sorted vector using the loop.

int main() 
{ 
    vector<int> numbers{ 1, 3, 8, 96, 9, 17, 11, 4, 12, 10 };
  
    sort(numbers.begin(), numbers.end()); 
  
    cout << "Sorted \n"; 
    for (auto x : numbers) 
        cout << x << " "; 
  
    return 0; 
} 

When you run this program, you will get this output on you screen:

Sorted
1 3 4 8 9 10 11 12 17 96

Sorting in descending Order

By default, the sort function arranges the elements in ascending order. If you want to sort the vector in descending order you need to greater function as a third parameter of sort function. it is part of built-in library. here is the code example of how you can sort it in decreasing order.


#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    vector<int> numbers{ 1, 3, 8, 96, 9, 17, 11, 4, 12, 10 };
  
    sort(numbers.begin(), numbers.end(), greater<int>()); 
  
    cout << "Sorted in decreasing order \n"; 
    for (auto x : numbers) 
        cout << x << " "; 
  
    return 0; 
} 

The output will be:

Sorted in decreasing order
96 17 12 11 10 9 8 4 3 1

Custom Sorting

Sometimes, you want to sort a vector using a custom parameter. You can do this by defining your own custom function or lambda expression by which you can sort expression for example you have a vector of string in which you want to sort that vector according to the string length for doing this you can use custom sorting expression here is the code:

#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
     vector<string> words = {"apple", "banana", "kiwi", "grape", "blueberry"};
  
    sort(words.begin(), words.end(),, [](const string &a, const string &b) {
        return a.size() < b.size();
    }); 
  
    cout << "Sorted by word length \n"; 
    for (auto x : words) 
        cout << x << " "; 
  
    return 0; 
} 

The output will be:

kiwi apple grape banana blueberry

In this example, the lambda function [](const string &a, const string &b) { return a.size() < b.size(); } compares the lengths of the strings to determine the order of words inside the vector you can see program output for better understanding.

Conclusion

Sorting a vector in C++ is straightforward with the sort function from the Standard Library. but in custom sorting it is little harder then the normal sort. you can try this code for better understanding. in this post i have covered all the common sorting problem occur during the cpp programming. if you getting any problem related to custom sort you can comment it i will defiantly solve that problem if you thing it help in you coding gurney then please like and share this post with you code laving friends

rajneesh

rajneesh

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.