C++ 字符串类型的转换

C++ 字符串类型的转换,c++,c++builder,C++,C++builder,C++中有很多字符串类型:WideString、UnicodeString、string、WSString、string、AnsiString、Variant 在我的代码中有很多转换,比如 WideString s1 = UnicodeString ( wstring(s2.str().c_str()).c_str()).c_str(); 一句话就是:令人困惑 有没有一种简单的方法来处理一个助手类的所有字符串转换,而不再考虑如何将一种字符串类型转换为另一种类型,如: s1 = sc(s2);

C++中有很多字符串类型:WideString、UnicodeString、string、WSString、string、AnsiString、Variant

在我的代码中有很多转换,比如

WideString s1 = UnicodeString ( wstring(s2.str().c_str()).c_str()).c_str();
一句话就是:令人困惑

有没有一种简单的方法来处理一个助手类的所有字符串转换,而不再考虑如何将一种字符串类型转换为另一种类型,如:

s1 = sc(s2);    // sc = string-converter

通过所有可能的转换,打造您自己的

在模板中,您应该使用类型安全强制转换,例如

那只是一次让人困惑

编辑

可能是这样的:

template <typename T>
T convert(T x)
{
  T value;
  value = static_cast<T>(x);
  return value;
}
string  s = convert<wstring,string> (wstring(L"hello"));
模板
T转换(tx)
{
T值;
值=静态(x);
返回值;
}

(我没有进入实现,因为这些是我不知道的变量类型。这是我尝试的方法)

我大部分是老式的C89-C99用户,因此我可以建议使用一些宏。当然,正如alredy所建议的那样,模板将更加优雅和安全(因为编译器将跟踪错误),但只需做出选择

#定义CONV_TO_WSTRING(s1)(WSTRING(s1.str().c_str()).c_str()).c_str()

比如说

那么在使用中,您有:


s1=CONV-TO-WSTRING(s2)

创建一个将处理所有可能转换的包装类。你可以用 模板类,但请注意,您将不得不创建其他模板 专门化,因为这些转换中的大多数不能用 通用方式。例如,您可以轻松地从
WideString
转换为
UnicodeString
使用强制转换,但不能简单地将
wstring
强制转换为
string
。对于那些 可以铸造,可以使用通用零件:

template <class TypeFrom, class TypeTo>
TypeTo convert(const TypeFrom &from)
{
    return static_cast<TypeTo>(from);
}
这里不可能列出所有的组合,你必须注意这一点。在这里 是可以帮助进行某些转换的链接:


经过一些实验后,我决定使用重载函数

如果使用模板,我的代码如下所示:

template <typename T>
T convert(T x)
{
  T value;
  value = static_cast<T>(x);
  return value;
}
string  s = convert<wstring,string> (wstring(L"hello"));
我的代码看起来是这样的:

string str2 = convert(wstring(L"hello"));

这就是我喜欢的方式:永远不要重复我自己。懒惰。尽量简单^^

这是我目前对转换问题的解决方案

std::string convert1(const std::wstring &from) {
  return std::string(from.begin(), from.end()); }

std::string convert1(const UnicodeString &from) {        // String = UnicodeString
  wstring strTemp = from.c_str();
  return std::string(strTemp.begin(), strTemp.end()); }

std::string convert1(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::string(strTemp.begin(), strTemp.end()); }


std::wstring convert2(const std::string &from) {
  return std::wstring(from.begin(), from.end()); }

std::wstring convert2(const UnicodeString &from) {
  wstring strTemp = from.c_str();
  return std::wstring(strTemp.begin(), strTemp.end()); }

std::wstring convert2(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::wstring(strTemp.begin(), strTemp.end()); }


UnicodeString convert3(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return UnicodeString(strTemp.c_str()); }

UnicodeString convert3(const std::wstring &from) {
  return UnicodeString(from.c_str()); }

UnicodeString convert3(const WideString &from) {
  return UnicodeString(from.c_bstr()); }


WideString convert4(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return WideString(strTemp.c_str()); }

WideString convert4(const std::wstring &from) {
  return WideString(from.c_str()); }

WideString convert4(const UnicodeString &from) {
  return WideString(from.c_str()); }



wchar_t* convert5(const std::string &from) {
  static wstring strTemp;
  strTemp.clear();
  strTemp.assign(from.begin(), from.end());
  return (wchar_t*) strTemp.c_str(); }

wchar_t* convert5(const std::wstring &from) {
  return (wchar_t*) from.c_str(); }

wchar_t* convert5(const UnicodeString &from) {
  return from.c_str(); }

wchar_t* convert5(const WideString &from) {
  return from.c_bstr(); }
用法:

  std::string   s1  = convert1 (wstring       (L"Hallo1" ));
  std::string   s2  = convert1 (UnicodeString (L"Hallo2" ));
  std::string   s3  = convert1 (WideString    (L"Hallo3" ));
  std::wstring  s4  = convert2 (std::string   ( "Hallo4" ));
  std::wstring  s5  = convert2 (UnicodeString (L"Hallo5" ));
  std::wstring  s6  = convert2 (WideString    (L"Hallo6" ));
  UnicodeString s7  = convert3 (std::string   ( "Hallo7" ));
  UnicodeString s8  = convert3 (std::wstring  (L"Hallo8" ));
  UnicodeString s9  = convert3 (WideString    (L"Hallo9" ));
  WideString    s10 = convert4 (std::string   ( "Hallo10"));
  WideString    s11 = convert4 (std::wstring  (L"Hallo11"));
  WideString    s12 = convert4 (UnicodeString (L"Hallo12"));

  Memo->Lines->Add(convert5(s1));
  Memo->Lines->Add(convert5(s2));
  Memo->Lines->Add(convert5(s3));
  Memo->Lines->Add(convert5(s4));
  Memo->Lines->Add(convert5(s5));
  Memo->Lines->Add(convert5(s6));
  Memo->Lines->Add(convert5(s7));
  Memo->Lines->Add(convert5(s8));
  Memo->Lines->Add(convert5(s9));
  Memo->Lines->Add(convert5(s10));
  Memo->Lines->Add(convert5(s11));
  Memo->Lines->Add(convert5(s12));
结果:

Hallo1
Hallo2
Hallo3
Hallo4
Hallo5
Hallo6
Hallo7
Hallo8
Hallo9
Hallo10
Hallo11
Hallo12

请解释什么是宽字符串、单字符串、反字符串和字符串。这些当然不是C++标准库中的类。可能是C++/CLI?另外,变体是WiAPI的一种类型,不是C++。我使用C++ Builder XE7。您是否试图在接受用户回答之前,以用户重新建议的方式创建转换函数模板?他的实现只有一个模板参数,他实际上是建议您将类型强制转换为自身(从T到T)?!我注意到了这个错误。错误对我来说并不重要,重要的是方法。他给我指明了正确的方向,这就是解决方案或灵感。剩下的是我自己的幻想和发散。对不起打扰你了。我知道这对你来说没那么重要。然而,一些经验比你少的人会看到这个问题,认为这是公认的答案,然后想知道为什么它不起作用。这就是我提到它的原因。这个答案的质量很差。大多数转换无法通过执行强制转换来执行。请用更多关于如何实现这个类的信息更新答案。我同意你的观点。正如问题所在,这篇文章的质量很差。为了更详细地展示如何做到这一点,我需要获得更多关于上面使用的类型的信息哦,不,重载函数有一个限制!“字符f1(双精度);字符串f1(双精度);”是不允许的“用另一种类型重新克拉定f1”-发生错误!这些转换函数有一个问题,它们在向下转换时会丢失信息。考虑你是否想在你的代码中使用UTF-8:STD::String Syes,如果存储的文本有大量的字节,你是对的。我只是想我可以用一组文本流来扩展我的函数…^顺便说一下,我把名字从“转换”缩短为“SC”(字符串转换)。UTF-8是一个分离的故事…我发现这个网站可以处理UTF-8编码的文本:哦,很高兴知道!再次感谢你^^
Hallo1
Hallo2
Hallo3
Hallo4
Hallo5
Hallo6
Hallo7
Hallo8
Hallo9
Hallo10
Hallo11
Hallo12