std::string::replace

canelia의 이미지

std::string str = "12,345,678,000";

이런식으로 문자열이 있을 때,

str.replace(",", "");

이런식으로 써서.. 쉼표를 다 지우고..

str == "12345678000"

로 만들려고 하는데..

오버로딩된 함수들을 찾아봐도 적절한게 없네요.. -_-;

어떤방법을 써야하나요?

doldori의 이미지

string의 멤버함수로 한큐에 할 수는 없고 루프를 돌려야 합니다.

string str = "12,345,678,000";
string::size_type pos = str.find(',');
while (pos != string::npos)
{
    str.erase(pos, 1);
    pos = str.find(',', pos);
}

한 문장으로 할 수도 있습니다.
#include <algorithm>
#include <functional>
using namespace std;

str.erase(remove_if(str.begin(), str.end(), bind1st(equal_to<char>(), ',')), str.end());