Python辞書をC++に変換するにはどうすればよいですか?
Python辞書はハッシュマップです。 C ++のマップデータ構造を使用して、Pythondictの動作を模倣できます。次のようにC++でマップを使用できます。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Initializer_list constructor */
map<char, int> m1 = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5}
};
cout << "Map contains following elements" << endl;
for (auto it = m1.begin(); it != m1.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
} これにより、出力が得られます
The map contains following elements a = 1 b = 2 c = 3 d = 4 e = 5
このマップはpythondictと同等であることに注意してください:
m1 = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5
} -
与えられた辞書に従ってPython文字列を翻訳する方法は?
オプションで、テーブル(文字列モジュールのmaketrans()関数で作成)を使用してすべての文字が翻訳された文字列のコピーを返すメソッドtranslate()を使用できます。文字列deletecharsで見つかったすべての文字を削除します。 例 from string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "
-
Python関数から辞書を返すにはどうすればよいですか?
Python関数から辞書を返す方法はいくつもあります。以下に示すものを検討してください。 例 # This function returns a dictionary def foo(): d = dict(); d['str'] = "Tutorialspoint" d['x'] = 50 return d print foo() 出力 {'x': 50