如何将 std::string 与采用 char[] 缓冲区的 Win32 函数混合?

发布于 2024-08-17 21:33:20 字数 351 浏览 3 评论 0原文

有许多 Win32 函数获取缓冲区的地址(例如 TCHAR[256]),并将一些数据写入该缓冲区。它可能小于缓冲区的大小,也可能是整个缓冲区。

通常,您会在循环中调用它,例如从流或管道中读取数据。最后,我想有效地返回一个字符串,该字符串包含所有迭代调用中检索该数据的完整数据。我一直在考虑使用 std::string ,因为它的 += 的优化方式与 Java 或 C# 的 StringBuffer.append()/StringBuilder.Append 类似() 方法,注重速度而不是内存。

但我不确定如何最好地将 std::string 与 Win32 函数混合在一起,因为这些函数首先采用 char[] 。有什么建议吗?

There are a number of Win32 functions that take the address of a buffer, such as TCHAR[256], and write some data to that buffer. It may be less than the size of the buffer or it may be the entire buffer.

Often you'll call this in a loop, for example to read data off a stream or pipe. In the end I would like to efficiently return a string that has the complete data from all the iterated calls to retrieve this data. I had been thinking to use std::string since it's += is optimized in a similar way to Java or C#'s StringBuffer.append()/StringBuilder.Append() methods, favoring speed instead of memory.

But I'm not sure how best to co-mingle the std::string with Win32 functions, since these functions take the char[] to begin with. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伊面 2024-08-24 21:33:21

std::string 有一个函数c_str(),它返回其等效的 C 样式字符串。 (const char *)

此外,std::string 具有重载赋值运算符,该运算符采用 C 样式字符串作为输入。

例如,让 ssstd::string 实例,sc 为 C 风格字符串,则可以执行相互转换:

ss = sc; // from C-style string to std::string
sc = ss.c_str(); // from std::string to C-style string

UPDATE :

正如 Mike Weller 指出的,如果定义了 UNICODE 宏,那么字符串将为 wchar_t*,因此您必须使用 std: :wstring 代替。

std::string has a function c_str() that returns its equivalent C-style string. (const char *)

Further, std::string has overloaded assignment operator that takes a C-style string as input.

e.g. Let ss be std::string instance and sc be a C-style string then the interconversion can be performed as :

ss = sc; // from C-style string to std::string
sc = ss.c_str(); // from std::string to C-style string

UPDATE :

As Mike Weller pointed out, If UNICODE macro is defined, then the strings will be wchar_t* and hence you would have to use std::wstring instead.

踏月而来 2024-08-24 21:33:21

我建议使用 std::vector 而不是 std::string,并在使用 v.size() 时使用 &v.front()。确保已分配空间!

您必须小心 std::string 和二进制数据。

s += buf;//will treat buf as a null terminated string
s += std::string(buf, size);//would work

Rather than std::string, I would suggest to use std::vector, and use &v.front() while using v.size(). Make sure to have space already allocated!

You have to be careful with std::string and binary data.

s += buf;//will treat buf as a null terminated string
s += std::string(buf, size);//would work
像你 2024-08-24 21:33:21
  • 您需要兼容的字符串类型:typedef std::basic_string; tstring; 是一个不错的选择。

  • 对于仅输入参数,您可以使用 .c_str() 方法。

  • 对于缓冲区,选择稍微不太明确:

std::basic_string 不能保证像 std::vector 一样使用连续存储。然而,我见过的所有 std::basic_string 实现都使用连续存储,并且 C++ 标准委员会认为缺少保证是标准中的缺陷。该缺陷已在 C++0x 草案中得到纠正。

如果您愿意稍微改变规则(不会产生任何负面后果),您可以使用 &(*aString.begin()) 作为指向长度为 aString.size() 的 TCHAR 缓冲区的指针。否则,你现在就只能使用 std::vector 了。

以下是 C++ 标准委员会关于连续字符串存储的规定:

没有标准化这个现有的
实践并没有给实施者
更多自由。我们认为这可能是
十年前。但商贩们有
都与他们交谈过
实施,并用他们的声音
在 LWG 会议上。这
实施将是
无论标准是什么,都是连续的
说。所以标准也可以
给弦乐客户更多的设计
选择。

  • You need a compatible string type: typedef std::basic_string<TCHAR> tstring; is a good choice.

  • For input only arguments, you can use the .c_str() method.

  • For buffers, the choice is slightly less clear:

std::basic_string is not guaranteed to use contiguous storage like std::vector is. However, all std::basic_string implementations I've seen do use contiguous storage, and the C++ standards committee consider the missing guarantee to be a defect in the standard. The defect has been corrected in the C++0x draft.

If you're willing to bend the rules ever so slightly - with no negative consequences - you can use &(*aString.begin()) as a pointer to a TCHAR buffer of length aString.size(). Otherwise, you're stuck with std::vector for now.

Here's what the C++ standard committee have to say about contiguous string storage:

Not standardizing this existing
practice does not give implementors
more freedom. We thought it might a
decade ago. But the vendors have
spoken both with their
implementations, and with their voice
at the LWG meetings. The
implementations are going to be
contiguous no matter what the standard
says. So the standard might as well
give string clients more design
choices.

倾城泪 2024-08-24 21:33:20

如果参数是仅输入,请像这样使用std::string

std::string text("Hello");
w32function(text.c_str());

如果参数是输入/输出,请使用std::vector< ;char> 改为这样:

std::string input("input");
std::vector<char> input_vec(input.begin(), input.end());
input_vec.push_back('\0');
w32function(&input_vec[0], input_vec.size());
// Now, if you want std::string again, just make one from that vector:
std::string output(&input_vec[0]);

如果参数是仅输出,也可以使用 std::vector 如下:

// allocates _at least_ 1k and sets those to 0
std::vector<unsigned char> buffer(1024, 0);
w32function(&buffer[0], buffer.size());
// use 'buffer' vector now as you see fit

您还可以使用 std::basic_stringstd::vector(如果需要)。

您可以在 Scott Meyers 所著的Effective STL一书中阅读有关该主题的更多信息。

If the argument is input-only use std::string like this

std::string text("Hello");
w32function(text.c_str());

If the argument is input/output use std::vector<char> instead like this:

std::string input("input");
std::vector<char> input_vec(input.begin(), input.end());
input_vec.push_back('\0');
w32function(&input_vec[0], input_vec.size());
// Now, if you want std::string again, just make one from that vector:
std::string output(&input_vec[0]);

If the argument is output-only also use std::vector<Type> like this:

// allocates _at least_ 1k and sets those to 0
std::vector<unsigned char> buffer(1024, 0);
w32function(&buffer[0], buffer.size());
// use 'buffer' vector now as you see fit

You can also use std::basic_string<TCHAR> and std::vector<TCHAR> if needed.

You can read more on the subject in the book Effective STL by Scott Meyers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文