RUN

Vectors and sort in C++

Read numbers into a vector, sort them and find the largest. Press Run to try it, then change the code and run it again.

main.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cout << "How many numbers? ";
    cin >> n;
    vector<int> v(n);
    cout << "Enter them: ";
    for (int &x : v) cin >> x;

    sort(v.begin(), v.end());
    cout << "Sorted: ";
    for (int x : v) cout << x << ' ';
    cout << "\nLargest: " << *max_element(v.begin(), v.end()) << '\n';
}
Output

Press Run to see the output.

More C++ examples

Open the full C++ online compiler →