std::unique_copy で余分なスペースを削除
余分のスペースを削除します*1
#include <iostream> #include <string> #include <algorithm> int main(void) { std::string s = "This is a unique_copy sample!!"; std::cout << "before:" << s << std::endl; auto result = std::unique_copy(s.begin(),s.end(),s.begin(), [](char c1,char c2) { // trueの場合コピーしないため両方も' 'ならtrueを返す return c1 == ' ' && c2 == ' '; }); s.erase(result,s.end()); // 不要な要素削除 std::cout << "after:" << s << std::endl; return 0; }
before:This is a unique_copy sample!! after:This is a unique_copy sample!!
*1:http://en.cppreference.com/w/cpp/algorithm/unique_copy