如何将 std::string 与采用 char[] 缓冲区的 Win32 函数混合?
有许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
std::string
有一个函数c_str()
,它返回其等效的 C 样式字符串。 (const char *
)此外,
std::string
具有重载赋值运算符,该运算符采用 C 样式字符串作为输入。例如,让
ss
为std::string
实例,sc
为 C 风格字符串,则可以执行相互转换:UPDATE :
正如 Mike Weller 指出的,如果定义了 UNICODE 宏,那么字符串将为 wchar_t*,因此您必须使用 std: :wstring 代替。
std::string
has a functionc_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
bestd::string
instance andsc
be a C-style string then the interconversion can be performed as :UPDATE :
As Mike Weller pointed out, If
UNICODE
macro is defined, then the strings will bewchar_t*
and hence you would have to usestd::wstring
instead.我建议使用
std::vector
而不是 std::string,并在使用v.size() 时使用
。确保已分配空间!&v.front()
您必须小心
std::string
和二进制数据。Rather than std::string, I would suggest to use
std::vector
, and use&v.front()
while usingv.size()
. Make sure to have space already allocated!You have to be careful with
std::string
and binary data.您需要兼容的字符串类型:
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++ 标准委员会关于连续字符串存储的规定:
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:
如果参数是仅输入,请像这样使用
std::string
如果参数是输入/输出,请使用
std::vector< ;char>
改为这样:如果参数是仅输出,也可以使用
std::vector
如下:您还可以使用
std::basic_string
和std::vector
(如果需要)。您可以在 Scott Meyers 所著的Effective STL一书中阅读有关该主题的更多信息。
If the argument is input-only use
std::string
like thisIf the argument is input/output use
std::vector<char>
instead like this:If the argument is output-only also use
std::vector<Type>
like this:You can also use
std::basic_string<TCHAR>
andstd::vector<TCHAR>
if needed.You can read more on the subject in the book Effective STL by Scott Meyers.