windows C++ 创建一个简单的 LPWSTR 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7048633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:50:35  来源:igfitidea点击:

C++ Creating a simple array of LPWSTR's

c++windowsarraysstringwinapi

提问by DaveUK

As a C++ noob, I can't seem to get this right even though I know it should be simple :{ - I need to create an array of LPWSTR and then populate it with unique strings. The idea was to do something simple like this:

作为一个 C++ 菜鸟,即使我知道它应该很简单,我似乎也无法做到这一点:{ - 我需要创建一个 LPWSTR 数组,然后用唯一的字符串填充它。这个想法是做一些简单的事情:

LPWSTR *wszArray = new LPWSTR[5];

for(int x = 0; x < 5; x++)
{
    swprintf(wszArray[x], "somestring_%d", x);
}

I know that I haven't allocated memory for the LPWSTR, but after trying a few things I am not having much luck. Also I'm not sure if the array should be free'd later once i'm done with the strings.

我知道我没有为 LPWSTR 分配内存,但是在尝试了一些事情之后,我的运气并不好。此外,我不确定一旦我完成了字符串,数组是否应该被释放。

Any advice would be great.

任何建议都会很棒。

采纳答案by Jerry Coffin

What you have right now is a single pointer to pointers to wide char. You're then initializing that with the address of an array of 5 dynamically allocated pointers to wide char's. That's fine as far as it goes, but does notallocate any space for the strings themselves, only for pointers to 5 strings. You then need to allocate space for the strings themselves:

您现在拥有的是一个指向宽字符指针的指针。然后,您将使用 5 个动态分配的指向宽字符的指针的数组的地址对其进行初始化。这很好,只要它去,但并没有对字符串本身分配任何空间,只为指针,以5个字符串。然后,您需要为字符串本身分配空间:

for (int i=0; i<5; i++) {
    array[i] = new char[13];
    swprintf(array[i], L"somestring_%d", i);
}

Yes, you should delete the space you allocated after you're done using it. That would look something like:

是的,您应该在使用完后删除分配的空间。那看起来像:

for (int i=0; i<5; i++)
    delete [] array[i];
delete [] array;

OTOH, you probably shouldn't do any of this, and instead use something like:

OTOH,您可能不应该这样做,而是使用以下内容:

std::vector<std::wstring> array;

for (int i=0; i<5; i++) {
     std::wostringstream temp;
     temp << L"somestring_" << i;
     array.push_back(temp.str());
}

In this case, you can retrieve a "LPWSTR" using array[i].c_str(). You don't have to allocate or free any of the memory explicitly at all though.

在这种情况下,您可以使用 检索“LPWSTR” array[i].c_str()。不过,您根本不必显式分配或释放任何内存。

回答by Chris

LPWSTR is a unicode long string pointer in MS Windows, try using strings with the L"" form such as:

LPWSTR 是 MS Windows 中的 unicode 长字符串指针,尝试使用带有 L"" 形式的字符串,例如:

swprintf(wszArray[x], L"somestring_%d", x);

swprintf(wszArray[x], L"somestring_%d", x);

Unicode macros in Windows are available by using #define UNICODE, in MS VS there is a setting to use unicode strings, when set, it will define UNICODE for you.

Windows 中的 Unicode 宏可以通过使用 #define UNICODE 获得,在 MS VS 中有一个使用 unicode 字符串的设置,设置后,它将为您定义 UNICODE。

LPWSTR is the same as wchar_t* and LPSTR is char*.

LPWSTR 与 wchar_t* 相同,LPSTR 与 char* 相同。

When allocate memory in c++, it should always be free'd, so the answer is Yes.

在 c++ 中分配内存时,它应该总是被释放,所以答案是肯定的。

回答by A. K.

actually LPWSTRis already a pointer you should rather do

实际上LPWSTR已经是一个你应该做的指针

LPTSTR pBuffer; // TCHAR* 
pBuffer = new TCHAR[128]; // Allocates 128 or 256 BYTES, depending on compilation.

and then the forloop will be fine. for unicode-long string you need to prefix the string with Llike in L"your string"

然后for循环就可以了。对于 unicode-long 字符串,您需要在字符串前加上 Llike in L"your string"

and later you need to free the memory allocated using delete[]

稍后您需要释放使用分配的内存 delete[]

like :

喜欢 :

delete[] pBuffer;

for further documentation you may like to read:

有关更多文档,您可能想阅读:

http://www.codeproject.com/Tips/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc

http://www.codeproject.com/Tips/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc

回答by Ajay

Learning and doing some research-and-development is different aspect. But you shoulnt use these native methods for string processing. Instead use classes like std::string, stringstream, CStringetc.

学习和做一些研发是不同的方面。但是您不应该使用这些本地方法进行字符串处理。而是使用类,如std::stringstringstreamCString等。

  • std::stringis used by many (NOT by me!), it doesn't support <<operator, nor formatting functions, but supports other functions.
  • stringstreamis rich, doesn't support formatting functions, but <<operators, which are resolved at compile time and are type-safe.
  • CStringsupports formatting functions, and other common string functionality. It doesn't support <<operator. It handles ANSI/Unicode issues quite well. If you arent using MFC, you stillcan use it by #include<atlstr.h>in non-MFC project.
  • std::string被很多人使用(不是我!),它不支持<<运算符,也不支持格式化功能,但支持其他功能。
  • stringstream很丰富,不支持格式化函数,但支持<<运算符,在编译时解析并且是类型安全的。
  • CString支持格式化函数和其他常见的字符串功能。它不支持<<运营商。它可以很好地处理 ANSI/Unicode 问题。如果您不使用 MFC,您仍然可以#include<atlstr.h>在非 MFC 项目中使用它。

回答by Marc

I am actually also new in C++ (only finish Deitel c++ book, nothing more), I like CString only :-( if I agree to allow the project to accept MFC library in the project settings, I include the whole library in the header file if I feel like to, then I can do something like

我实际上也是 C++ 新手(只完成 Deitel c++ 书,仅此而已),我只喜欢 CString :-( 如果我同意在项目设置中允许项目接受 MFC 库,我将整个库包含在头文件中如果我愿意,那么我可以做类似的事情

CString str="";
CArray<CString,CString> arr;
for(int i=0;i<n;i++)
{ 
   str.Format("something_%d",i);
   arr.Add(str);
} 

If you really really love LPWSTR, the above also works with it. CString is a class designed to handle ANSi and Unixcode... :-)

如果你真的很喜欢 LPWSTR,上面的也适用于它。CString 是一个旨在处理 ANSI 和 Unixcode 的类...... :-)