[c++] 스니펫 및 디버깅

C++은 강력한 프로그래밍 언어로, 많은 유용한 스니펫과 디버깅 기술이 있습니다. 이 블로그 글에서는 유용한 C++ 스니펫과 디버깅 기술에 대해 알아보겠습니다.

C++ 스니펫

벡터 초기화

#include <vector>
std::vector<int> v = {1, 2, 3, 4, 5};

문자열 분할

#include <iostream>
#include <string>
#include <sstream>

std::string s = "Hello, World";
std::stringstream ss(s);
std::string token;
while (std::getline(ss, token, ',')) {
    std::cout << token << std::endl;
}

맵 순회

#include <iostream>
#include <map>

std::map<std::string, int> m = {{"apple", 2}, {"banana", 3}, {"cherry", 5}};
for (auto const& pair : m) {
    std::cout << pair.first << " = " << pair.second << std::endl;
}

C++ 디버깅

gdb를 이용한 디버깅

$ g++ -g file.cpp -o file
$ gdb ./file
(gdb) break main
(gdb) run

내부 변수 출력

(gdb) print variable

스택 트레이스 출력

(gdb) backtrace

조건에 따른 중단점 설정

(gdb) break file.cpp:10 if variable == 5

C++ 프로그래밍을 즐겁게 하기 위해 위의 스니펫과 디버깅 기술을 활용해 보세요. 만약 추가적인 도움이 필요하다면, C++ 관련 문서를 참고하시기 바랍니다.

참고 자료