C++ 将字符串或字符* 放入 istream 的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/499381/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:41:34  来源:igfitidea点击:

Optimal way to get a string or char* into an istream?

c++stream

提问by kal

What is the most optimal way to get a string or char* pointer into an istream.

将字符串或 char* 指针放入 istream 的最佳方法是什么。

I want to do the following

我想做以下事情

std::string a = "abc..";

//I know this can be done, not sure if this is most efficient
// and not sure about char*    pointers
std::istringstream istr (a);
...
foo (istr); 


void foo(std::istream& is) {

}

回答by Johannes Schaub - litb

If you want to construct an istringstream from it, a char* up to the null character, or all the stuff from an std::string:

如果你想从中构造一个 istringstream,一个 char* 到空字符,或者来自 std::string 的所有东西:

istringstream str(ptr); // char*
istringstream str(other_str); // std::string

If you talk about wanting a raw pointer into the buffer of an istream, you can't do it. Streams get their data on-demand if they need them either from files, terminals or else, optionally buffering their stuff (well, not exactly right. You can use a strstream, which accepts a raw pointer and reads/writes directly from that. But it's a deprecated class - don't use it. I'm lucky i've never done so). If all you want is something you can use somewhat like a pointer, you can use streambuf iterators. They are not really pointers though, so you can't subtract endfrom beginand other stuffs:

如果你说想要一个原始指针进入 istream 的缓冲区,你就做不到。如果流需要从文件、终端或其他方式获取它们的数据,则可以按需获取它们的数据,可选择缓冲它们的内容(嗯,不完全正确。您可以使用 a strstream,它接受原始指针并直接从中读取/写入。但它是不推荐使用的类 - 不要使用它。我很幸运我从来没有这样做过)。如果你想要的只是你可以像指针一样使用的东西,你可以使用流缓冲迭代器。不过,它们并不是真正的指针,因此您不能endbegin和其他东西中减去:

std::istreambuf_iterator<char> begin(one_istream), end;
while(begin != end)
    std::cout << *begin++;

If you talk about getting a string out of what was written into a stringstream, you can use ostringstream::str:

如果您谈论从写入字符串流的内容中获取字符串,您可以使用ostringstream::str

ostringstream o;
o << "This is a number: " << 42;
std::string str = o.str(); // str == "This is a number: 42"

Otherwise, you can only generally readstuff from an istream. You need an ostream, then you can do

否则,你一般只能读取来自东西istream。你需要一个ostream,然后你可以做

stream.write(ptr, N);
stream.write(ptr.c_str(), ptr.c_str() + ptr.size());

to write exactly N characters from the bytes that str points to. You can write it into the stream using <<too. It will write everything up to the null character, or everything from an std::string, but will respect formatting flags, like the field width:

从 str 指向的字节中准确写入 N 个字符。您也可以使用它将其写入流中<<。它会将所有内容写入空字符或 std::string 中的所有内容,但会尊重格式标志,例如字段宽度:

stream << ptr; // char*
stream << other_str; // everything from std::string

回答by Fredrik Jansson

This will work:

这将起作用:

 std::istringstream is("abc...");

And since istringstream is a istream, you will be able to use your is object as an istream.

由于 istringstream 是一个 istream,您将能够将您的 is 对象用作 istream。