C&C++ (12) 썸네일형 리스트형 [C++] std::string::data() data()는 std::string 멤버함수로, c_str()과 유사하게(?) 동작한다. (const char* 반환) C++ 표준대략 C++표준에 대한 내용은 다음과 같다. C++11, data() 반환 타입에 대해 명시되어 있지 않음.컴파일러에 따라, char*를 반환할수도 있고, const char*를 반환할수도 있음.많은 구현체에서 const char*를 반환하도록 구현했음. C++14, data() 반환 타입이 const char*로 명시 C++17, std::string::data() 와 std::string::c_str()이 동일한 반환 타입(const char*)을 가지며, null-종결을 보장. 그런데, C++17, C++20에서 컴파일하면 위 내용과 조금 다르게 동작한다.테스트는 g+.. [C++11] cstdint 고정폭 정수 타입 자료형(fixed-width integer types)cstdint를 사용하면 플랫폼 독립적으로 정수 타입의 크기를 명확히 정의할 수 있다. 에서 제공하는 고정 폭 정수 타입 목록int8_t: 8비트 부호 있는 정수uint8_t: 8비트 부호 없는 정수int16_t: 16비트 부호 있는 정수uint16_t: 16비트 부호 없는 정수int32_t: 32비트 부호 있는 정수uint32_t: 32비트 부호 없는 정수int64_t: 64비트 부호 있는 정수uint64_t: 64비트 부호 없는 정수 [C++] 소수점 출력 g++에서 cout은 기본적으로 6자리까지만 출력한다.좀 더 자세하게 출력하려면, std::fixed 와 std::setprecision을 사용하면 된다.#include #include // std::fixed, std::setprecisionint main() { // 소수점 연산을 위한 변수 double a = 1.0 / 3.0; // 예제 값: 1/3의 소수 표현 // 소수점 이하 최대 자릿수까지 출력 std::cout 주의사항최대 20자리까지 출력할 수 있지만, 자료형에 따라 정확도가 달라진다. (double 자료형의 정확도는 15 ~ 16자리까지이다) [C++] std::string std::string 생성 // 문자열을 초기화하여 생성 std::string str1 = "Hello, world!";// 빈 문자열 생성std::string str2;std::string str22{};std::string str222 = {};// 문자 배열로부터 생성char char_array[] = {'H', 'e', 'l', 'l', 'o'};std::string str3(char_array);// 특정 문자 반복하여 생성std::string str4(5, 'X'); // "XXXXX".std::string 문자열 길이 구하기std::string str = "Hello, world!";std::cout std::string 인덱스를 사용한 문자 접근std::string str = "Hell.. [C++11] thread thread를 표준 라이브러리로 지원한다. 1. 일반 함수를 스레드 함수로 사용#include #include // 스레드에서 실행될 함수void threadFunction(int id) { std::cout 2. 클래스 멤버 함수를 스레드 함수로 사용#include #include class MyClass {public: void threadFunction(int id) { std::cout 3. 클래스 멤버 함수 내에서, 자기 자신을 객체(this)로 전달하여 스레드 생성#include #include class MyClass {public: void threadFunction(int id) { std::cout [C++17] filesystem 파일시스템 관련 C++ 표준 함수#include #include #include // C++17 이상에서 지원됨namespace fs = std::filesystem;int main() { // 파일 생성 fs::path filePath = "example.txt"; std::ofstream outputFile(filePath); // 파일스트림을 이용해 파일 생성 outputFile [C++11] constexpr 컴파일 시간에 평가되는 상수 표현식을 지정하는데 사용한다.즉, 컴파일 과정에서 구문의 오류를 확인할 수 있다. constexpr int size = 10; // 변수 size는 컴파일 시간에 상수로 평가될 수 있음int array[size]; // 배열의 크기로 constexpr 변수를 사용할 수 있음 함수에 사용하면, 역시 컴파일 과정에서 오류를 확인할 수 있다.#include constexpr int square(int x) { return x * x;}int main() { constexpr int result = square(5); // 함수 호출을 포함한 constexpr 변수 초기화 std::cout 장점1. 성능 향상2. 에러 예방 [C++11] nullptr 포인터를 초기화 할 때 사용할 수 있다.int* ptr = nullptr; // int를 가리키는 포인터를 널 포인터로 초기화if (ptr == nullptr) { // ptr이 널일 때 처리할 내용} 이전 1 2 다음