从字符串 C++ 中删除元音 [英] Remove vowels from string C++

查看:41
本文介绍了从字符串 C++ 中删除元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是 C++ 的新手并试图创建一个函数来从字符串中删除元音,但我失败了,这是我目前的代码:

So I am new to C++ and tried to create a function that would remove vowels from a string, but I am failing at it horribly, Here is my code so far :

#include <string>
using namespace std;
string remove(string st) {
    for(int i = 0; st[i] != '\0'; i++) 
        st[i] = st[i] == 'a' || st[i] == 'e' || st[i] == 'i' || st[i] == 'o' || st[i] ==
        'u' || st[i] == 'A' || st[i] == 'E' || st[i] == 'I' || st[i] == 'O' || st[i] ==
        'U' ? '' : st[i];
    }
return st;

这似乎会引发错误?知道我做错了什么

This seems to throw an error ? any idea what I am doing wrong

我得到的错误是:

main.cpp:10:16: error: expected expression 'U' ? '' : Z[i];

并在另一个解释器上运行:

And running on another interpreter :

   .code.tio.cpp:7:14: error: incompatible operand types ('const char *' and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char'))
            'U' ? "" : Z[i];
                ^ ~~   ~~~~

推荐答案

根据谓词(条件)从顺序容器中删除元素的规范方法是使用 std::remove_if.不像它的名字暗示的那样,这个标准算法并没有完全删除元素,它将它们移动到容器的后面,这样它们就很容易擦除,而其他元素则保持不变并保持相同的顺序.它返回一个迭代器,指示包含已删除"元素的容器部分的开始.由于标准算法无法更改它们操作的容器的大小,因此必须使用容器的适当 remove 方法删除这些元素.对于 std::string,就是 <代码>std::string::erase.

The canonical way to remove elements from a sequential containers according to a predicate (a condition) is to use std::remove_if. Unlike it's name implies, this standard algorithm doesn't quite remove the elements, it moves them to the back of the container so they are easy to erase, leaving the other elements intact and in the same order. It returns an iterator that indicates the beginning of the portion of the container which contains the "removed" elements. Since the standard algorithms cannot change the size the containers they operate on, these elements must then be removed using the container's appropriate remove method. In the case of std::string, that's std::string::erase.

std::remove_if 接受一对定义要检查的元素范围的迭代器,以及一个用于确定要删除哪些元素的谓词.谓词为 true 的元素被移除.

std::remove_if accepts a pair of iterators which define the range of elements to inspect, as well as a predicate that is used to determine which elements to remove. Elements for which the predicate is true are removed.

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

// Returns true if p_char is a vowel
bool is_vowel(const char p_char)
{

    constexpr char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
    return std::find(std::begin(vowels), std::end(vowels), p_char) != std::end(vowels);
}

std::string remove_vowel(std::string st) 
{
    // Moves all the characters for which `is_vowel` is true to the back
    //  and returns an iterator to the first such character
    auto to_erase = std::remove_if(st.begin(), st.end(), is_vowel);

    // Actually remove the unwanted characters from the string
    st.erase(to_erase, st.end());
    return st;
}

int main()
{
    std::cout << remove_vowel("Hello, World!");
}

这篇关于从字符串 C++ 中删除元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆