【发布时间】:2010-05-23 09:53:46
【问题描述】:
有一串空格:
string *str = new string();
str->resize(width,' ');
我愿意 在某个位置填充长度字符。
在 C 中看起来像
memset(&str[pos],'#', length );
我如何用 c++ 字符串实现这一点,我试过了
string& assign( const string& str, size_type index, size_type len );
但这似乎截断了原始字符串。 有没有一种简单的 C++ 方法可以做到这一点? 谢谢。
【问题讨论】:
-
是否有理由在您的示例中动态分配字符串?
-
@GMan - 我不希望它们作为自动变量,而是以邪恶的 strdup() 方式在堆上