df 去空格
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include <functional>
void main()
{
string str(" d df df ");
string::iterator new_end = remove_if(str.begin(), str.end(), bind2nd(equal_to<char>(), ' '));
str.erase(new_end, str.end());
cout << str << endl;
}
#include <iostream> #include <string>usingnamespace std; void Fun(string& str); int main() { string str(" I am a flur "); cout<<str<<endl; Fun(str); cout<<str<<endl; return0; } void Fun(string& tempStr) { string::size_type loc =0; loc = tempStr.find(''); while (loc !=string::npos) { tempStr.erase(loc,1); loc = tempStr.find('',loc); } }
#include <string>usingnamespace std; void Fun(string&str) { int i = str.length(); int t =0; char* temp =newchar[i +1]; for (int j =0; j < i; j++) { if (str[j] !='' ) { temp[t++] = str[j]; } } temp[t] ='\0'; str = temp; delete []temp; } void main() { string a ="he l lo"; Fun(a); cout << a << endl; }