我用了this approach为字符串创建不区分大小写的 typedef。现在,我正在尝试将 std::string
转换为 ci_string
。以下所有抛出编译器错误:
std::string s {"a"};
ci_string cis {s};
ci_string cis (s);
ci_string cis {(ci_string)s};
ci_string cis ((ci_string)s);
ci_string cis = s;
我花了一些时间试图找出如何重载 =
运算符,并且我尝试使用 static_cast
和 dynamic_cast
但没有成功。我该怎么做?
最佳答案
您的两种类型不同,因此您不能将构造函数与常规 std::string
一起使用。但是您的字符串仍然能够复制 C 字符串,所以这应该有效:
std::string s{"a"};
ci_string cis{ s.data() }; // or s.c_str(), they are the same
关于c++ - 将 std::string 转换为 ci_string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722743/