我想找到最有效的方式來從字符串中刪除標點符號在c + +,這是我目前有。最有效的方式來從字符串中刪除標點符號在c + +
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <algorithm>
using namespace std;
void PARSE(string a);
int main()
{
string f;
PARSE(f);
cout << f;
}
void PARSE(string a)
{
a = "aBc!d:f'a";
a.erase(remove_if(a.begin(), a.end(), ispunct), a.end());
cout << a << endl;
}
有沒有更簡單/更有效的方法來做到這一點?
我在想使用str.len,獲取字符串的長度,通過for循環運行它並檢查ispunct,然後刪除它。
「_I正在考慮使用str.len,獲取字符串的長度,通過for循環運行並檢查ispunct然後刪除它。」這幾乎就是您的'erase'和'remove_if'調用的內容這樣做。與使用高度優化,經過嚴格測試的標準庫函數相比,您認爲自己做這件事更容易,更有效,或者更少出錯。 –
是什麼讓你覺得這樣使用[* erase-remove idiom *](https://en.wikipedia.org/wiki/Erase-remove_idiom)不會是最「有效」的方式?你是否認爲這是你程序中的瓶頸?就像前面的評論者說的那樣,試圖自己實現這一點將會變得更加混亂,並且更容易出錯和錯誤。 –
尋找這個問題的一部分,它沿着*「我知道有一個更有效的方法來做到這一點,它是什麼?」*如上面2條評論所暗示的。只找到問題*「有沒有更簡單/更有效的方法來做到這一點?」* –