Skip to main content

要素の取得(value-for-key)

Python

py
dict1['name1']

Ruby

rb
dict1["name1"]

キーがシンボルのときはこう

rb
dict1[:name1]

PowerShell

powershell
$dict1["name1"]

JavaScript 風に、こうも行ける。

powershell
$dict1.name1

C++

cpp
if (map1.count(k) > 0) {
val = map1[k];
}

C++ の map は [] でアクセスしに行くと、値を作りに行ってしまうため、find か count で値チェックしてから取る

cpp
// 1
map<string, int>::const_iterator it = map1.find(k);
if (it != map1.end()) {
val = it->second;
}

// 2
if (map1.count(k) > 0) {
val = map1[k];
}