本文介绍了istringstream在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我相信我只是在这里做一些蠢事,但我不能弄清楚它是什么。当我尝试运行此代码:
I'm sure I'm just doing something stupid here, but I can't quite figure out what it is. When I try to run this code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
string s("hello");
istringstream input(s, istringstream::in);
string s2;
input >> s2;
cout << s;
}
我收到此错误:
malloc: *** error for object 0x100016200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
我可以想到的唯一的事情是我在堆栈上分配了s2,但我认为字符串在堆上管理自己的内容。
The only thing I can think of is that I allocated s2 on the stack, but I thought strings manage their own content on the heap. Any help here would be appreciated.
感谢,
helixed
EDIT:修复main的最后一行,其中 cout< s
本应该是 cout<< s2
。如果我将s2初始化为hi,但没有其他错误,它运行没有错误。这是一个奇怪的gcc编译问题?
Fixed the last line of main, where cout << s
should have been cout << s2
. It runs without error if I initialized s2 to "hi", but not otherwise. Is this just a weird gcc compilation problem?
推荐答案
适用于我。
但我从来没有这样做:
istringstream input(s, istringstream::in);
尝试
istringstream input(s);
这篇关于istringstream在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!