replace()去除空格

执行redis 命令的时候会默认用 空格  分割命令,所以当value 中存在空格,就会导致命令执行错误。于是,我想到了用replace函数去除空格,发现了几个注意事项:

#include <algorithm>
std::replace (myvector.begin(), myvector.end(), ' ', '');
  1. 单引号里面必须有值,替换空字符为空,注意这里不能用''虽然可以移除第一个空格但是字符串读到了结束标志就不会往后读了!
  2. replace函数是直接修改入参的!所以必须保证入参不是常量

/usr/include/c++/4.8.2/bits/stl_algo.h: In instantiation of ‘void std::replace(_FIter, _FIter, const _Tp&, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; _Tp = char]’:
OrderPool.cpp:226:85: required from here
/usr/include/c++/4.8.2/bits/stl_algo.h:4998:13: error: assignment of read-only location ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<const char*, std::basic_string<char> >()’
*__first = __new_value;

原文地址:https://www.cnblogs.com/lthuang/p/11862837.html