# はじめましての標準ライブラリたちPart2 ## std::clamp [std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp)はなにかを区間に収める関数。 ```cpp= template< class T, class Compare > constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); ``` 1つ目の引数`v`を2つ目と3つ目の引数`lo`~`hi`の間に収める。`comp`でその比較演算を定義できるし、しなくてもデフォルトの比較演算を使う。 ## std::in_place_t オーバーロードのための空クラス。 C++17から導入された。 ```cpp= // optionalの中でstd::string型のオブジェクトを生成することになる。 // そのコンストラクタ引数として3と'A'を指定している。 std::optional<std::string> p{std::in_place, 3, 'A'}; ``` ## 範囲for文 有名な構文ではじめましてではないが、なんとなく使っているので確認してみる。 以下のような構文を指す。 ```cpp= for (const auto& e : v) { Get(e.value); } ``` 中身は以下のように展開されている。 `for ( for-range-declaration : for-range-initializer ) statement`に対して。 ```cpp= { auto && __range = for-range-initializer; auto __begin = begin-expr; auto __end = end-expr; for ( ; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } } ``` beginとendは異なる方でもOK。 begin()とend()があり、イテレータにoperator++ があるなら使えるらしい。 ## std::isnan, std::isinf [isnan](https://cpprefjp.github.io/reference/cmath/isnan.html)は引数の値がNaNかを判定するだけ。 [isinf](https://cpprefjp.github.io/reference/cmath/isinf.html)はinfinityかを判定する。 ## std::modf [std::modf](https://en.cppreference.com/w/cpp/numeric/math/modf)は浮動小数点を整数部と小数部に分解する。 ```cpp= float x = 1.23f; float integral_part = 0; float fractional_part = std::modf(x, &integral_part); ``` このとき`integral_part`は1で`fractional_part`は0.23。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up