Я написал функцию, которая удаляет пробелы и тире из строки. Затем он вставляет пробел после каждого третьего символа. Мой вопрос: может ли кто-нибудь предложить другой способ сделать это, не используя stringstream
?
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
string FormatString(string S) {
/*Count spaces and dashes*/
auto newEnd = remove_if(S.begin(), S.end(), [](char c){return c == ' ' || c == '-';});
S.erase(newEnd, S.end());
std::stringstream ss;
ss << S[0];
for (unsigned int i = 1; i < S.size(); i++) {
if (i%3==0) {ss << ' ';}
ss << S[i];
}
return ss.str();
}
int main() {
std::string testString("AA BB--- ash jutf-4499--5");
std::string result = FormatString(testString);
cout << result << endl;
return 0;
}
std::stringstream
? - person Some programmer dude   schedule 02.04.2018std::string::insert
? Я не мог понять это? Спасибо - person arcoxia tom   schedule 02.04.2018std::string::insert
просто и понятно. Будь проще. - person Some programmer dude   schedule 02.04.2018