C++

    C++ 문자열 공백 제거하는 방법

    #include #include #include #include #include #include #include #include using namespace std; int main() { string str = "Hello World!"; string str2 = str, str3 = str; cout

    [실1] 14888 - 연산자 끼워넣기

    #include using namespace std; #define OPERATOR_COUNT 4 int n; int arr_operand[1000]; // 수열 int arr_operator[OPERATOR_COUNT]; int min_val = INT_MAX; int max_val = INT_MIN; void DFS(int result, int idx) { if (idx == n) { if (result > max_val) max_val = result; if (result 0) { arr_operator[i]--; ..

    C++ virtual 다중 상속, 가상 부모 클래스

    다중 상속을 할 시 예상치 못하게 클래스가 중복될 가능성이 있다. class A { public: int a; }; class B : public A { public: int b; }; class C : public A { public: int c; }; class D : public B, public C { public: int d; }; A 클래스를 B, C 클래스가 상속받고 B, C 클래스를 D클래스가 상속받는 구조다. 문제는 이와같은 구조로 상속받을 때 A클래스의 내용물이 중복이 된다 하지만 중복되는 걸 희망하지 않을 때는 virtual 상속을 사용하면 된다. 클래스를 상속받을 때 상속받는 클래스에 virtual 을 앞에 붙여주면 된다 그렇게 되면, B, C 클래스를 상속받는 경우 B, C 클래스..

    [실1] 11403 - 경로 찾기

    플로이드 와샬 #include #define INF 100000 #define NODE 1000 using namespace std; int graph[NODE][NODE]{ 0 }; void Floyd_washall() { int n; cin >> n; for (int i = 0; i > graph[i][j]; } for (int k = 0; k < n; k++) // 거쳐가는 노드 { for (int i = 0; i < n; i++) // 출발지 노드 { for (int j = 0; j < n; j++) // 도착지 노드 { if (graph[i][k] && graph[k][j]) graph[i][j]= 1; } } } ..

    컴파일 속도 향상 (시간 초과 오류)와 Stream (스트림)

    ios::sync_with_stdio, cin.tie, cout.tie란? ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); Stream 우선 stream에 대한 이해가 먼저 필요하다 우리가 c언어와 c++언어를 가장 처음 배울 때 적는 것은 사실 아래에 두 헤더파일이다 각각은 stdio: standard input output iostream: input output stream #include #include 표준 스트림(standard streams)은 특정한 프로그래밍 언어 인터페이스뿐 아니라 유닉스 및 유닉스 계열 운영 체제(어느 정도까지는 윈도에도 해당함)에서 컴퓨터 프로그램과 그 환경(일반적으로 단말기) 사이에 미리 연결된 입출력 통로를 가리킨다. 우리..

    C++ _if STL 알고리즘 함수 (파라미터 _Pr _Pred) > 조건자 (Predicate)

    참고할만한 부분은 _if로 끝나는 함수들만 매개변수(조건자)로 넘길 수 있다는 점, sort 함수 예외. 참고 사이트 : https://en.cppreference.com/w/cpp/algorithm #include #include #include #include #include using namespace std; bool Is_one(int _val) { return _val == 1; } class Test { public: bool operator()(int i) { return i % 4 == 0; } }; struct s_test { public: bool operator()(int i) { return i % 2 == 0; } }; int main() { // 파라미터 _Pr _Pred > ..

    [실1] 11286 - 절대값 힙

    #include #include #include #include using namespace std; #define y first #define x second int main() { using pq_type = pair; cin.sync_with_stdio(0); cin.tie(0); priority_queue q; int n; cin >> n; for (int i = 0; i > val; if (val != 0) q.push({ abs(val), val }); else { if (q.empty()) cout

    C++ 구조적 바인딩 (Structured Bindings)

    C++17부터 지원하는 structured binding이란 어떤 배열, STL 같은 컨테이너에서 멤버들을 쉽게 바인딩할 수 있도록 도와주는 문법이다. 친숙한 std::map을 예시로 바인딩을 해보자. #include #include int main() { std::map mp; for (int i = 1; i < 11; i++) { mp[i] = i * i; } // iterator, basic for for (std::map::iterator it = mp.begin(); it != mp.end(); it++) { std::cout first

    [실1] 1991 - 트리 순회

    #include using namespace std; int n; struct node { char left; char right; }; struct node tree[100]; void preOrder(char root) { if (root == '.') return; else { cout t1 >> t2 >> t3; tree[t1].left = t2; tree[t1].right = t3; } preOrder('A'); cout left); cout data; In_order(_node->right); } // (왼쪽, 오른쪽, 루트) void Post_order(s_node* _node) { if (!_node) return; Post_order(_node->left); Post_order(_node..

    [실1] 2667 - 단지 번호 붙이기

    #include #include #include #include #include #include using namespace std; #define TOTAL_SIZE 5000 #define y first #define x second pair dir[] { { 1, 0 }, // 상 {-1, 0 }, // 하 { 0,-1 }, // 좌 { 0, 1} // 우 }; vector res(TOTAL_SIZE); int map[TOTAL_SIZE][TOTAL_SIZE]{ 0 }; bool visited[TOTAL_SIZE][TOTAL_SIZE]; int b_size = 0; int idx = 0; void BFS(int _y, int _x) { // 탐색 시작 queue q; q.push({ _y, _x })..