我试着根据它们所拥有的,过滤一个2个向量的字符串。我的意思是我的向量fvalues包含[“01”,“11”,“10”]
和另一个向量xbarl,该向量是使用get_combs()
函数递归创建的,以生成基于数字=2的二进制数[“00”,“01”,“11”,“10”]
。我想对这两个值进行排序,并将xbarl中的值(不在fvalues中)排序为另一个向量字符串。因此,使用上面的示例,我希望第三个向量包含[“00”]
,它应该是一个字符串。
这是我的代码,但在最后一个for循环中,我实际上执行了过滤,它给了我一个错误
#include <iostream>
#include <bitset>
#include <vector>
std::vector<std::string> get_combs(int width)
{
if (width == 1)
{
return {"0", "1"};
}
std::vector<std::string> rest = get_combs(width-1);
std::vector<std::string> appended;
for (const auto& s: rest)
{
appended.push_back(s + '0');
appended.push_back(s + '1');
}
return appended;
}
int main(){
std::vector<std::string> fvalues;
std::string row;
row = "01";
fvalues.push_back(row);
row = "11";
fvalues.push_back(row);
row = "10";
fvalues.push_back(row);
// fvalues now contains ["01","11","10"]
std::vector<std::string> xbarl;
xbarl = get_combs(2);
for(int i=0;i<xbarl.size();i++){
if( xbarl[i] in fvalues ){ <-- error
}
}
错误说
no viable conversion from
'__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > >::value_type' (aka
'std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >') to 'bool'
if( xbarl[i] in fvalues ){
^~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/basic_string.h:816:7: note:
candidate function
operator __sv_type() const noexcept
^
1 error generated.
compiler exit status 1
我不知道该怎么办,有人能帮帮我吗?
C++没有in
运算符。编译器不能识别它,但它也不知道它在那里,所以它给出了一些其他的随机错误。