stringstream提供了一个操作字符串的接口,就好像它们是输入/输出流一样。
使用seekg时std::istreambuf_iterator相等
我对下面代码的输出感到困惑: std::string 内容 = "abcdefghijklmnopqrstuvwxyz1234567890"; std::istringstream iss(内容); iss.seekg(10); std::istreambuf_iterator 我对下面代码的输出感到困惑: std::string content = "abcdefghijklmnopqrstuvwxyz1234567890"; std::istringstream iss(content); iss.seekg(10); std::istreambuf_iterator<char> it{iss}, end; EXPECT_TRUE(iss.good()); iss.seekg(35); std::istreambuf_iterator<char> it2{iss}; EXPECT_TRUE(iss.good()); iss.seekg(0); std::istreambuf_iterator<char> it3{iss}; EXPECT_TRUE(iss.good()); bool oneAndTwo = it == it2; bool twoAndThree = it2 == it3; std::cout << std::boolalpha; std::cout << oneAndTwo << std::endl; // true std::cout << twoAndThree << std::endl; // true std::cout << "char at the iterators: "; std::cout << std::string(1, *it) << " " // a << std::string(1, *it2) << " " // a << std::string(1, *it3) << std::endl; // a EXPECT_TRUE(iss.good()); std::string s1{it, end}; // abcdefghijklmnopqrstuvwxyz1234567890 std::cout << s1 << std::endl; std::string s2{it2, end}; // a std::cout << s2 << std::endl; std::string s3{it3, end}; // a std::cout << s3 << std::endl; 以及 std::cout 的输出: true true char at the iterators: a a a abcdefghijklmnopqrstuvwxyz1234567890 a a 基于输出的两个问题: seekg对之后构建的std::istreambuf_iterator产生了影响。但是,如果我将一个迭代器与另一个迭代器进行比较,为什么所有迭代器都相等(it、it2 和 it3 相等)? 如果它们确实相等,为什么它们构造的字符串不同?为什么 it 给出完整的字符串,而其他两个迭代器只给出第一个字符“a”? 数据流是否已损坏?我哪里搞砸了? seekg 对之后构建的 std::istreambuf_iterator 产生了影响。但是,如果我将一个迭代器与另一个迭代器进行比较(it、it2 和 it3 相等),为什么所有迭代器都相等? 因为std::istreambuf_iterator是一个单遍输入迭代器,它从为其构造的std::basic_streambuf对象中读取连续的字符(请参阅cppreference)。 因此在流中移动会影响所有迭代器。 如果它们确实相等,为什么它们构造的字符串不同?为什么它给我完整的字符串,而其他两个迭代器只给出第一个字符“a”? 因为当您构造第一个字符串时,您会消耗流。
有没有办法从 string_view 创建字符串流而不复制数据?
我认为这是一个非常简单的问题。我特别想使用 std::get_time,但它需要某种类型的流来使用。我正在 string_view 中传递数据并且会...
我目前正在尝试实现一个函数,以便解析来自 Arduino 电路板的数字数据(来自通过 USB 连接连接到我的 PC 的电位计的浮子),但是...
我正在使用流 << "content" << std::endl. Then I'm getting the content by stream.str(). If added a line with std::endl into a std::
c++ stringstream read 似乎没有读取整个缓冲区
我有以下代码:https://godbolt.org/z/9aqqe5eYh #包括 #包括 #包括 #包括 int main() { std::string 行 = "
为什么 chrono::parse 无法解析 POSIX 日期和时间字符串?
我正在尝试使用库来解析POSIX日期和时间字符串,无论全局区域设置是什么,基于以下代码: #包括 #包括 #包括...
std::stringstream 与 std::string 连接多个字符串
我知道 std::stringstream 的优点之一是它是一个 std::istream,因此它可以接受来自定义运算符的任何类型的输入<< to std::istream, and also from primitives types. I...
尝试通过从中生成 vec 的 vec,然后使用 istringstream 验证双精度来对以下文件的所有行和列(仅有效双精度)进行求和: 18,459,45A,AG7,490,486 4A 2,65,-87,...
我想将一个整数输出到 std::stringstream,其格式与 printf 的 %02d 等效。有没有比以下更简单的方法来实现这一目标: std::stringstream 流; 流.setfill('0'); 流.s...
如何处理 std::stringstream 中的无效类型?
我有一个代码 #包括 #包括 使用命名空间 std; int main() { stringstream ss("123 ab 4"); 整数a、b、c; SS >> 一个; SS >> b...
我使用stringstream和libcurl来下载数据。我也有一个解析函数。 布尔解析() { istringstream temp(buff.str()); buff.str(""); 串线; QString line_QStr,
我正在编写一个 Book 类,作为我正在阅读的 C++ 书籍的练习。我想存储作者的姓氏以对所有书籍进行排序。 假设作者的名字是:“Albert Ein St...
我有一个示例程序,我想在我的应用程序上实现这个步骤。我想将字符串上的 int 元素分别推回向量中。我怎么能够? #包括 #
这些帖子没有回答我: 重置字符串流 如何清除字符串流变量? std::ifstream 文件( szFIleName_p ); if( !file ) 返回 false; // 创建一个用于解析的字符串流...
我可以通过 0 拷贝获取 std::stringstream 累积数据的原始指针吗?
这是我想做的: std::stringstream s; <<"Some "< 这就是我想做的: std::stringstream s; s<<"Some "<<std::hex<<123<<" hex data"<<...; 有了这个s,我非常想把它传递出去,这是很容易做到的。但是,在某些时候,需要(概念上)将其传递到仅接受描述内存区域的 const void */size_t 对的接口。 据我所知(希望得到纠正),在 C++17 及以下版本中没有办法使用 0 拷贝来做到这一点。必须使用 .str() 这将创建一个字符串副本,然后从那里获取它。 作为一个“黑客”,这就是我想出的: struct stringstream_extractor_t { // this structure will be used to get access to pbase member function which is protected // the way we do that is to inherit from it template <typename T> struct accessor_t : public T { static const char* data(const accessor_t* pT) { return pT->pbase(); } }; // get the return type for std::stringstream::rdbuf using bufferType = std::remove_pointer<decltype(((std::stringstream*)nullptr)->rdbuf())>::type; // having the std::stringstream::rdbuf result type, we can now create our accessor type // which will be able to call pbase inside it using accessorType = accessor_t<bufferType>; // this is where we deposit the data, in a const manner const std::string_view _data; // syntactic sugar: init the _data with the stuff from stream reference stringstream_extractor_t(std::stringstream& stream) : _data{getBuffer(stream), static_cast<size_t>(stream.tellp())} {} // syntactic sugar: init the _data with the stuff from stream pointer stringstream_extractor_t(std::stringstream* pStream) : _data{pStream ? getBuffer(*pStream) : nullptr, pStream ? static_cast<size_t>(pStream->tellp()) : 0} {} // this uses the accessor type to grab access to data static const char* getBuffer(const std::stringstream& stream) { // we get the buffer and we cast it to our accessor type. This is safe, as we do not // have any members inside it, just a static function const accessorType* pBuffer = reinterpret_cast<accessorType*>(stream.rdbuf()); // grab the data now return accessorType::data(pBuffer); } // convenience functionality inline const char* data() const { return _data.data(); } inline size_t size() const { return _data.size(); } }; 这是它的使用方式,具有类似 C 的界面 std::stringstream s; s << "Hi there! " << std::hex << 0xdeadbeef; const stringstream_extractor_t e(s); write(2, e.data(), e.size()); 是的,我知道指针必须保持活动状态(std::stringstream 实例),以及所有生命周期的影响。 是否有一种更舒适的非复杂方法来实现这个非常基本的事情:使用移动语义从输出字符串流中获取缓冲区。 我显然错过了一些东西,这不应该这么难。 https://godbolt.org/z/G53P6oocb 在 C++ 20 中你可以这样做 #include <iostream> #include <ios> #include <sstream> void c_style_func(const char* cp, std::size_t size) { std::cout << std::string_view (cp,size) << "\n"; } int main() { std::stringstream s; s << "Hi there! " << std::hex << 0xdeadbeef; auto view = s.view(); c_style_func(view.data(), view.size()); } 使用标准 1411 的第 29.8.2.4 页 basic_string_view view() const noexcept; 11 令 sv 为 basic_string_view。 12 返回: 一个 sv 对象,引用 buf 中 basic_stringbuf 的底层字符序列: (12.1) — 如果 ios_base::out 设置为 mode,则返回 sv(pbase(), high_mark-pbase())。 (12.2) — 否则,如果 ios_base::in 设置为 mode,则返回 sv(eback(), egptr()-eback())。 (12.3) — 否则,返回 sv()。 13 [注意:在底层字符序列被破坏或失效后使用返回的 sv 对象 *这是未定义的行为,除非 sv.empty() 为 true。 ——尾注] 2024 年补充: 与 std::string_view.data 相比,std::string::data 不受保证,并且通常 不是 null 终止。因此,上面的用法仍然安全,因为它不假设 NTS,但是请注意,如果您使用任何通常只采用 char* 的函数。
为什么 stoi 比没有 -O3 的 stringstream 慢很多?
今天我谈论的是 C++11 中的新闻特性,例如线程、to_string 和 stoi。 但事实上,这一切在 C++98 中已经成为可能。 然后我决定将旧库与新闻库进行比较...
当 std::istringstream/std::ostringstream 上的 I/O 操作失败时,我想短路以避免在已经失败的 (x)string 上不必要地调用其余的 <> 运算符...
std::stringstream::flush() 应该做任何事情吗?
std::ostream有一个flush()方法,它: 将未提交的更改写入底层输出序列。 这对于 std::stringstream 意味着什么?如果我没理解错的话,那就意味着……
问题与标题相同,当对字符串使用提取运算符“>>”时 “Hello World”有 3 个前导空白字符,在“...