devSoyoung / algorithm-study

Algorithm Problem Source Code (Major Language: C++)
0 stars 0 forks source link

vector container sort : 오름차순, 내림차순 #3

Open devSoyoung opened 5 years ago

devSoyoung commented 5 years ago

오름차순

작은 수부터 앞에 정렬하고 싶을 때

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;

int main(void) {
    srand((int)time(NULL));      // for creating random number   

    vector<int> v;
    int n = 10;

    for (int i = 0; i < n; i++) {
        v.push_back(rand() % 10);   
    }
    sort(v.begin(), v.end());    
    return 0;
}

내림차순

큰 수부터 앞에 정렬하고 싶을 때

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;

int main(void) {
    srand((int)time(NULL));      // for creating random number   

    vector<int> v;
    int n = 10;

    for (int i = 0; i < n; i++) {
        v.push_back(rand() % 10);   
    }
    sort(v.begin(), v.end(), greater<int>());    
    return 0;
}
devSoyoung commented 5 years ago

Reference