C++从最小到最大排序数字

Oli*_*ver 9 c++ sorting numbers

如果我让用户输入10个随机数,我想从最小到最大排序,那么使用最基本的C++语言执行此操作的最佳方法是什么.

Jer*_*fin 21

std::vector<int> numbers;

// get the numbers from the user here.    

std::sort(numbers.begin(), numbers.end());
Run Code Online (Sandbox Code Playgroud)


pyf*_*fex 14

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {

    vector<int> vec;

    vec.push_back(1);
    vec.push_back(4);
    vec.push_back(3);
    vec.push_back(2);

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

    for (vector<int>::const_iterator it=vec.begin(); it!=vec.end(); ++it) {
      cout << *it << " ";
    }
    cout << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)