C++ forward
·
C++/Library
forward 라이브러리 예제 간단 forward를 사용하지 않았을 경우 #include struct MyStruct {}; void func(MyStruct& s) { std::cout
C++ future
·
C++/Library
future 라이브러리 라이브러리에 라이브러리가 포함되어 있다. #include #include #include #include #include #include #include #include #include 예제 쓰레드 예제와 이를 future, async로 적용한 예제 #include #include int main() { using namespace std; { int result; thread t([&] { result = 1 + 2; }); t.join(); cout
C++ 멀티쓰레딩 (Multithreading)
·
C++/Library
멀티쓰레딩 (Multithreading) 라이브러리 병렬 프로그래밍을 위한 라이브러리이다. 쓰레드를 생성하여 다른 코어에서 작업할 수 있도록 한다. 예제 CPU 사용률 100% 찍어보기 출력이 섞여서 나오는 현상(레이스 컨디션)이 발생한다. 이는 mutex나 atomic, 세마포어 등으로 해결할 수 있다. #include #include #include #include int main() { using namespace std; const int num_logical_processors = std::thread::hardware_concurrency(); cout
C++ atomic
·
C++/Library
atomic 라이브러리 보장하고 싶은 변수를 atomic 형식으로 정의하면 된다. 성능이 느려진다. 성능을 위해 mutex를 사용하는 것 같다. 예제 atomic을 사용하지 않은 예제 #include #include #include int main() { using namespace std; int shared_memory(0); auto count_func = [&]() { for (int i = 0; i < 1000; ++i) { this_thread::sleep_for(chrono::milliseconds(1)); shared_memory++; } }; thread t1 = thread(count_func); thread t2 = thread(count_func); t1.join(); t2.join..
C++ mutex
·
C++/Library
mutex 라이브러리 Mutual Exclusion의 약자이다. 예제 mutex를 적용하지 않은 예제 출력이 섞여서 나오는 것을 볼 수 있다. #include #include #include int main() { using namespace std; const int num_logical_processors = std::thread::hardware_concurrency(); cout
C++ 파일 임의 위치 접근
·
C++/Library
파일 임의 위치 접근 라이브러리 예제 ifs.seekg() 함수는 첫 번째 인자(Offset)를 받아서 그만큼 이동한다. 두 번째 인자로 ios::cur(current)를 주면, 현재의 위치를 기준으로 오프셋만큼 이동한다. 이외에도 ios::beg(begin), ios::end를 넣을 수 있다. ifs.get() 함수는 커서 위치에서 1바이트를 읽고 반환한다. 위치는 1바이트 뒤로 이동된다. ifs.tellg() 함수는 현재 위치를 반환한다. #include #include #include #include int main() { using namespace std; const string filename = "my_file.txt"; { ofstream ofs(filename); // abcdefghij..
C++ fstream
·
C++/Library
fstream 라이브러리 기본 옵션은 ASCII 포맷으로 덮어쓰기이다. 옵션에 대한 플래그는 다음과 같다. xiosbase static constexpr _Openmode in = static_cast(0x01); static constexpr _Openmode out = static_cast(0x02); static constexpr _Openmode ate = static_cast(0x04); static constexpr _Openmode app = static_cast(0x08); static constexpr _Openmode trunc = static_cast(0x10); static constexpr _Openmode _Nocreate = static_cast(0x40); static con..
C++ 정규 표현식 (Regular Expressions)
·
C++/Library
정규 표현식 (Regular Expressions) 라이브러리 http://www.cplusplus.com/reference/regex/ ECMAScript Grammar 사용 http://www.cplusplus.com/reference/regex/ECMAScript/ 기본 예제 #include #include int main() { using namespace std; regex re("\\d"); //regex re("[ab]"); //regex re("[[:digit:]]{3}"); //regex re("[A-Z]+"); //regex re("[A-Z]{3}"); //regex re("[A-Z]{1, 5}"); //regex re("([0-9]{1})([-]?)([0-9]{1,4})"); //re..
C++ 흐름 상태 (Stream States)
·
C++/Library
흐름 상태 (Stream States) 흐름 상태 출력 예제 오류 상태 플래그를 통해 입력이 잘 들어갔는지에 대한 결과를 확인할 수 있다. #include #include #include #include void printCharacterClassification(const int& i) { using namespace std; cout
C++ cout
·
C++/Library
cout 라이브러리 출력 형식 설정 여러가지 방법으로 출력 형식을 설정할 수 있다. +부호 붙이기, 16진수 출력, 대문자 출력 #include int main() { using namespace std; cout.setf(std::ios::showpos); cout
C++ ostream
·
C++/Library
ostream cout
C++ istream
·
C++/Library
istream 입력 범위 제한 setw() 함수 라이브러리 ```c++ #include #include int main() { using namespace std; char buf[5]; cin >> setw(5) >> buf; cout setw(5) >> buf; cout
C++ wstring
·
C++/Library
wstring 라이브러리 일반적인 char의 범위를 넘어가는 유니코드 등의 글자를 저장할 때 사용되는 클래스이다. #include #include #include int main() { using namespace std; const wstring texts[] = { L"Hello", // English L"안녕하세요", // Korean L"Ñá", // Spanish L"forêt intérêt", // French L"Gesäß", // German L"取消波蘇日奇諾", // Chinese L"日本人のビット", // Japanese L"немного русский", // Russian L"ένα κομμάτι της ελληνικής", // Greek L"ਯੂਨਾਨੀ ਦੀ ਇੱਕ ਬਿੱਟ", ..
C++ sstream
·
C++/Library
sstream 라이브러리 stringstream의 약자이다. : extraction operator 상속 관계 istringstream, ostringstream, stringstream은 비슷하지만 약간 다른 부분이 존재한다. istringstream에 연산자는 정의되어있지 않다. using istringstream = basic_istringstream; using ostringstream = basic_ostringstream; using stringstream = basic_stringstream; // CLASS TEMPLATE basic_istringstream template class basic_istringstream : public basic_istream { // input strea..
C++ string
·
C++/Library
string 라이브러리 basic_string string, wstring 등의 문자열 클래스는 다음과 같이 basic_string 클래스 템플릿이 인스턴스화된 것이다. using string = basic_string; using wstring = basic_string; #ifdef __cpp_lib_char8_t using u8string = basic_string; #endif // __cpp_lib_char8_t using u16string = basic_string; using u32string = basic_string; 예제 #include #include #include int main() { using namespace std; const char* my_string = "my stri..
C++ STL 알고리즘 (Algorithms)
·
C++/Library
STL 알고리즘 (Algorithms) 예제 클래스를 원소로 넣을 경우 대부분 비교 연산자 오버로딩을 해놔야 한다. #include #include #include int main() { using namespace std; vector container; for (int i = 0; i < 10; ++i) container.push_back(i); auto itr = min_element(container.begin(), container.end()); cout
C++ STL 반복자 (Iterators)
·
C++/Library
STL 반복자 (Iterators) STL 컨테이너의 멤버들에 접근할 때 공통적인 방법으로 접근할 수 있도록 해주는 것이다. iterator와 const_iterator는 사용할 때 크게 차이가 없는 듯하다. 타이핑이 귀찮아서 보통 auto 키워드를 많이 사용하는 것 같다. end() 함수는 맨 마지막 원소의 다음 주소를 가리킨다. STL의 algorithm 함수들이 iterator로 first와 last를 받을 경우(처음과 끝), 마지막은 보통 end()가 오는 것으로 생각하여 포함시키지 않는다. 즉 [first, last)라고 생각하면 된다. 참고 : https://modoocode.com/256 vector 예제 원소를 전부 순회하려면 container.begin()부터 container.end()..
C++ multimap
·
C++/Library
multimap 라이브러리 예제 key가 중복이 될 수 있는 map이다. #include #include int main() { using namespace std; multimap map; map.insert(std::pair(&#39;a&#39;, 10)); map.insert(std::pair(&#39;c&#39;, 50)); map.insert(std::pair(&#39;b&#39;, 20)); map.insert(std::pair(&#39;a&#39;, 100)); cout
C++ map
·
C++/Library
map 라이브러리 예제 key-value 쌍으로 이루어진 클래스이다. first는 key를, second는 value를 가리킨다. 자료를 저장할 때 오름차순으로 정렬한다. #include #include int main() { using namespace std; map map; map[&#39;c&#39;] = 50; map[&#39;a&#39;] = 10; map[&#39;b&#39;] = 20; cout
C++ multiset
·
C++/Library
multiset 라이브러리 기본 예제 중복을 허용하는 set이다. #include #include int main() { using namespace std; multiset str_set; str_set.insert("Hello"); str_set.insert("World"); str_set.insert("Hello"); cout
C++ set
·
C++/Library
set 라이브러리 예제 원소가 중복되지 않는다. #include #include int main() { using namespace std; set str_set; str_set.insert("Hello"); str_set.insert("World"); str_set.insert("Hello"); cout
C++ Associative Containers
·
C++/Library
Associative Containers set multiset map multimap
C++ Priority queue
·
C++/Library
Priority queue 라이브러리 기본 예제 원소 간에 우선순위가 존재한다. 템플릿으로 클래스를 사용하려면 비교 연산자 오버로딩을 해야한다. #include #include int main() { using namespace std; priority_queue queue; for (const int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2}) queue.push(n); for (int i = 0; i < 10; ++i) { cout
C++ queue
·
C++/Library
queue 라이브러리 기본 예제 pop()으로 맨 앞의 원소를 제거한다. #include #include int main() { using namespace std; queue queue; queue.push(1); queue.push(2); queue.push(3); cout
C++ stack
·
C++/Library
stack 라이브러리 기본 예제 pop()으로 맨 위의 원소를 제거한다. #include #include int main() { using namespace std; stack stack; stack.push(1); stack.emplace(2); stack.emplace(3); cout
C++ Container Adaptors
·
C++/Library
Container Adaptors stack queue priority queue
C++ deque (double-ended queue)
·
C++/Library
deque (double-ended queue) 라이브러리 기본 예제 vector와 비슷하다. push_front로 앞에서부터 추가할 수도 있다. #include #include int main() { using namespace std; deque deq; for (int i = 0; i < 10; ++i) { deq.push_front(i); deq.push_back(i); } for (auto& e : deq) cout
C++ Sequences Containers
·
C++/Library
Sequences Containers vector deque (double-ended queue) list slist
C++ STL 컨테이너 (STL Containers)
·
C++/Library
STL 컨테이너 (STL Containers) Simple Containers pair Sequences Containers Container Adaptors Associative Containers Other types of Containers bitset valarray 참고 https://en.wikipedia.org/wiki/Standard_Template_Library https://en.cppreference.com/w/cpp/container
C++ 표준 템플릿 라이브러리 (STL, Standard Template Libraries)
·
C++/Library
표준 템플릿 라이브러리 (STL, Standard Template Libraries) Standard Library와 STL의 차이 STL은 아래 4가지로 구성되어있고, 나머지는 그냥 Standard Library이다. STL 컨테이너 (Containers) STL 반복자 (Iterators) STL 알고리즘 (Algorithms) Functions