代码之家  ›  专栏  ›  技术社区  ›  Stephen Diehl

将Ostream转换为标准字符串

  •  59
  • Stephen Diehl  · 技术社区  · 14 年前

    我对C++ STL非常陌生,所以这可能是微不足道的。我有一个 ostream 变量中包含一些文本。

    ostream* pout;
    (*pout) << "Some Text";
    

    是否有方法提取流并将其存储在类型为的字符串中 char* ?

    3 回复  |  直到 8 年前
        1
  •  55
  •   Community CDub    7 年前
         std::ostringstream stream;
         stream << "Some Text";
         std::string str =  stream.str();
         const char* chr = str.c_str();
    

    我解释了这个问题的答案 question 我一小时前写的。

        2
  •  141
  •   Kalin Paul Lemmens    10 年前

    问题是关于 ostream 串, ostringstream 串。

    对于有兴趣回答实际问题的人(特定于 溪流 试试这个:

    void someFunc(std::ostream out)
    {
        std::stringstream ss;
        ss << out.rdbuf();
        std::string myString = ss.str();
    }
    
        3
  •  6
  •   Community CDub    8 年前

    尝试 std::ostringstream

       std::ostringstream os;
       os<<"Hello world";
       std::string s=os.str();
       const char *p = s.c_str();