我发现一些 C++ 代码完全可以满足我的需要,但是我需要用 C 编写它,但我不知道如何用 C 编写它,所以我希望有人能帮助我。
C++代码是:
std::string value( (const char *)valueBegin, (const char *)valueEnd );
这是使用 string::string 构造函数:
template<class InputIterator> string (InputIterator begin, InputIterator end);
谁能帮我把它转换成 C 代码?
谢谢!
最佳答案
// Get the number of characters in the range
size_t length = valueEnd - valueBegin;
// Allocate one more for the C style terminating 0
char *data = malloc(length + 1);
// Copy just the number of bytes requested
strncpy(data, valueBegin, length);
// Manually add the C terminating 0
data[length] = '\0';
关于C++ std::string InputIterator 代码到 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2253825/