개발/C,C++ 4

StringFormat 만들기

C++20 이상: std::format 사용 예시C++20에서는 std::format이라는 새로운 함수를 도입하여 문자열 포맷팅을 더욱 쉽게 할 수 있게 되었습니다. 이 함수는 Python의 str.format과 유사한 방식으로 작동합니다. 다만, std::format은 컴파일 시간에 포맷 문자열을 검사하여 타입 안전성을 보장합니다.#include #include #include templatestd::string stringformat(const std::string& format, Args ... args) { size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; if (size buf(new char[size]); ..

개발/C,C++ 2024.03.04