我有一个字符串,我从ostringstream
得到。我目前正试图在此字符串(content.replace(content.begin(), content.end(), "\n", "");
)来代替某些字符,但有时我得到一个异常:C++字符串替换字符串的任意长度
malloc: *** mach_vm_map(size=4294955008) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
std::bad_alloc
我怀疑,这是因为该字符串是太大。这些情况的最佳做法是什么?在堆上声明字符串?
更新
我的完整的方法:
xml_node HTMLDocument::content() const {
xml_node html = this->doc.first_child();
xml_node body = html.child("body");
xml_node section = body.child("section");
std::ostringstream oss;
if (section.type() != xml_node_type::node_null) {
section.print(oss);
} else {
body.print(oss);
}
string content;
content = oss.str();
content.replace(content.begin(), content.end(), "<section />", "<section></section>");
content.replace(content.begin(), content.end(), "\t", "");
xml_node node;
return node;
}
如果你正在寻找的帮助与此特定问题,我想你会需要提供一个[最小的,可验证的和完整的例子](http://stackoverflow.com/help/mcve) – Yann 2014-09-29 14:48:00
有一个很好的机会,错误与这段代码无关。你有没有试过用valgrind跑这个? – dasblinkenlight 2014-09-29 14:48:31
我无法在OSX上运行valgrind。 – ruipacheco 2014-09-29 14:51:22