我正在製作控制檯計算器,並且希望刪除用戶在使用該程序時可能輸入的任何空格。這是代碼:該代碼應該返回沒有空格的字符串,但它返回字符串,直到第一個空格字符
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
std::cout<<"Enter sum): ";
cin>>str;
str.erase(std::remove_if(str.begin(), str.end(), (int(*)(int))isspace), str.end());
cout<<str;
system("pause");
return 0;
}
如果我進入2 + 2 =,輸出應該是2 + 2 = 但輸出:2 我使用了錯誤的功能我在這裏?
也許這可以幫到您? http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-: [1] [1] [在C++中取下的std :: string空間] c – Patrik