Error with std::find
Error with std::find
所以,我基本上有这段代码:
#include <algorithm>
using std::vector;
int WpMaxSAT::findInClause(int clause, int var, ClauseType type)
{
switch (type) {
case SOFT:
int val = std::find(softClauses.at(clause).begin(),
softClauses.at(clause).end(), var);
break;
case HARD:
break;
}
}
其中softClauses
在报头中定义为:
std::vector<std::vector<int>> softClauses;
这给了我这个错误:
wpmaxsat.cpp: In member function ‘int WpMaxSAT::findInClause(int, int, WpMaxSAT::ClauseType)’:
wpmaxsat.cpp:84:66: error: cannot convert ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ to ‘int’ in initialization
softClauses.at(clause).end(), var);
只是要注意的是,我正在与一位朋友并行工作,所以构造函数还没有定义(他正在处理它)。但我认为问题不在于初始化。
我以为这是#include<algorithm>
行的一个错误,但看起来不是。
std::find
返回迭代器,而不是值。您可以阅读编译器抱怨的内容,并自行解决。
这就是正确编写代码的方式:
auto it = std::find(softClauses.at(clause).begin(),
softClauses.at(clause).end(), var);
if (it != softClauses.at(clause).end()) {
int val = *it;
}
如果您只想知道值var
是否存在于vector
中,则可以使用std::count。它不返回迭代器,而是返回某种类型的InputIterator::difference_type
,即:
有符号积分型
我会将其视为size_t
,就像它通常用于STL
容器一样。std::count
的使用很简单:
auto count = std::count(softClauses.at(clause).begin(),
softClauses.at(clause).end(), var);
if (count) {
// value exists here
}
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 为什么在向量上使用 std::find() 时会出现错误?
- 使用 std::find 时没有匹配的函数调用错误
- std::set<Key,Compare,Allocator>::find() 函数使用"<"运算符而不是"=="运算符背后的直觉是什么?
- STL std::find() C++
- 如何将 std::find() 与 2d std: 数组一起使用?
- 为什么 std::find( s.begin(), s.end(), val ) 比集合 s 的 s.find(val) 慢 1000 倍<int>?
- 如果迭代器没有因插入而无效,则使用std::find和C::insert()是线程安全的
- 未按值排序的std::find和std::any_of
- 为什么std::map有find成员函数
- 在libc++和libstdc++之间的std::map上使用std::find时的实现差异
- 有没有办法在Cython中导入std::find find_if等?
- std::map::find 的效率是否与值的数据大小有关?
- std::string::find 为取消引用的迭代器和等效字符串文本返回不同的值
- 使用 find() 通过 std::set 的迭代器将不起作用。出了什么问题?
- Android-NDK 的 GGC/C++ 对于 std::string::find/std::getline(...) 来说似乎不稳定
- MinGW g++ cannot find std::stof
- 如何将std::find/std::find_if用于自定义类对象的向量