- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Có thể trùng lặp:
Printing a string to a temporary stream object in C++
std::ostringstream printing the address of the c-string instead of its content
我正在尝试使用 stringstream 构建一个字符串,就像您使用 cout 一样。这类似于日志记录类。我遇到的问题是,如果 << 运算符的第一个参数是一个字符串,当我随后使用 stringstream::str() 调用打印出该字符串流时,我得到一个垃圾地址,而不是字符串。这只发生在第一个字符串上。随后的字符串很好。数字总是好的。这是代码:
// class I use to print out the stream
class StreamWriter
{
công cộng:
StreamWriter()
{}
~StreamWriter()
{
std::string myMessage = m_stringstream.str();
std::cout << myMessage << std::endl;
}
std::stringstream m_stringstream;
};
// macro for simplification
#define OSRDEBUG (StreamWriter().m_stringstream)
// actual use
OSRDEBUG << "Hello " << "my " << "name is Pris " << 123456;
// đầu ra
0x8054480my name is Pris 123456
0x8054480my name is Pris 123456
0x8054480my name is Pris 123456
0x8054480my name is Pris 123456
谁能阐明正在发生的事情,以及我该如何解决这个问题?
编辑:以下更改(除了 padiablo 的示例之外)也适用,保持类的析构函数与宏的使用。
// class I use to print out the stream
class StreamWriter
{
công cộng:
StreamWriter()
{ m_stringstream = new std::stringstream; }
~StreamWriter()
{
std::string myMessage = m_stringstream.str();
std::cout << myMessage << std::endl;
delete m_stringstream;
}
std::stringstream * m_stringstream;
};
// macro for simplication
#define OSRDEBUG *(StreamWriter().m_stringstream)
虽然最初的问题仍然存在,因为它看起来应该有效......而且我认为了解何时将其放入生产质量代码中可能很重要。
我重载了 operator", str); // googletest test //Second test ss.str("");ss.clear(); //Mandatory if 'ss' ne
我有这个代码,想知道是否有可能让 stringstream 将 uint8_t 视为数字而不是字符? uint8_t s; std::stringstream sstream( "255" ); ss
函数 fprintf 写入 FILE*。我有一个写入 stringstream 的 debugPrint 函数。我不想更改我的功能,因为它在很多地方都被使用,而且它会破坏代码。 如何将字符串流传递给
我知道 stringstream 可以用 stringstream::str() 更新,但是如果之后我在 stringstream 中输入其他内容,它没有按预期工作。以下片段演示了该现象: #incl
我希望 stringstream 有一个从 string&& 窃取其初始内容的构造函数。 STL中通常不存在这种跨物种的“move 构造函数”吗?如果没有,为什么不呢? 最佳答案 有历史,令人失望。但
我什么时候使用 std::istringstream、std::ostringstream 和 std::stringstream 以及为什么我不应该只使用std::stringstream 在每个场
考虑以下代码,它编译并运行: #include #include struct Foo {}; void operator>>(std::istream &, Foo) { } int main(
考虑: std::string s_a, s_b; std::stringstream ss_1, ss_2; // at this stage: // ss_1 and ss
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
这个问题在这里已经有了答案: Why is "using namespace std;" considered bad practice? (42 个回答) 关闭 9 年前。 我是 C++ 的新手,
伙计们,实际上我想在这里做的是,当我输入 3,12,36 时,输出将是: 3 12 36 但是在这里我很难让它输出所有的答案。我一直在做的是,当您输入 3,12,36 时,它只会输出 3 12,如果您
我这里有这段代码 cout > s; stringstream is(s); is >> x >> y; cout > x cin >> y 关于C++ Strings
我遇到了以下代码的问题。 short int decrypted0[] = {0,2}; string message = "22"; string string0 =""; for(i=0;i<>
我正在尝试一个“stringstream”程序,它是这样的: #include #include using namespace std; int main() { int x; char ch;
我的印象是未格式化的 write()会将字节逐字复制到 ostream它被称为。另一位 SO 用户还告诉我“写入函数不会跳过任何内容”。如果您在使用 stringstream 时遇到问题,问一个问题。
这是一个用于解析文本文件的示例,问题是如果我在内部 while 循环之外声明“string parm”,那么一行中的最后一个字符串总是得到双重输出(这只发生在某些空格字符出现在“source.txt”
这个问题在这里已经有了答案: How to clear stringstream? [duplicate] (1 个回答) 关闭 9 年前。 例如我有这段代码: #include #include
#include #include #include #include using namespace std; stack aStack; s
我试图仅将 char 数组中的数字添加到 stringstream 对象中。代码是: char[50] buffer = ''; stringstream str; int page; str > p
为什么这个程序打印出空白? string str; stringstream ss(str); ss << "A string"; cout << str; // just blank
Tôi là một lập trình viên xuất sắc, rất giỏi!