c++ 对象类型识别
在我们使用泛型编程时候,需要针对不同类型的对象,采取不同的操作,如何判断类型是不是你所想的那个类型?
int main(int args, char* argv[])
{
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
//cin.tie(nullptr);
//auto start = std::chrono::steady_clock::now();
//auto end = std::chrono::steady_clock::now();
//std::chrono::duration<double> diff = end - start;
//std::cout << diff.count() << " s\n";
map<std::type_index, string> a,b;
if (typeid(a).hash_code() == typeid(b).hash_code())
cout << "fasdasdf";
//typeid(a).hash_code() == typeid(b).hash_code() better than typeid(a).name() == typeid(b).hash_name() better than return type string.
//better than typeid(a)==typeid(b)
//please use != ,it removed on c++ 20
a[type_index(typeid(int))] = "int";
return 0;
}
最好使用散列值hash_code(),其他==操作c++并不保证同一类型对象==类型比较时候是否 true

浙公网安备 33010602011771号