C++ 如何将字符串流内容放入字符而不是字符串类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8765574/
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
How to put stringstream contents into char instead string type?
提问by Lion King
Every one know stringstream.str()
need a string variable type to store the content of stringstream.str()
into it .
每个人都知道stringstream.str()
需要一个字符串变量类型来存储其中的内容stringstream.str()
。
I want to store the content of stringstream.str()
into char variable or char array or pointer.
我想将内容存储stringstream.str()
到 char 变量或 char 数组或指针中。
Is it possible to do that?
有可能这样做吗?
Please, write a simple example with your answer.
请用你的答案写一个简单的例子。
回答by Vlad
Why not just
为什么不只是
std::string s = stringstream.str();
const char* p = s.c_str();
?
?
Edit: Note that you cannot freely give the p
outside your function: its lifetime is bound to the lifetime of s
, so you may want to copy it.
编辑:请注意,您不能自由地将p
您的功能提供给外部:其生命周期绑定到 的生命周期s
,因此您可能想要复制它。
Edit 2: as @David suggests, copyabove means copying of the content, not the pointer itself. There are several ways for that. You can either do it manually (legacy way "inherited" from C) -- this is done with the functions like std::strcpy
. This way is quite complicated, since it involves manual resources management, which is usually discouraged, since it leads to a more complicated and error-prone code. Or you can use the smart pointers or containers: it can be either std::vector<char>
or std::unique_ptr
/std::shared_ptr
.
编辑2:作为@大卫所暗示的,复制上述装置的内容的复制,而不是指针本身。有几种方法可以做到这一点。您可以手动完成(从 C 中“继承”的传统方式)——这是通过像std::strcpy
. 这种方式相当复杂,因为它涉及手动资源管理,通常不鼓励这样做,因为它会导致更复杂和容易出错的代码。或者您可以使用智能指针或容器:它可以是std::vector<char>
或std::unique_ptr
/ std::shared_ptr
。
I personally would go for the second way. See the discussion to this and @Oli's answer, it can be useful.
我个人会选择第二种方式。请参阅对此的讨论和@Oli 的回答,它可能很有用。
回答by Dietmar Kühl
If you want to get the data into a char
buffer, why not put it there immediately anyway? Here is a stream class which takes an array, determines its size, fills it with null characters (primarily to make sure the resulting string is null terminated), and then sets up an std::ostream
to write to this buffer directly.
如果您想将数据放入char
缓冲区,为什么不立即将其放入缓冲区呢?这是一个流类,它接受一个数组,确定它的大小,用空字符填充它(主要是为了确保结果字符串是空终止的),然后设置一个std::ostream
直接写入这个缓冲区。
#include <iostream>
#include <algorithm>
struct membuf: public std::streambuf {
template <size_t Size> membuf(char (&array)[Size]) {
this->setp(array, array + Size - 1);
std::fill_n(array, Size, 0);
}
};
struct omemstream: virtual membuf, std::ostream {
template <size_t Size> omemstream(char (&array)[Size]):
membuf(array),
std::ostream(this)
{
}
};
int main() {
char array[20];
omemstream out(array);
out << "hello, world";
std::cout << "the buffer contains '" << array << "'\n";
}
Obviously, this stream buffer and stream would probably live in a suitable namespace and would be implemented in some header (there isn't much point in putting anything of it into a C++ file because all the function are templates needing to instantiated). You could also use the [deprecated] class std::ostrstream
to do something similar but it is so easy to create a custom stream that it may not worth bothering.
显然,这个流缓冲区和流可能会存在于合适的命名空间中,并且会在某个头文件中实现(将它的任何内容放入 C++ 文件中没有多大意义,因为所有函数都是需要实例化的模板)。您也可以使用 [deprecated] 类std::ostrstream
来做类似的事情,但创建自定义流非常容易,可能不值得打扰。
回答by Oliver Charlesworth
You can do this if you want an actual copy of the string (vital if the stringstream object is going to go out of scope at some point):
如果您想要字符串的实际副本,则可以执行此操作(如果 stringstream 对象将在某个时候超出范围则至关重要):
const char *p = new char[ss.str().size()+1];
strcpy(p, ss.str().c_str());
...
delete [] p;
As discussed in comments below, you should be wary of doing it like this (manual memory management is error-prone, and very non-idiomatic C++). Why do you want a raw char array?
正如下面的评论中所讨论的,您应该警惕这样做(手动内存管理容易出错,并且非常不习惯 C++)。为什么要原始字符数组?
回答by zokia
I figured it out. Using namespace std and replacing tstingstream
with stringstream
. Next step is:
stringstream strstream;
strstream.imbue(std::locale("C"));
string str = strstream.str();
const char *sql= str .c_str();
Now you can execute sql statement.
我想到了。使用命名空间 std 并替换tstingstream
为stringstream
. 下一步是:
stringstream strstream;
strstream.imbue(std::locale("C"));
string str = strstream.str();
const char *sql= str .c_str();
现在可以执行sql语句了。
sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
Maybe it helps to somebody.
也许它对某人有帮助。