std::string remove_ctrl(std::string s) {
std::string result;
for (int i=0; i<s.length(); ++i) {
if(s[i] >= 0x20)
result = result + s[i];
}
return result;
}
// 使用复合赋值操作 避免临时字符串
result = result + s[i]; // result += s[i];
// 通过预留存储空间 减少内存的重新分配
std::string result;
result.reserve(s.length());
for (int i=0; i<s.length(); ++i) {
// 消除对参数字符串的复制
std::string remove_ctrl_ref_args(std::string const& s) // 要解引用成指针
// 使用迭代器消除指针解引
for (auto it=s.begin(),end=s.end(); it != end; ++it) {
if (*it >= 0x20)
result += *it;
}
// 消除对返回的字符串的复制
void remove_ctrl_ref_result_it (
std::string& result,
std::string const& s)
{
result.clear();
// 用字符数组代替字符串
void remove_ctrl_cstrings(char* destp, char const* srcp, size_t size) {
for (size_t i=0; i<size; ++i) {
if (srcp[i] >= 0x20)
*destp++ = srcp[i];
// 更好的算法,提前结束for break
// substr生成字串
// 如果std::stringstream 是用std::string 实现的,那么它在性能上永远不能胜过std::string。
它的优点在于可以防止某些降低程序性能的编程实践