๐Ÿ“ Coding Test Study/C++

C++ ๋ฐฐ์—ด/๋ฒกํ„ฐ์„ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ณ  ๋ฐ›๊ธฐ

ibelieveinme 2021. 4. 2. 00:53
728x90

vector์™€ array์˜ ์ฃผ์†Œ๊ฐ’์„ ์ „๋‹ฌํ•ด์„œ ์ง€์—ญ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

 

- vector๋ฅผ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ธฐ

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

void tossVector(vector<int>&);

int main() {
    int N = 0, num = 0;
    vector<int> v;

    //Input
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> num;
        v.push_back(num);
    }

    tossVector(v);
    
    //Output
    for (int i = 0; i < N; i++) {
        cout << v[i] << "\n";
    }
}

void tossVector(vector<int> &v) {
     sort(v.begin(), v.end());
}

- array๋ฅผ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ธฐ

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

void tossArray(int[]);

int main(void) {
    int n;
    cin >> n;

    int *arr = new int[n];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    tossArray(arr);

    for (int i = 0; i < n; i++) {
        cout << arr[i] "\n";
    }
    
    delete[] arr;
    return 0;
}

void tossArray(int arr[]){
    sort(arr, arr+sizeof(arr));
}

 

 

728x90