如何通过长度从 std::vector 中删除元素<string>(擦除不起作用)
How to erase element from std::vector<string> by its length(erase not working)
我给了向量`
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';
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为std::string的某个索引赋值
- <<操作员在下面的行中工作
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数