【发布时间】:2011-01-23 04:33:58
【问题描述】:
比较std::strings 的最佳方法是什么?显而易见的方法是使用if/else:
std::string input;
std::cin >> input;
if ( input == "blahblahblah" )
{
// do something.
}
else if ( input == "blahblah" )
{
// do something else.
}
else if ( input == "blah" )
{
// do something else yet.
}
// etc. etc. etc.
另一种可能性是使用std::map 和switch/case。在进行大量(例如 8、10、12 次以上)这些比较时,最好的方法是什么?
【问题讨论】:
-
是的,只需使用从字符串到函数的映射。
-
@Ben 你能发布一个例子作为答案吗?
标签: c++ string comparison io string-comparison