A Proposal toAdd an Extensible Random
Number Facility to the Standard Library
(Revision 2) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html
const auto x= 5; // const int
vector<vector<int>> vv(10);
auto a = vv[0]; // vector<int>
auto& b = vv[0]; // vector<int>&
const auto& c = vv[0]; // const vector<int>&
auto [4/4]
const 修飾や参照の宣言
struct Team {
enum{ Red, White };
};
struct Palette {
enum { Red, Green, Blue };
};
従来の enum の問題点 [2/2]
名前の衝突を防ぐために
スコープを作る必要がある
78.
enum class name: type {
enumerator = constexpr,
enumerator = constexpr, ...
};
enum struct name : type {
enumerator = constexpr,
enumerator = constexpr, ...
};
enum class [1/4]
強い型付けとスコープを持つ列挙型
79.
enum class Team{ Red, White };
enum class Month {
January = 1, February, ...
};
enum class Align : char {
Left = 'L',
Center = 'C',
Right = 'R',
};
enum class [2/4]
80.
enum class Team{ Red, White };
enum class Palette { Red, Green, Blue };
const Team team = Team::Red;
switch (palette) {
case Palette::Red : break;
case Palette::Green : ...
}
enum class [3/4]
列挙型名のスコープを持つ
81.
void SetColor(unsigned);
enum classTeam { Red, White };
const unsigned red = 0xffFF0000;
SetColor(red);
SetColor(Team::Red); // error
const int white = Team::White; // error
enum class [4/4]
整数型へ暗黙的に変換されない
82.
enum class はenum の強化版で、強い型付
けとスコープを持つ
enum class を使えば、整数型への暗黙的な
変換や、列挙子の名前の衝突を防げる
復習
const int a= 5, b = 10, c = 50, d = 20;
const int smallest
= min(min(min(a,b),c),d); // 5
const int largest
= max(max(max(a,b),c),d); // 50
最小/最大値の取得
3 つ以上の変数に対しては
何重もの min() / max() が必要
131.
const int a= 5, b = 10, c = 50, d = 20;
const int smallest
= min({ a, b, c, d }); // 5
const int largest
= max({ a, b, c, d }); // 50
最小/最大値の取得
initializer-list で複数の値を渡す
132.
int a =25, b = 30;
const int smaller = min(a,b); // 25
const int larger = max(a,b); // 30
最小/最大値を同時に取得
min() と max() で比較演算が 2 回必要
133.
int a =25, b = 30;
// results は pair<int,int> 型
const auto results = minmax(a,b);
const int smaller = results.first; // 25
const int larger = results.second; // 30
最小/最大値を同時に取得
minmax() で比較演算が 1 回に
134.
int a[100]; //{0,1,2,3,...} で初期化したい
vector<int> v(50); // {1000,1001,...}
for (int i=0; i<100; ++i)
a[i] = i;
for (size_t i=0; i<v.size(); ++i)
v[i] = i + 1000;
配列に連続した値を代入
ループが必要