C ++中最常用的字符串类型是什么以及如何在它们之间进行转换?

时间:2022-09-06 18:38:37

OR How to not kill yourself or someone the next time the C++ compiler twists your arm to convert between 2 arbitrary string types just to mess with you?

或者下次C ++编译器扭曲你的手臂以转换为2个任意字符串类型只是为了弄乱你时,如何不杀死自己或某人?

I have a tough time coding in C++ since I am used to VB6, C#, Ruby, for string operations. But now I've spent over 30 mins trying to log a string containing 2 guids and a string to the debug window... and it isn't getting any easier And I've already met RPC_WSTR, std::wstring and LPCWSTR

我很难用C ++编写代码,因为我习惯使用VB6,C#,Ruby来进行字符串操作。但是现在我花了超过30分钟试图将包含2个guid和一个字符串的字符串记录到调试窗口......并且它没有变得更容易而且我已经遇到了RPC_WSTR,std :: wstring和LPCWSTR

Are there simple (or any) rules to know the conversions between them? Or does it only come after years of torture?

是否有简单(或任何)规则来了解它们之间的转换?或者它只是经过多年的折磨才来?

Basically I'm looking for the most frequently used string types in both standard APIs and MS-specific/Visual C++ libraries ; What to do the next time, i see

基本上我正在寻找标准API和MS特定/ Visual C ++库中最常用的字符串类型;我明白下次该怎么办

Error   8   error C2664: 'OutputDebugStringW' : cannot convert parameter 1 from 'std::wstring' to 'LPCWSTR'

Update: I fixed the ^^^^ compile error. I am looking for a more global answer not for the solution for specific issue I listed as an example.

更新:我修复了^^^^编译错误。我正在寻找一个更全面的答案,而不是我列举的具体问题的解决方案。

6 个解决方案

#1


There are two built-in string types:

有两种内置字符串类型:

  • C++ strings use the std::string class (std::wstring for wide characters)
  • C ++字符串使用std :: string类(宽字符为std :: wstring)

  • C-style strings are const char pointers const char*) (or const wchar_t*)
  • C风格的字符串是const char指针const char *)(或const wchar_t *)

Both can be used in C++ code. Most API's, including Windows' are written in C, and so they use char pointers rather than the std::string class.

两者都可以在C ++代码中使用。大多数API(包括Windows)都是用C语言编写的,因此它们使用char指针而不是std :: string类。

Microsoft further hides these pointers behind a number of macros.

微软进一步隐藏了许多宏背后的指针。

LPCWSTR is a Long Pointer to a Const Wide String, or in other words, a const wchar_t*.

LPCWSTR是Const宽字符串的长指针,换句话说,是const wchar_t *。

LPSTR is a Long Pointer to String, or in other words, a char* (not const).

LPSTR是String的长指针,换句话说,是char *(不是const)。

They have a handful more, but they should be pretty easy to guess once you know these first few. They also have *TSTR variants, where the T is used to indicate that this might be either regular or wide chars, depending on whether UNICODE is enabled in the project. LPCTSTR resolves to LPCWSTR if UNICODE is defined, and LPCSTR otherwise.

他们还有一些,但是一旦你知道了前几个,它们应该很容易猜到。它们也有* TSTR变体,其中T用于表示这可能是常规字符或宽字符,具体取决于项目中是否启用了UNICODE。如果定义了UNICODE,则LPCTSTR解析为LPCWSTR,否则解析为LPCSTR。

So really, when working with strings, you just need to know the two types I listed at the top. The rest are just macros for various variants of the char pointer version.

所以,在使用字符串时,您只需要知道我在顶部列出的两种类型。其余的只是char指针版本的各种变体的宏。

Converting from a char pointer to a string is simple:

从char指针转换为字符串很简单:

const char* cstr = "hello world";
std::string cppstr = cstr;

And the other way isn't much hader:

而另一种方式并不多:

std::string cppstr("hello world");
const char* cstr = cppstr.c_str();

That is, std::string takes a C-style string as an argument in the constructor. And it has a c_str() member function that returns a C-style string.

也就是说,std :: string在构造函数中将C样式的字符串作为参数。它有一个c_str()成员函数,返回一个C风格的字符串。

Some commonly used libraries define their own string types, and in those cases, you'll have to check the documentation for how they interoperate with the "proper" string classes.

一些常用的库定义了自己的字符串类型,在这些情况下,您必须检查文档中它们如何与“正确的”字符串类进行互操作。

You should usually prefer the C++ std::string class, as, unlike char pointers, they behave as strings. For example:

您通常应该更喜欢C ++ std :: string类,因为与char指针不同,它们表现为字符串。例如:

std:string a = "hello ";
std:string b = "world";
std:string c = a + b; // c now contains "hello world"

const char* a = "hello ";
const char* b = "world";
const char* c = a + b; // error, you can't add two pointers

std:string a = "hello worl";
char b = 'd';
std:string c = a + b; // c now contains "hello world"

const char* a = "hello worl";
char b = 'd';
const char* c = a + b; // Doesn't cause an error, but won't do what you expect either. the char 'd' is converted to an int, and added to the pointer `a`. You're doing pointer arithmetic rather than string manipulation.

#2


Here is a article that covers thing you need mainly for CString

这篇文章主要介绍CString所需的内容

#3


OutputDebugStringW (myString.c_str ());

#4


Welcome to C++ ;-)

欢迎来到C ++ ;-)

You can just create a wrapper function that accepts a std::string. Then in the function extract the c-style string and pass to OutputDebugStringW.

您可以创建一个接受std :: string的包装函数。然后在函数中提取c样式字符串并传递给OutputDebugStringW。

#5


std::wstring and std::string are just aliases for std::basic_string<wchar_t> and std::basic_string<char>.

std :: wstring和std :: string只是std :: basic_string 和std :: basic_string 的别名。

Both have a .c_str()-method which returns a conventional C-string pointer (LPCWSTR etc.) and a constructor that takes a C-string.

两者都有一个.c_str()方法,它返回一个传统的C字符串指针(LPCWSTR等)和一个带有C字符串的构造函数。

#6


You might want to look at CStdString. It's a cross platform standard C++ CString implementation that converts to most other string types pretty easily. Makes almost all string related headaches go away and it's just one header file.

您可能想要查看CStdString。它是一个跨平台的标准C ++ CString实现,可以很容易地转换为大多数其他字符串类型。几乎所有与字符串相关的头痛都消失了,它只是一个头文件。

#1


There are two built-in string types:

有两种内置字符串类型:

  • C++ strings use the std::string class (std::wstring for wide characters)
  • C ++字符串使用std :: string类(宽字符为std :: wstring)

  • C-style strings are const char pointers const char*) (or const wchar_t*)
  • C风格的字符串是const char指针const char *)(或const wchar_t *)

Both can be used in C++ code. Most API's, including Windows' are written in C, and so they use char pointers rather than the std::string class.

两者都可以在C ++代码中使用。大多数API(包括Windows)都是用C语言编写的,因此它们使用char指针而不是std :: string类。

Microsoft further hides these pointers behind a number of macros.

微软进一步隐藏了许多宏背后的指针。

LPCWSTR is a Long Pointer to a Const Wide String, or in other words, a const wchar_t*.

LPCWSTR是Const宽字符串的长指针,换句话说,是const wchar_t *。

LPSTR is a Long Pointer to String, or in other words, a char* (not const).

LPSTR是String的长指针,换句话说,是char *(不是const)。

They have a handful more, but they should be pretty easy to guess once you know these first few. They also have *TSTR variants, where the T is used to indicate that this might be either regular or wide chars, depending on whether UNICODE is enabled in the project. LPCTSTR resolves to LPCWSTR if UNICODE is defined, and LPCSTR otherwise.

他们还有一些,但是一旦你知道了前几个,它们应该很容易猜到。它们也有* TSTR变体,其中T用于表示这可能是常规字符或宽字符,具体取决于项目中是否启用了UNICODE。如果定义了UNICODE,则LPCTSTR解析为LPCWSTR,否则解析为LPCSTR。

So really, when working with strings, you just need to know the two types I listed at the top. The rest are just macros for various variants of the char pointer version.

所以,在使用字符串时,您只需要知道我在顶部列出的两种类型。其余的只是char指针版本的各种变体的宏。

Converting from a char pointer to a string is simple:

从char指针转换为字符串很简单:

const char* cstr = "hello world";
std::string cppstr = cstr;

And the other way isn't much hader:

而另一种方式并不多:

std::string cppstr("hello world");
const char* cstr = cppstr.c_str();

That is, std::string takes a C-style string as an argument in the constructor. And it has a c_str() member function that returns a C-style string.

也就是说,std :: string在构造函数中将C样式的字符串作为参数。它有一个c_str()成员函数,返回一个C风格的字符串。

Some commonly used libraries define their own string types, and in those cases, you'll have to check the documentation for how they interoperate with the "proper" string classes.

一些常用的库定义了自己的字符串类型,在这些情况下,您必须检查文档中它们如何与“正确的”字符串类进行互操作。

You should usually prefer the C++ std::string class, as, unlike char pointers, they behave as strings. For example:

您通常应该更喜欢C ++ std :: string类,因为与char指针不同,它们表现为字符串。例如:

std:string a = "hello ";
std:string b = "world";
std:string c = a + b; // c now contains "hello world"

const char* a = "hello ";
const char* b = "world";
const char* c = a + b; // error, you can't add two pointers

std:string a = "hello worl";
char b = 'd';
std:string c = a + b; // c now contains "hello world"

const char* a = "hello worl";
char b = 'd';
const char* c = a + b; // Doesn't cause an error, but won't do what you expect either. the char 'd' is converted to an int, and added to the pointer `a`. You're doing pointer arithmetic rather than string manipulation.

#2


Here is a article that covers thing you need mainly for CString

这篇文章主要介绍CString所需的内容

#3


OutputDebugStringW (myString.c_str ());

#4


Welcome to C++ ;-)

欢迎来到C ++ ;-)

You can just create a wrapper function that accepts a std::string. Then in the function extract the c-style string and pass to OutputDebugStringW.

您可以创建一个接受std :: string的包装函数。然后在函数中提取c样式字符串并传递给OutputDebugStringW。

#5


std::wstring and std::string are just aliases for std::basic_string<wchar_t> and std::basic_string<char>.

std :: wstring和std :: string只是std :: basic_string 和std :: basic_string 的别名。

Both have a .c_str()-method which returns a conventional C-string pointer (LPCWSTR etc.) and a constructor that takes a C-string.

两者都有一个.c_str()方法,它返回一个传统的C字符串指针(LPCWSTR等)和一个带有C字符串的构造函数。

#6


You might want to look at CStdString. It's a cross platform standard C++ CString implementation that converts to most other string types pretty easily. Makes almost all string related headaches go away and it's just one header file.

您可能想要查看CStdString。它是一个跨平台的标准C ++ CString实现,可以很容易地转换为大多数其他字符串类型。几乎所有与字符串相关的头痛都消失了,它只是一个头文件。