stringstream 相关问题

stringstream提供了一个操作字符串的接口,就好像它们是输入/输出流一样。

我可以通过 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* 的函数。

回答 1 投票 0

为什么 stoi 比没有 -O3 的 stringstream 慢很多?

今天我谈论的是 C++11 中的新闻特性,例如线程、to_string 和 stoi。 但事实上,这一切在 C++98 中已经成为可能。 然后我决定将旧库与新闻库进行比较...

回答 2 投票 0

布尔测试与字符串流中的异常

当 std::istringstream/std::ostringstream 上的 I/O 操作失败时,我想短路以避免在已经失败的 (x)string 上不必要地调用其余的 <> 运算符...

回答 1 投票 0

std::stringstream::flush() 应该做任何事情吗?

std::ostream有一个flush()方法,它: 将未提交的更改写入底层输出序列。 这对于 std::stringstream 意味着什么?如果我没理解错的话,那就意味着……

回答 1 投票 0

提取操作符“>>”如何知道何时停止提取?

问题与标题相同,当对字符串使用提取运算符“>>”时 “Hello World”有 3 个前导空白字符,在“...

回答 1 投票 0

超出范围问题?

我使用的框架已经在 CentOS 7 上运行了多年。我们正在将其迁移到 RHEL 8,但一些单元测试失败了。其中一个特别涉及从...

回答 1 投票 0

如何在std::wstringstream中插入NULL字符?

我需要根据 std::vector 中表示的值列表在 C++ 中构造一个多字符串 (REG_MULTI_SZ)。 这是首先想到的,但当然是错误的: std::ws...

回答 1 投票 0

如何在不写入任何内容的情况下获得字符串流的真正最大大小?

我正在使用下面的程序处理大的 libpacp 文件。 我对 stringstream 可以从操作系统分配的实际最大内存大小感到困惑。 第一部分代码是处理程序...

回答 3 投票 0

C++ 使用 stringstream 从字符串中提取 int

我正在尝试编写一个短行,使用 getline 获取字符串并使用 stringstream 检查它是否为 int 。我在如何检查正在检查的字符串部分是否是 i 时遇到了麻烦...

回答 3 投票 0

如何重载运算符<<(double const&) in an inherited std::stringstream?

我想覆盖运算符<< such that: double d = 3.0; mycustomstringstream << "Hello World " << d << "what a nice day."; std::cout <<

回答 1 投票 0

如何超载<<opeartor(double const&) in an inherited std::stringstream

我想覆盖 < 我想覆盖 <<opeartor 这样 double d = 3.0; mycustomstringstream << "Hello World " << d << "what a nice day."; std::cout << mystream.str() << std::endl; 将产生监视器输出 Hello World (double)(3.00000000000)what a nice day. 我尝试了什么 压倒一切 因为我可以像这样打印双打,所以我大胆地实现了: std::ostream& operator<<(std::ostream& o, double const& d){ o<<"(double)("<< d << ")"; return o; } 这不起作用,因为编译器会解释歧义(因为该运算符已经定义)。 继承 就像一开始一样,我可以从 std::stringstream 继承,只需替换我的自定义字符串流的该运算符的定义即可: #include<sstream> class MyCustomStringStream: public std::stringstream{}; MyCustomStringStream& operator<<(MyCustomStringStream& o, double const& d){ o<<"(double)("<< ( (std::stringstream)(o) << d ) << ")"; return o; } 这里的错误是: error: use of deleted function 'std::__cxx11::basic_stringstream<_CharT, _Traits, _Alloc>::basic_stringstream(const std::__cxx11::basic_stringstream<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' 那么那么: #include <iostream> #include <sstream> class MyStringStream: public std::stringstream{ std::stringstream aux; public: MyStringStream& operator<<(double const& d){ aux.str() = ""; aux << std::scientific << "((double)(" << d << ")"; *this << aux.str(); return *this; } }; int main() { double d = 12.3; MyStringStream s; s << "Hello World " << d << "what a nice day."; std::cout << s.str() << std::endl; } 但这仍然印着我 Hello World 12.3what a nice day. 演示[ https://godbolt.org/z/daG3bo6hv ] 无论你怎么想,继承通常都不是问题。无论如何,我不会从流派生(通常不建议从 STL 类型继承,尽管流允许这样做)。 对于您的情况,有一个更可重用的解决方案,制作一个辅助格式化对象: #include <iostream> #include <sstream> template<typename type_t> struct as_scientific { type_t value; }; template<typename type_t> std::ostream& operator<<(std::ostream& os, const as_scientific<type_t>& scientific) { os << std::scientific << "((" << typeid(type_t).name() << ")(" << scientific.value << ")"; return os; } int main() { double d = 12.3; std::ostringstream s; s << "Hello World " << as_scientific{d} << "what a nice day."; std::cout << s.str() << "\n"; // or directly std::cout << as_scientific{d} << "\n"; }

回答 1 投票 0

Python 保存字符串列表以供以后导出

我正在使用Python 3.12。 str = (" setV 当前测量 V") 打印(字符串) 结果 = [str] 我有以下是一个循环: print(f'{伏特:.3f} ',current.rstrip()," ",f'{Meas_Vo...

回答 1 投票 0

C++ std::istringstream 如何不在“ ”处终止

有一个函数 Foo 接受 char 的输入流。我想从我的函数 Bar 调用 Foo,它接受一个具有大小的 C 风格数组。 char 用于表示一个字节(不使用 std::byte

回答 1 投票 0

为什么 stringstream ss(s) 与 ss 不同 << s?

当我尝试使用 stringstream 时 #包括 #包括 #包括 int main() { std::string s = "Hello World 测试"; std::stringstream ...

回答 1 投票 0

std::istringstream::peek() 应该设置 eof 标志吗?

考虑以下代码: #包括 #包括 void print_flags(std::istringstream & iss) { std::cout << "good: " << iss.good() <&l...

回答 1 投票 0

从 g++/stdlib++ 和 clang++/libc++ 之间的流读取双精度数的差异

最小的“失败”示例: #包括 #包括 #包括 #包括 int main(int argv, char** argc) { std::string line = &quo...

回答 1 投票 0

长字符串流 LibXML 字符串转换为浮点数会添加不需要的数字

如何修改以下代码等以产生所需的结果? 我有一个包含超过 400 个浮点数的字符串,我一直试图将其放入包含 400 个 elem 的浮点数组中...

回答 1 投票 0

从不同的数据类型(字符串文字、整数变量、布尔值等)创建单个字符串。通过使用 cpp 中可用的可变模板变量

#define DLT_LOG(...)prepare_String(__VA_ARGS__) 模板 std::stringprepareMsg(Msg... 消息) { std::stringstream 流消息; (流消息<< ... << message) <&l...

回答 1 投票 0

如何从C++中的字符串中删除以某个字符开头的所有单词

我必须在 C++ 中创建一个函数,该函数将从字符串中删除以用户输入的某个字符开头的所有单词。例如,如果我有一个字符串“She made up he...

回答 3 投票 0

有人可以简单地向我解释一下什么是 std::streampos 吗?

你能给我看一些 std::streampos 的例子吗? 我不确定它的用途是什么,也不知道如何使用它。 我在github的一个项目中看到: std::streampos pos = ss.tellg(); 我在哪里...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.