5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

学生にありがちな下限、上限のif文の改善

Last updated at Posted at 2019-10-17

#学生にありがちな下限、上限のif文#

こんばんは。今回使うのはstd::min とstd::maxです
便利なのでソースを綺麗にしたい人はやってみてほしいです

##std::minとは

before
    if(Num>100){Num=100;}

std::minを使うと

after
Num=std::min(Num,100);

このようにif文を書かないで済みます。

std::min関数は2つの引数のうち小さいほうの値を返す関数です
##std::maxとは

before
     Num--;
    if(Num<0){Num=0;}

std::maxを使うと

after
  Num--;
Num=std::max(Num,0);

std::max関数は2つの引数のうち大きいほうの値を返す関数です

##少しでもif文を減らす
パット来ないかもしれないけれどこれから先便利にあるので知らない人は片隅にでも覚えててほしい:grin:

5
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?