[c++] 문자열에서 공백 제거하는 방법

다음은 eraseremove_if를 사용하여 문자열에서 공백을 제거하는 간단한 예제 코드입니다.

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str = "Hello, World! This is a test string.";

    // 공백을 제거할 문자열
    str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());

    std::cout << "공백 제거 후: " << str << std::endl;

    return 0;
}

위 코드에서 std::remove_if(str.begin(), str.end(), ::isspace) 부분은 isspace 함수를 사용하여 문자열에서 공백을 제거합니다.

참고 자료: