C++ 如何替换整个字符串中字符的每个匹配项??在c++;

C++ 如何替换整个字符串中字符的每个匹配项??在c++;,c++,C++,这是我的代码,当我输入aeiou aeiou时,它只输出xxxxx我哪里出错了???我想替换整个字符串中的每个元音,但我只替换第一个单词。我的代码错了吗 #include<iostream> #include<stri

这是我的代码,当我输入
aeiou aeiou
时,它只输出
xxxxx
我哪里出错了???我想替换整个字符串中的每个元音,但我只替换第一个单词。我的代码错了吗

#include<iostream>                                                                                                                                    
#include<string>                                                                                                                                      
#include<algorithm>                                                                                                                                   

using namespace std;                                                                                                                                  

class remove_vowels_class                                                                                                                             
{                                                                                                                                                     
 private:                                                                                                                                             

 public:                                                                                                                                              

    void remove_vowels_accept(string the_string)                                                                                                      
    {                                                                                                                                                   
      std::replace(the_string.begin(),the_string.end(),'a','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'A','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'e','x');                                                                                          
      std::replace(the_string.begin(),the_string.end(),'E','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'i','x');                                                                                        
      std::replace(the_string.begin(),the_string.end(),'I','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'o','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'O','x');                                                                                         
      std::replace(the_string.begin(),the_string.end(),'u','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'U','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'~',' ');                                                                                      
      cout<<"the replaced string is :"<<the_string<<endl;                                                                                             

  }                                                                                                                                                   
};                                                                                                                                                    

int main()                                                                                                                                          
  {                                                                                                                                                   
remove_vowels_class maniobj;                                                                                                                      
string input_string;                                                                                                                              
cout<< "Enter a string to replace all vowels:"<<endl;                                                                                             
cin>>input_string;                                                                                                                                
std::replace(input_string.begin(),input_string.end(),' ','~');                                                                                    
maniobj.remove_vowels_accept(input_string);                                                 
#包括
#包括
#包括
使用名称空间std;
类删除\u元音\u类
{                                                                                                                                                     
私人:
公众:
void remove_元音_accept(字符串_字符串)
{                                                                                                                                                   
替换(字符串begin(),字符串end(),'a','x');
替换(字符串begin(),字符串end(),'A','x');
替换(字符串begin(),字符串end(),'e','x');
替换(字符串begin(),字符串end(),'E','x');
替换(字符串begin(),字符串end(),'i','x');
替换(字符串begin(),字符串end(),'I','x');
替换(字符串begin(),字符串end(),'o','x');
替换(字符串begin(),字符串end(),'O','x');
替换(字符串begin(),字符串end(),'u','x');
替换(字符串begin(),字符串end(),'U','x');
std::replace(字符串begin()、字符串end()、“~”、“”;

cout
cin>>input\u string;
将从输入中提取文本,直到它到达空白,因此只有输入的第一个字将被放入input\u string。使用字符串库中的
std::getline
获得整行。

就像
scanf
一样,
cin
忽略空白。检查。尝试
获取
以读取字符串


编辑:正如@James Reed指出的,
gets
正在从标准中删除。另一个答案提供了另一种选择。我将把答案留作将来参考。

使用
std::getline
而不是
cin>>输入字符串

cin>>输入
将只读取输入字符串直到空格

std::getline( std::cin, input_string); 
您需要使用,这里有一个更干净/更简单的c++11实现

#include <string>
#include <iostream>

std::string replace_vowels(const std::string & str) {
    static const char vowels[] = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};
    std::string cp(str);
    for(std::size_t i = 0; i < str.length(); ++i) {
        auto & ch = str[i];
        for(auto & v : vowels) {
            if(ch == v) {
                cp[i] = 'x';
                break;
            }
        }
    }
    return std::move(cp);
}

int main() {
    std::string input, output;
    std::cout << "Enter a string to replace all vowels : ";
    std::getline(std::cin, input);
    output = replace_vowels(input);
    std::cout << "Input : " << input << " / Output : " << output << std::endl;
}
#包括
#包括
std::string替换_元音(const std::string和str){
静态常量字符元音[]={'a','a','e','e','i','i','o','o','u','u'};
std::字符串cp(str);
对于(std::size_t i=0;iSTD::你看到了什么?如果你打印了代码>输入字符串[/COD]?你真的需要把Emacs模式行放在问题中吗?<代码>获取< /C> >被禁止为C++ 11。无论如何,你不应该使用C++字符串,除非你必须这样做。