Function(1)

EHminShoov2J·2023년 8월 18일
0

CPP/코딩테스트

목록 보기
5/25
post-thumbnail

1. 초기화

1.1. fill()

fill()은 어떤 값으로든 배열을 초기화 할 수 있다.

fill(시작주소, 끝주소, 원하는 값)

이때 끝 주소는 포함하지 않는다. fill()을 사용하는 경우, 앞에서 부터 초기화가 발생하기 때문에, 우리가 예상치 못한 방식의 초기화가 발생할 수 있으므로, 사용시에는 되도록이면 전체를 초기화 하도록 하자.

#include <iostream>
#include <vector>

using namespace std;

int main(){
    cout << "fill()" << endl; // 모든 값으로 초기화 가능 
    int a[10];
    int b[10][10];

    fill(&a[0], &a[10], 100); // fill은 시작값 포함, 마지막 값 미포함이다. 
    for(int k : a){
        cout << k << endl;
    }
    fill(&b[0][0], &b[9][10], 7);
    for(int i=0; i < 10; i++){
        for(int j=0 ; j <10; j++){
            cout << b[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

1.2. memset()

바이트 단위의 초기화를 진행. 0,-1,char형(문자 하나)등으로 초기화 할 때만 사용한다. (fill() 보다 속도 측면에서 유리하다.)

memset( 배열이름, 초기화 값, 배열의 크기)

#include <iostream>
#include <vector>

using namespace std;

int main(){
    cout << "memset()" << endl; // 0,-1, char형 문자 하나 정도로 초기화 가능
    int b[10][10];
    
    memset(b, 0 , sizeof(b)); // 배열 이름, 원하는 수, 크기가 끝임
    for(int i=0; i < 10; i++){
        for(int j=0 ; j <10; j++){
            cout << b[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

2. 복사

2.1. memcpy()

Array에 대한 깊은 복사를 진행할 때 사용. 깊은 복사이기 때문에 원본에 영향을 주지 않는다.

memcpy( dest, source, size)

#include <iostream>
#include <vector>

using namespace std;

int main(){
cout << "memcpy(dest, source, size)" << endl;
    int c[3] = {1,2,3};
    int ret[3];
    memcpy(ret, c, sizeof(c));
    cout << ret[2] << endl;
    ret[2] = 1;
    cout << ret[2] << endl;
    cout << c[2] << endl << endl; // 깊은 복사가 진행되기 때문에 원본이 변하지는 않는다.
}

하지만 memcpy()는 TriviallyCopyable 타입이 아닌 vector와 같은 컨테이너에 대해서는 작동하지 않는다.

2.2. copy()

copy()는 vector와 array 모두에서 사용가능.

copy(fisrt, last, result)

fisrt, last는 원본의 시작과 끝(포함 x)을, result에는 저장하고자 하는 대상의 주소를 입력하면 된다.

#include <iostream>
#include <vector>

using namespace std;

int main(){
cout << "copy(first, last, result)" << endl;
    vector<int> v = {1,2,3};
    vector<int> v_ret(sizeof(v)); // 복사당할 Vector의 크기를 맞춰 주어야 함
    copy(v.begin(), v.end(), v_ret.begin());
    cout << v_ret[2] << endl << endl;

}

3. 정렬

3.1. sort()

컨테이너들의 요소를 정렬하는 함수.

sort(first, last, cmp)

  • 숫자의 경우 기본적으로 오름차순으로 정렬 (cmp 에 greater<타입>()을 인수로 제공하는 경우 내림차순으로 변경 가능)
  • pair 가 들어가는 경우 기본적으로 first, second 순으로 오름차순 정렬됨
  • cmp 생성시, return 값이 True 이면 순서가 유지.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(pair<int,int> a, pair<int,int> b){
    return a.second < b.second; // 참인 경우 순서 유지!! a < b 인경우, a가 그대로 앞으로 유지 
}

int main(){
    cout << "sort(first, last, cmp)" << endl;
    vector<int> v_sort = {2,1,5,6,3};
    for(int v : v_sort){ cout << v << " ";}
    cout << endl;

    sort(v_sort.begin(), v_sort.end()); // 기본적으로 오름차순으로 정렬
    for(int v : v_sort){ cout << v << " ";} 
    cout << endl;

    sort(v_sort.begin(), v_sort.end(), greater<int>()); // cmp 자리에 greater<타입>() 제공하여 내림차순으로 가능
    for(int v : v_sort){ cout << v << " ";} 
    cout << endl<<endl;

    vector<pair<int,int>> v_sort_pair;
    for(int i=1; i < 10; i++)v_sort_pair.push_back({i, 10-i});
    for(pair<int,int> v : v_sort_pair){ cout << v.first << "," << v.second << endl;}
    cout << endl;

    sort(v_sort_pair.begin(), v_sort_pair.end()); // pair 가 들어가는경우 자체적으로 첫번째, 두번째 기준으로 정렬됨 
    for(pair<int,int> v : v_sort_pair){ cout << v.first << "," << v.second << endl;}
    cout << endl;

    sort(v_sort_pair.begin(), v_sort_pair.end(), cmp); // cmp적용 
    for(pair<int,int> v : v_sort_pair){ cout << v.first << "," << v.second << endl;}


}

0개의 댓글