直接匹配字符串是不行的,C++中case只可以匹配a constant expression of the same type as the type of condition after conversions and integral promotions,所以在这里我需要把字符串转换为一个字面值整数从而进行case匹配。
// We provide appropriate hash functions so StringPiece and StringPiece16 can // be used as keys in hash sets and maps.
// This hash function is copied from base/strings/string16.h. We don't use the // ones already defined for string and string16 directly because it would // require the string constructors to be called, which we don't want. #define HASH_STRING_PIECE(StringPieceType, string_piece) \ std::size_t result = 0; \ for (StringPieceType::const_iterator i = string_piece.begin(); \ i != string_piece.end(); ++i) \ result = (result * 131) + *i; \ return result;
intmain(int argc,char* argv[]) { cout<<"switch list in \"123\"/\"456\"/\"789\",Please input:"; string input; cin>>input; switch(CALC_STRING_HASH(input)){ case"123"_HASH:{ cout<<"the case is 123"<<endl; break; } case"456"_HASH:{ cout<<"the case is 456"<<endl; break; } case"789"_HASH:{ cout<<"the case is 789"<<endl; break; } default: cout<<"Not found"<<endl; break;