본문 바로가기

c++18

c ++ std::sdplog 사용법 한방정리 #include class Logger { public: static std::shared_ptr instance(); void log(const std::string& message, const std::string& category); private: Logger(); // private 생성자 std::unordered_map loggers_; // 로그 카테고리별 logger 저장 // ... }; Logger::Logger() { auto file_sink = std::make_shared("log.txt", true); spdlog::set_default_logger(std::make_shared("logger", file_sink)); spdlog::set_pattern("[%Y-%m-%d .. 2023. 7. 26.
c++ shared ptr 활용해서 싱글톤 만들기 #ifndef LOGGER_H #define LOGGER_H #include #include class Logger { public: static std::shared_ptr instance(); void log(const std::string& message); private: Logger(); // private 생성자 // ... }; // Logger 인스턴스를 전역적으로 사용하기 위해 전역 함수로 래핑 std::shared_ptr logger(); #endif // LOGGER_H #include "logger.h" #include #include std::shared_ptr Logger::instance() { static std::shared_ptr logger_instance = std::.. 2023. 7. 25.
c++ static 사용이유 정적 멤버 변수: 클래스의 정적 멤버 변수는 클래스 인스턴스와 독립적으로 존재하며, 클래스의 모든 인스턴스가 공유합니다. 정적 멤버 변수는 클래스의 모든 객체 간에 상태를 공유하고 유지하기 위해 사용될 수 있습니다. 1.정적 멤버 함수: 정적 멤버 함수는 클래스의 인스턴스와 무관하게 호출할 수 있는 함수입니다. 이러한 함수는 특정 객체에 속한 데이터에 접근하지 않으므로, 주로 클래스 수준의 작업을 수행하기 위해 사용됩니다. 예를 들어, 유틸리티 함수 또는 클래스의 생성자를 대신하는 팩토리 함수 등을 구현할 때 유용합니다. 2.지역 정적 변수: 함수 내에서 static 키워드로 선언된 변수는 해당 함수가 호출될 때 한 번 초기화되며, 그 값을 유지합니다. 이러한 변수는 함수가 호출될 때마다 초기화되지 않고.. 2023. 7. 7.
c++ string 문자열 자르기 #include #include #include int main() { std::string s = "1230456789"; // example string std::stringstream ss(s); // create a stringstream with the string std::string token; while (getline(ss, token, '0')) { // split the string based on '0' character std::cout 2023. 2. 17.