std::删除指定元素的矢量
std::remove vector for specified element
我试图删除一个由if语句定义的特定值,然后将其存储为int,然后我想查看矢量并使用将其擦除
if (comparedValuesBetween2[i] == First0ValueFor0Number2[j])
{
//make sure if there are no values to compare left just leave the the while loop by editing the count.
comparedValuesBetween2.resize(std::remove(comparedValuesBetween2.begin(), comparedValuesBetween2.end(), 8) - comparedValuesBetween2.begin());
}
但我收到了这些错误,我不知道为什么如果你能帮助
6 IntelliSense: too many arguments in function call g: 8227 acwACWSudokuSudokumain.cpp 225
5 IntelliSense: no suitable conversion function from "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>" to "const char *" exists g: 8227 acwACWSudokuSudokumain.cpp 225
我对c++很陌生。谢谢你的帮助。
您可以简单地调用std::vector::erase()
从容器中删除指定的元素:
if (comparedValuesBetween2[i] == First0ValueFor0Number2[j])
comparedValuesBetween2.erase(comparedValuesBetween2.begin() + i);
此外,vector.esers()只是一个旁注,它返回一个迭代器,指向向量中的下一个元素。因此,如果您正在通过迭代器遍历向量,则必须确保在删除向量中的元素后不会丢失对迭代器的跟踪。
您并没有真正提供足够的信息来说明您想要实现的目标。我假设i
和j
是循环索引?
做这件事的"惯用"方式被称为删除/擦除习惯用法:
for(int j; .....) {
...
if(....) {
comparedValuesBetween2.erase(std::remove(comparedValuesBetween2.begin(), comparedValuesBetween2.end(), First0ValueFor0Number2[j]));
}
}
它必须根据您的用例进行细化。理想情况下,j上的循环也不应该是原始循环。
相关文章:
- C++如何通过用户输入删除列表元素
- 使用函数"remove"删除重复元素
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 删除列表中的第n个元素
- 从矢量中删除元素后出现隔离错误
- 删除映射和分割错误中的一个过去结束元素
- 如何使用remove_if从矢量中删除元素
- 如何从使用 for 循环中的矢量大小的矢量中删除元素
- 从矢量中删除空元素
- 关于比较两个无序数组并删除 a[] 中可以在 b[] 中找到的元素的问题
- 删除指针数组 (C++) 中的元素
- 删除列表 c++ 中的最后 3 个元素
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 从std::multiset的第i个元素删除到末尾
- 删除 [] 是否等于每个元素删除.C++
- std::列出元素删除后节点的内存回收