将值分配给字符

时间:2014-01-15 16:21:05

标签: c++ dictionary

C ++中有一种简单的方法可以为char赋值吗?我想得到字符列表,并将它们连接到一个值,例如:

  • a - true
  • b - true
  • c - false
  • r - true

有没有简单的方法,就像C#中的字典一样?

1 个答案:

答案 0 :(得分:2)

与“Dictionary”类型对象最接近的C ++类似物是std::map

#include <map>

int main()
{
  std::map <char, bool> myDictionary;
  myDictionary ['a'] = true;
  myDictionary ['b'] = true;
  myDictionary ['r'] = false;
}

您可以搜索项目的存在:

[C ++ 11]

auto it = myDictionary.find ('x');
assert (it == myDictionary.end()); // not found

it = myDictionary.find ('a');
assert (it != myDictionary.end());  // found
assert (it->second == true); // assert that a is mapped to true