C++:将 std::string 传递给想要更改字符串的 C 函数的正确实现?
我在用 C 编写的第三方库中有一个函数:char* fix_filename_slashes(char* path)
。此函数需要传递给它的可变 C 字符串,以便它可以根据操作系统将路径中的所有斜杠更改为正确的使用方式。我在 Facade 中使用的所有字符串都声明为 std::string
。我尝试简单地使用 foo.c_str() ,因为每个其他需要 C 字符串的函数都不会更改它,并且需要 const char * ,但是这个函数会导致错误:错误:“const char *”类型的参数与“char *”类型的参数不兼容
我得出的结果是:
char* tempf = const_cast<char*>(filename.c_str());
filename = std::string(fix_filename_slashes(tempf));
tempf = NULL;
被认为是“正确”还是还有其他(更正确?)完成任务的方法?
编辑
哎呀。显然该函数返回字符串的副本。尽管如此,已经给出了一些很好的答案。
I have a function in a third-party library written in C: char* fix_filename_slashes(char* path)
. This function expects a mutable C-string passed to it so it can change all the slashes in the path to the correct use based on the operating system. All the strings I'm using in my Facade are declared as std::string
s. I attempted to simply use foo.c_str()
as every other function that expects a C string doesn't change it and expects a const char *
, but this function causes an error: Error: Argument of type "const char *" is incompatible with parameter of type "char *"
Is the result I came up with:
char* tempf = const_cast<char*>(filename.c_str());
filename = std::string(fix_filename_slashes(tempf));
tempf = NULL;
considered "correct" or are there other (more correct?) ways to accomplish the task?
EDIT
Whups. Apparently the function returns a COPY of the string. Still there are some nice answers already given.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果字符串长度不变,可以使用指向字符串第一个字符的指针。这是 C++03 标准中未定义的行为,但所有已知的实现都可以正常工作,并且在 C++11 标准下明确允许。
如果字符串的大小可能发生变化,您将需要做更多的工作。
If the string length does not change, you can use a pointer to the first character of the string. This is undefined behavior in the C++03 standard, but all known implementations work properly and it is explicitly allowed under the C++11 standard.
If the size of the string may change, you'll have to do a little more work.
将其转换为存储在
std::vector
中的以 null 结尾的字符序列:用法示例:
Convert it into a null-terminated sequence of characters stored in a
std::vector
:Usage example:
由于您会遇到所有麻烦,因此您只需遵守 C 函数的要求并将字符串复制到 char 数组,然后在函数之后从 char 数组创建一个字符串或强制对原始字符串进行复制分配。
Since you are going to all the trouble you could just comply with the requirements of the C function and copy your string to a char array then after the function create a string from the char array or force a copy assignment on your original string.
如果
string
使用单独的缓冲区来存储c_str
字符串,则不会修改原始字符串。更好的方法是在堆栈或堆上创建一个 char 缓冲区,将字符复制到其中(以 null 终止),调用修复函数,然后将缓冲区分配回字符串。
If the
string
used a separate buffer to store thec_str
string this wouldn't modify the original string.Better is to create a
char
buffer on the stack or heap, copy the characters into it (null terminated), call the fix function, and then assign the buffer back into the string.这是另一种方法,需要进行一些设置,但之后会自动工作。它依赖于一个临时对象,该对象获取原始字符串的副本并将修改后的字符串复制回析构函数中。显然,所有这些复制的效率都不会太高,但在大多数情况下,效率并不重要。
Here's another approach that takes a little setup, but works automatically after that. It relies on a temporary object, which takes a copy of the original string and copies the modified string back in the destructor. Obviously all this copying won't be too efficient, but in most cases the efficiency won't matter.