-3

特定の文字列から空白と句読点を削除するため。正規表現の一致を使用することはアプローチのようですが、bool 配列 [256] を使用して句読点とスペースの値を true に設定すると効率的です。また、これは複数回呼び出されるため、静的配列として使用することをお勧めしますが、char 配列で句読点とスペースの値を true に設定するにはどうすればよいですか? それを行うために別の静的メソッドを書くのは好きですか?

4

3 に答える 3

2

提供された 2 つの回答は機能しますが、関数ポインターのキャストを必要としないアプローチです。

std::string text = "some text, here and there.  goes up; goes down";
std::string result;
std::remove_copy_if(text.begin(), text.end(), std::back_inserter(result), [](char c)
{
    std::locale loc;
    return std::ispunct(c, loc) || std::isspace(c, loc);
}); 
于 2013-10-10T17:47:37.650 に答える
0

std::remove_copy_if一緒に使うstd::ispunct

string text ="some text with punctuations",result;
std::remove_copy_if(text.begin(), text.end(),            
                        std::back_inserter(result), //Store output           
                        std::ptr_fun<int, int>(&std::ispunct)  
                       );
于 2013-10-10T17:34:22.473 に答える