2014-01-15 64 views
-5

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

  • 一个 - 真正
  • b - 为true
  • Ç - 假
  • 的R - 真

有什么简单的方法就像在C#中的字典?

+3

也许你可以使用std :: map ? –

+0

你的术语有点令人困惑。为char赋值是指char c; c ='x';'。 – juanchopanza

+0

@juanchopanza:我也这么认为。演示的用例和“词典”这个词的使用使我相信他正在寻找某种联想容器。虽然只是一个猜测。 –

回答

2

最接近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 
+0

@sftrabbit:编辑历史记录表明您删除了我的链接,但链接仍处于活动状态。你真的改变了什么? –

+0

你有链接的代码引用,所以它不能正常工作。我把它们放进去。 –

+0

@sftrabbit:啊,谢谢你! –