본문 바로가기

C++22

[C++] Header-only Library Header-only Library 라이브러리를 구성하는 매크로, 함수, 클래스 등이 모두 헤더 파일 안에서 정의되어 컴파일러에게 보여지는 라이브러리 Header-only Library의 장점 추상화(구현의 구체적인 사항을 사용자에게로부터 숨김)하여 헤더파일+라이브러리(.dll, .lib, .dylib 등)의 구성으로 배포하는 일반 라이브러리와 달리, 운영체제별 컴파일 또는 복잡한 경로설정이 필요 없다. 빌드할 필요 없이 파일을 코드에 포함시키기만 하면 된다. 따라서 멀티플랫폼 지원이 가능하다. + 라이브러리가 템플릿 코드인 경우 header-only 형태로 구현/배포하는 것이 필수적이다. Header-only Library의 단점 - 라이브러리를 수정할 경우 해당 라이브러리를 사용하는 모든 컴파일 단위.. 2022. 3. 31.
[C++] C++ 11/14/17/... New STL (ENG) Unordered Map std::unordered_map std::unordered_multimap // std::map #include #include #include int main() { std::map scores; scores["Nana"] = 60; scores["Mocha"] = 70; scores["Coco"] = 100; for (auto it = scores.begin(); it != scores.end(); ++it) { std::cout first 2022. 1. 2.
[C++] C++ 11/14/17/... New Types (ENG) nullptr The old C style of NULL is a number. // Class.h float GetScore(const char* name); // meant to call this float GetScore(int id); // actual // Main.cpp #define NULL 0 Class* myClass = new Class("COMP3100"); // ... int score = myClass->GetScore(NULL); So in C++ 11, you can use nullptr as a pointer. typedef decltype(nullptr) nullptr_t; // null pointer constant int number = NULL; // OK int* p.. 2022. 1. 2.
[C++] C++ 11/14/17/... New Keywords (ENG) auto The keyword auto infers the data type automatically. It is not a dynamic type (like var of JavaScript). So you need to initialize auto's variables. auto x; // error auto x = "Chris"; // OK auto x = 50; // error It can get values, pointers, and even references (and so on). auto for pointers can omit *, but it's better to use * for readability of programmers. Reference types must use & so tha.. 2022. 1. 2.
[C++] Template Programming (ENG) Function Template STL container is also a template. With template, you don't have to write codes in duplicate. template // typename can be replaced with class (even if it is not a class) T Add(T a, T b) { return a + b; } Function template is a template for functions. You can skip the template parameter. Add (3,10); Add (3,10); There's not the difference between typename and class, so I'll use ty.. 2022. 1. 2.
[C++] Purpose of STL Container (ENG) the Purpose STL container has a standard interface that can be applied to every container. - It is a little weird. Extremely object-oriented. std::vector scores; scores.push_back(10); // seems like a stack std::list ages; ages.push_back(100); // seems like a stack std algorithms work in many containers. It is based on template programming. It allows the memory to be menaged automatically. - Freq.. 2022. 1. 2.
[C++] Standard Template Library Container: Others (ENG) Set #include int main() { std::set scores; scores.insert(20); scores.insert(100); for (std::set::iterator it = scores.begin(); it != scores.end(); ++it) { std::cout 2022. 1. 2.
[C++] Standard Template Library Container: Map (ENG) Creating Map A map stores pairs of key and value. Keys cannot be duplicated. C++ maps are automatically sorted. It is based on the binary search tree (ascending order). #include int main() { std::map simpleScoreMap; simpleScoreMap.insert(std::pair("Mocha", 100)); simpleScoreMap.insert(std::pair("Coco", 50)); simpleScoreMap["Mocha"] = 0; std::cout 2022. 1. 2.
[C++] Standard Template Library Container: Vector (ENG) STL Container vector, map, set, stack, queue, list, deque ... STL container is a standard interface for all template-based containers. It allows for automatic memory management. Creating Vectors Vector is a dynamic array that can hold any data type. All elements in a vector are located in contiguous memory. Memory is automatically managed as the number of elements increase. You can randomly acce.. 2022. 1. 2.