如何通过长度从 std::vector 中删除元素<string>(擦除不起作用)

How to erase element from std::vector<string> by its length(erase not working)

本文关键字:lt string 不起作用 擦除 gt 删除 何通过 std vector 元素      更新时间:2023-10-16

我给了向量`

  vector<string> inputArray = { "aba","aa","ad","vcd","aba" };

我想返回此向量,该向量仅包含最长长度的字符串,在这种情况下,我只想返回 {"aba","vcd","aba"},所以现在我想删除长度不等于最高

的元素
vector<string> allLongestStrings(vector<string> inputArray) {
int length = inputArray.size();
int longstring = inputArray[0].length();
int count = 0;
vector<string> result;
for (int i = 0; i < length; i++)
{
    if (longstring < inputArray[i].length())
    {
        longstring = inputArray[i].length();
    }
    count++;
}
for (int = 0; i<count;i++)
{
    if (inputArray[i].length() != longstring)
    { 
        inputArray[i].erase(inputArray.begin() + i);
        count--;
        i--;
    }
}
return inputArray;

}

但是我在inputArray[i].erase(inputArray.begin()+i);中遇到此错误no instance of overloaded fucntion "std::basic_string<_Elem,_Traits,_Alloc>::erase[with_Elem=char,_Traits=std::char_traits<char>,_Alloc=std::allocator<char>]" matches the argument list"

怎么了?

还有其他问题,但是此特定的编译器消息告诉您,这不是从字符串中删除特定字符的正确方法。

但是,阅读OP中的问题,我们看到您想从向量中删除字符串。要解决一个特定的错误,只需更改

inputArray[i].erase( /*character position(s) in the string*/ )

to

inputArray.erase( /*some position in the array*/ )

您可以修复它,因此它使用InputArray表示的字符串中的迭代器实际上从该字符串中删除字符,这当然不是您想做的。关键是,错误消息是因为您正在使用错误的迭代器类型,因为您认为您正在使用向量,但是实际上您告诉它与您从矢量出来的字符串一起工作。

,然后您将编译并遇到其他问题,这些问题已经在评论中涵盖了。

inputArray[i].erase(inputArray.begin() + i);的问题可以如Kenny Ostrom的答案中所示。

我想指出的是,OP可以使用擦除式词组成语,甚至可以创建一个只有更大字符串的新向量(已发布的代码已经复制了源向量)。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
template <typename InputIt>
auto only_the_longest_of(InputIt first, InputIt last)
{
    using value_type = typename std::iterator_traits<InputIt>::value_type;
    std::vector<value_type> result;
    // find the longest size
    auto longest = std::max_element(first, last,
        [](value_type const &a, value_type const &b) {
             return a.size() < b.size();
    });
    if ( longest == last )
        return result;
    // extract only the longest ones, instead of erasing
    std::copy_if( first, last, std::back_inserter(result)
                , [max_size = longest->size()] (value_type const& v) {
                    return v.size() >= max_size;    
    });
    return result;
}
template <typename T>
auto erase_the_shortest_from(std::vector<T> &input)
{    
    // find the longest size
    auto longest = std::max_element(input.cbegin(), input.cend(),
        [](T const &a, T const &b) {
             return a.size() < b.size();
    });
    if ( longest == input.cend()  ||  longest->size() == 0 )
        return input.end();
    // implement erase-remove idiom
    return input.erase(std::remove_if(
        input.begin(), input.end(), [max_size = longest->size()] (T const &v) {
            return v.size() < max_size;
    }));
}
int main()
{
    std::vector<std::string> test = {
        "aba", "aa", "ad", "vcd", "aba"
    };
    // The original vector remain unchanged
    auto result = only_the_longest_of(test.cbegin(), test.cend());
    for (auto const& str : result)
        std::cout << str << 'n';
    std::cout << 'n';
    // This will change the vector
    erase_the_shortest_from(test);
    for (auto const& str : test)
        std::cout << str << 'n';
}