erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
 
1 // Utility function to trim whitespace off the ends of a string
2 inline std::string trim(std::string source)
3 {
4     std::string result = source.erase(source.find_last_not_of(" \t"+ 1);
5     return result.erase(0, result.find_first_not_of(" \t"));
6 }

 

 1
 std::string& trim(std::string &s) {
 2 23  if (s.empty()) {
 3 24    return s;
 4 25  }
 5 26
 6 27  s.erase(0,s.find_first_not_of(" "));
 7 28  s.erase(s.find_last_not_of(" "+ 1);
 8 29  return s;
 9 30}
10 

 

posted on 2009-12-16 16:02  allen.yu  阅读(1001)  评论(0编辑  收藏  举报