[C++] STL 정리

ewillwin·2023년 9월 14일
0

아무거나

목록 보기
23/23

C++ 코테 벼락치기

Pair

  • 첫번째 데이터는 first, 두번째 데이터는 second로 접근
  • p = make_pair(f, s)로 한 번에 대입하는 것도 가능
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main(int argc, const char* argv[]){
	pari<int, char> p;
    
    scanf("%d %c", &p.first, &p.second);
    printf("%d %c\n", p.first, p.second);
    
    p.first = 1;
    p.second = 'a';
    printf("%d %c\n", p.first, p.second);
    
    p = make_pair(3, 'b');
    printf("%d %c\n", p.first, p.second);
}

Vector

  • 크기가 가변적인 배열
  • front(): 첫 번째 원소
  • back(): 마지막 원소
  • begin(): 첫번째 위치
  • end(): 마지막의 다음 위치
  • push_back(): 마지막에 데이터 추가
  • pop_back(): 마지막에서 데이터 뽑기
  • size(): 원소의 개수
  • clear(): 비우기
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main(int argc, const char* argv[]){
	vector<int> v1 = {1, 2, 3};
    vector<pair<int, char>> v2;
    int a, b;
    char c, d;
    
    v1.push_back(5);
    v1.push_back(6);
    for (int i=0; i < v1.size(); i++){
    	pirntf("%d ", v1[i]));
    }
    printf("\n");
    
    a = v1.front();
    b = v1.back();
    printf("front: %d, back: %d\n", a, b);
    v1.pop_back();
    for (int i=0; i < v1.size(); i++){
    	printf("%d ", v1[i]);
    }
    printf("\n");
    
    v2.push_back(make_pair(1, 'a'));
    v2.push_back(make_pair(2, 'b'));
    v2.push_back(make_pair(3, 'c'));
    for (int i=0; i < v2.size(); i++) {
    	printf("<%d %c> ", v2[i].first, v2[i].second);
    }
    printf("\n");
    
    a = v2.front().first;
    c = v2.front().second;
    b = v2.back().first;
    d = v2.back().second;
    printf("front: <%d %c>\n", a, c);
    printf("back: <%d %c>\n", b, c);
    v2.clear();
}

Queue

  • queue 헤더파일 추가
  • push(): 마지막에 데이터 추가
  • pop(): 맨 앞에서 데이터 뽑기
  • front(): 첫 번째 원소
  • back(): 마지막 원소
  • size(): queue의 크기
  • empty(): queue가 비어있는 지 확인
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int main(int argc, const char* argv[]) {
	queue<int> q1;
    queue<pair<int, char>> q2;
    int a, b;
    int n;
    char c, d;
    
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.push(4);
    q1.push(5);
    q1.push(6);
    a = q1.front();
    b = q1.back();
    n = q1.size();
    
    for(int i=0; i<n; i++){
        printf("%d ", q1.front()); q1.pop();
        
    }
    printf("\n");
    
    printf("front: %d, back: %d\n", a, b);
    q2.push(make_pair(1, 'a'));
    q2.push(make_pair(2, 'b'));
    q2.push(make_pair(3, 'c'));
    q2.push(make_pair(4, 'd'));
    q2.push(make_pair(5, 'e'));
    
    a = q2.front().first;
    c = q2.front().second;
    b = q2.back().first;
    d = q2.back().second;
    n = q2.size();
    for(int i=0; i<n; i++){
        printf("<%d %c> ", q2.front().first, q2.front().second);
        q2.pop();
        
    }
    printf("\n");
    printf("front: <%d %c>, back: <%d %c>\n", a, c, b, d);
}

Stack

  • stack 헤더 파일 추가
  • push(): top에 데이터 추가
  • pop(): top에서 데이터 뽑기
  • top(): top에 있는 원소
  • size(): stack의 크기
  • empty(): stack이 비어있는 지 확인
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;

int main(int argc, const char * argv[]) {
    stack<int> s1;
    stack<pair<int, char>> s2;
    int a, b;
    int n;
    char c, d;
    
    s1.push(1); s1.push(2); s1.push(3);
    s1.push(4); s1.push(5); s1.push(6);
    
    a = s1.top();
    n = s1.size();
    
    for(int i=0; i<n; i++){
        printf("%d ", s1.top()); s1.pop();
        
    }
    printf("\n"); printf("top: %d\n", a);
    
    s2.push(make_pair(1, 'a'));
    s2.push(make_pair(2, 'b'));
    s2.push(make_pair(3, 'c'));
    s2.push(make_pair(4, 'd'));
    s2.push(make_pair(5, 'e'));
    
    a = s2.top().first;
    c = s2.top().second;
    n = s2.size();
    for(int i=0; i<n; i++){
        printf("<%d %c> ", s2.top().first, s2.top().second);
        s2.pop();
        
    }
    printf("\n");
    printf("top: <%d %c>\n", a, c);
    

Set

  • set 헤더 파일 추가
  • Key값 중복 허용 안됨
    • insert()를 통해 삽입
    • 자동으로 오름차순 정렬
  • inset(k): 원소 k 삽입
  • begin(): 맨 첫번째 원소를 가리키는 iterator 반환
  • end(): 맨 마지막 원소를 가리키는 iterator 반환
  • find(k): 원소 k를 가리키는 iterator 반환
  • size(): set의 원소 수
  • empty(): 비어있는 지 확인
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

int main(int argc, const char * argv[]) {
    set<int> s1;
    s1.insert(1); s1.insert(2); s1.insert(6);
    s1.insert(5); s1.insert(4); s1.insert(3);
    
    set<int>::iterator it;
    
    for (it = s1.begin(); it != s1.end(); it++) {
        printf("%d ", *it);
        
    }
    printf("\n");
    
    it = s1.find(7);
    printf("%d\n", *it);
    
    it = s1.find(6);
    printf("%d\n", *it);   
}

Map

  • map 헤더 파일 추가
  • []연산자가 제공되어 Key에 해당ㅇ하는 원소의 value에 바로 접근 가능
  • insert(make_pair(k, v)): 원소를 key와 value의 pair로 삽입
  • erase(k): key값 k를 갖는 원소를 삭제
  • begin(): 맨 첫번째 원소를 가리키는 iterator를 반환
  • end(): 맨 마지막 원소를 가리키는 iterator를 반환
  • find(k): key 값 k에 해당하는 iterator를 반환
  • size(): map의 원소 수
  • empty(): 비어있는 지 확인
#include <iostream>
#include <cstdio>
#include <map>

using namespace std;

int main(int argc, const char * argv[]) {
    map<char, int> m1;
    m1.insert(make_pair('a', 1)); m1.insert(make_pair('e', 5));
    m1.insert(make_pair('c', 3)); m1.insert(make_pair('d', 4));
    m1.insert(make_pair('b', 2));
    
    m1['e'] = 6;
    m1['f'] = 7;
    
    map<char, int>::iterator it;
    for (it = m1.begin(); it != m1.end(); it++) {
        printf("<%c %d> ", (*it).first, (*it).second);
        
    }
    printf("\n");
    it = m1.find('c');
    printf("%d\n", (*it).second);
    it = m1.find('d');
    printf("%d\n", (*it).second);
    m1.erase('a');
    m1.erase('c');
    
    for (it = m1.begin(); it != m1.end(); it++) {
        printf("<%c %d> ", (*it).first, (*it).second);
        
    } printf("\n");
}

Priority queue

  • push(): top에 데이터 추가
  • pop(): top에서 데이터 뽑기
  • top(): top에 있는 원소
  • size(): priority queue의 크기
  • empty(): priority queue가 비어있는 지 확인
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int main(int argc, const char * argv[]) {
    priority_queue< int, vector<int>, less<int> > q;
    int n;
    q.push(3); q.push(2);
    q.push(5); q.push(4);
    q.push(1); q.push(6);
    
    printf("top: %d\n", q.top());
    n = q.size();
    
    for(int i=0; i<n; i++){
        printf("%d ", q.top());
        q.pop();
        
    }
    printf("\n");    
}
profile
Software Engineer @ LG Electronics

0개의 댓글