2016-06-23 45 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