场景:
1. C++没有提供删除std::(w)string的前后空格的函数,比如TrimSpace.
2. 很多库都提供, 但是为了移植代码方便,最好还是能用标准库解决就用标准库.
下边用模板实现了移除空格的函数. test.cpp
2016.5.29 修改.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <ctype.h>
using namespace std;
template<class T>
T RemovePreAndLastSpace(const T& str)
{
int length = str.size();
int i = 0,j = length -1;
// vc的isspace实现> 256就崩溃
while(i < length && isspace(str[i] & 0xFF)){i++;}
while(j >= 0 && isspace(str[j] & 0xFF)){j--;}
if(j<i) return T();
re
本文介绍了如何使用C++标准库和模板来实现删除std::(w)string的前后空格功能,提供了相关代码示例,并提到了《C++模板》一书中更优的解决方案。
订阅专栏 解锁全文
3018

被折叠的 条评论
为什么被折叠?



