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

时间:2019-03-22 13:18:24

标签: c++

我给了一个向量`

  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;

}

但我在此行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"中收到此错误inputArray[i].erase(inputArray.begin()+i);

怎么了?

2 个答案:

答案 0 :(得分:2)

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

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

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

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

或者您可以修复它,以便它在inputArray [i]表示的字符串中使用迭代器实际上从该字符串中删除字符,这当然不是您要说的。关键是,错误消息是因为您使用错误的迭代器类型,因为您认为自己正在使用向量,但是实际上却告诉它使用从向量中提取的字符串。

然后您将进行编译,并有其他问题已经在注释中得到了很好的说明。

答案 1 :(得分:0)

inputArray[i].erase(inputArray.begin() + i);的问题可以解决,如Kenny Ostromanswer所示。

我想指出的是,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';
}
相关问题