mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
document vector sort, reverse example
This commit is contained in:
parent
1e5f97c042
commit
2412f95964
@ -1,45 +1,43 @@
|
|||||||
// A C++ program to demonstrate working of sort(),
|
/**
|
||||||
// reverse()
|
* @file
|
||||||
|
* @brief A C++ program to demonstrate working of std::sort(), std::reverse()
|
||||||
|
*/
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <numeric> // For accumulate operation
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <numeric> //For accumulate operation
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int main()
|
/** Main function */
|
||||||
{
|
int main() {
|
||||||
// Initializing vector with array values
|
// Initializing vector with array values
|
||||||
int arr[] = {10, 20, 5, 23 ,42 , 15};
|
int arr[] = {10, 20, 5, 23, 42, 15};
|
||||||
int n = sizeof(arr)/sizeof(arr[0]);
|
int n = sizeof(arr) / sizeof(arr[0]);
|
||||||
vector<int> vect(arr, arr+n);
|
std::vector<int> vect(arr, arr + n);
|
||||||
|
|
||||||
cout << "Vector is: ";
|
std::cout << "Vector is: ";
|
||||||
for (int i=0; i<n; i++)
|
for (int i = 0; i < n; i++) std::cout << vect[i] << " ";
|
||||||
cout << vect[i] << " ";
|
|
||||||
|
|
||||||
// Sorting the Vector in Ascending order
|
// Sorting the Vector in Ascending order
|
||||||
sort(vect.begin(), vect.end());
|
std::sort(vect.begin(), vect.end());
|
||||||
|
|
||||||
cout << "\nVector after sorting is: ";
|
std::cout << "\nVector after sorting is: ";
|
||||||
for (int i=0; i<n; i++)
|
for (int i = 0; i < n; i++) std::cout << vect[i] << " ";
|
||||||
cout << vect[i] << " ";
|
|
||||||
|
|
||||||
// Reversing the Vector
|
// Reversing the Vector
|
||||||
reverse(vect.begin(), vect.end());
|
std::reverse(vect.begin(), vect.end());
|
||||||
|
|
||||||
cout << "\nVector after reversing is: ";
|
std::cout << "\nVector after reversing is: ";
|
||||||
for (int i=0; i<6; i++)
|
for (int i = 0; i < 6; i++) std::cout << vect[i] << " ";
|
||||||
cout << vect[i] << " ";
|
|
||||||
|
|
||||||
cout << "\nMaximum element of vector is: ";
|
std::cout << "\nMaximum element of vector is: ";
|
||||||
cout << *max_element(vect.begin(), vect.end());
|
std::cout << *max_element(vect.begin(), vect.end());
|
||||||
|
|
||||||
cout << "\nMinimum element of vector is: ";
|
std::cout << "\nMinimum element of vector is: ";
|
||||||
cout << *min_element(vect.begin(), vect.end());
|
std::cout << *min_element(vect.begin(), vect.end());
|
||||||
|
|
||||||
// Starting the summation from 0
|
// Starting the summation from 0
|
||||||
cout << "\nThe summation of vector elements is: ";
|
std::cout << "\nThe summation of vector elements is: ";
|
||||||
cout << accumulate(vect.begin(), vect.end(), 0);
|
std::cout << accumulate(vect.begin(), vect.end(), 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user