2016-06-23 49 views
-3

所以我有一個小問題,我想在C++中實現這一點,但我不知道如何做到這一點:刪除兩個特定的字符

由於是包含隨機數字,符號和字母的字符串:

std::string = "1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"; 

現在我試圖找到所有^字符,如果這些之後是0到9之間的數字,刪除^和數量,因此:

"^1ghhu^7dndn^g" 

變爲:

"ghhudndn^g" 

我知道如何查找和替換/字符串中的字符刪除,但我不知道如何,如果它的後面跟一個數字在沒有硬編碼的方式來檢查。任何幫助表示讚賞。

+5

您需要嘗試自己解決問題。否則,你不會得到很好的迴應,它將被視爲一項家庭作業,並且你正試圖作弊。你在錯誤的網站,如果你沒有代碼和erros,這將得到downvoted –

+0

它不是一個家庭作業,我可以提供我試過的編碼,但我不知道爲什麼我應該發佈它,如果它不工作,我不dont得到任何錯誤,因爲我不知道如何實現,我試着沒有給出錯誤,所以IDC – SyxDuLappen

+0

只要發佈它,並提及它作爲你的嘗試,這會讓你更好的迴應。我會幫你解決問題 –

回答

1

我會做這種方式:

#include <iostream> 
#include <string> 
#include <utility> 
#include <iterator> 

template<class Iter, class OutIter> 
OutIter remove_escaped_numbers(Iter first, Iter last, OutIter out) { 
    for (; first != last ;) 
    { 
     auto c = *first++; 
     if (c == '^' && first != last) 
     { 
      c = *first++; 
      if (std::isdigit(c)) 
       continue; 
      else { 
       *out++ = '^'; 
       *out++ = c; 
      } 
     } 
     else { 
      *out++ = c; 
     } 
    } 
    return out; 
} 

int main() 
{ 
    using namespace std::literals; 
    auto input = "^1ghhu^7dndn^g"s; 
    auto output = std::string{}; 

    remove_escaped_numbers(input.begin(), input.end(), std::back_inserter(output)); 

    std::cout << output << std::endl; 
} 

或者這樣說:

#include <iostream> 
#include <regex> 



int main() 
{ 
    using namespace std::literals; 
    auto input = "^1ghhu^7dndn^g"s; 

    static const auto repl = std::regex { R"___(\^\d)___" }; 
    auto output = std::regex_replace(input, repl, ""); 
    std::cout << output << std::endl; 
} 
1
std::string s = "^1ghhu^7dndn^g"; 
for (int i = 0; i < s.length() - 1; ++i) 
{ 
    if (s[i] == '^' && std::isdigit(s[i + 1])) 
    { 
     s.erase(i, 2); 
     --i; 
    } 
} 

這需要這些包括:

#include <string> 
#include <cctype> 
+1

不是最快的方式來做到這一點,但具有死亡簡單和易於閱讀的優點。一個建議:如果你沒有擦除,使用'while(i user4581301

0

,你可以簡單地循環過字符串並複製它,同時跳過不需要的字符。這裏是一個可能的功能來做到這一點。

std::string filterString (std::string& s) { 
    std::string result = ""; 
    std::string::iterator it = s.begin(); 
    char c; 
    while (it != s.end()) { 
     if (*it == '^' && it != s.end() && (it + 1) != s.end()) { 
      c = *(it + 1); 
      if(c >= '0' && c <= '9') { 
       it += 2; 
       continue; 
      } 
     } 
     result.push_back(*it); 
     ++ it; 
    } 
    return result; 
} 
0

一個強大的解決方案是使用C++ 11帶來了在regex library

std::string input ("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"); 
std::regex rx ("[\\^][\\d]{1}"); // "[\^][\d]{1}" 
std::cout << std::regex_replace(input,rx,"woot"); 

>> 1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9wootdkkns 

這找到了一個「^」字符([\^])然後是1({1})數字([\d]),並用"woot"代替所有發生的事件。

1

一個解決方案使用std :: stringstream,並返回輸入字符串清除插入數字的。

#include <iostream> 
#include <sstream> 
#include <cctype> 

int t404() 
{ 
    std::stringstream ss; 

    std::string inStr("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns"); 

    for (size_t i = 0; i<inStr.size(); ++i) 
    { 
     if(('^' == inStr[i]) && isdigit(inStr[i+1])) 
     { 
     i += 1; // skip over caret followed by single digit 
     } 
     else 
     { 
     ss << inStr[i]; 
     } 
    } 
    std::cout << inStr << std::endl;  // compare input 
    std::cout << ss.str() << std::endl; // to results 
    return 0; 
} 

輸出:

1653gbdtsr362g2v3f3t52bv^hdtvsbjj; hdfuue,9^1dkkns 1653gbdtsr362g2v3f3t52bv^hdtvsbjj; hdfuue,9dkkns

+0

Loving this:''^'== inStr [i]'。其中一個最好的,最簡單的防禦類似編程世界中的排字錯誤。 – user4581301

0

希望這段代碼可以解決你的問題:

#include <iostream> 
#include <string> 

    int main() 
{ 
    std::string str = "^1ghhu^7dndn^g"; 
    std::string::iterator first, last; 
    for (std::string::iterator it=str.begin(); it!=str.end(); ++it) 
    { 
     if(*it == '^') 
     { 
      first = it; 
      it++; 
      while(isdigit(*it)) 
      { 
       it++; 
      } 
      last = it - 1; 
      if(first != last) 
      { 
       if((last + 1) != str.end()) 
       { 
        str.erase(first, last + 1); 
       } 
       else  
       { 
        str.erase(first, str.end()); 
        break; 
       } 
      } 
     } 
    } 
    std::cout<< str << std::endl; 
    return 0; 
} 

輸出:

$ ./erase 
ghhudndn^g