Skip to content

0x342 C++

Abstraction

Standard Library

IO

iostream

the only way to pass or return stream is by non-const reference

  • stream cannot be copyied or assigned
  • stream might change state (so non-const)
istream& read(istream& is, int hogehoge);

each stream has a iostate, which indicates the state of the current stream, it can be

  • good: valid state
  • eof: get eof
  • fail: recoverable failure (e.g: cin string to a int type)
  • bad: stream is corrupt somehow

The state can be modified by users

When it is in good state, the stream is evaled as true, for example, to continuously read until the eof

string line, word;

while(getline(cin, line)){
    dosomething();
}

while(cin >> word){
    dosomething();
}

fstream

stringstream

Concurrency

std::thread: it needs to join or detach. use RAII to make sure it will be handled correctly. std::mutex: RAII with std::lock_guard or std::unique_lock(can lock multiple mutex and unlock) std::recursive_mutex: allow a thread to acquire a mutex multiple times without deadlock

Algorithm

sort: looks optimized using different sort (insertion <-> quick) when length is short or long Standardization

STL Container

not thread-safe

Sequential Container

Associative Container

C++ Standards

C++11 (C++0x)

lambda expression

C++14

C++17

C++20

Generic Programming:

concept, constraint

C++23 ?!

Reference

[1] Bjarne Stroustrup's C++ Programming Language 3rd edition

[2] llvm libc++ implementation

[3] A History of C++ by Bjarne Stroustrup

[4] C++ standard

[5] C++ Primer