【问题标题】:boost::algorithm::contains std::vector<long> & long valueboost::algorithm::contains std::vector<long> & long 值
【发布时间】:2013-03-24 05:19:28
【问题描述】:

我正在使用boost::algorithm::contains(std::vector&lt;long&gt;, long value) 并收到大量错误。

std::vector<long> instance;
long byteIndex;
// (Perhaps more code?...)
boost::algorithm::contains(instances, byteIndex);

我不明白这个编译器错误C2039: 'type' : is not a member of 'boost::range_const_iterator&lt;C&gt;'

我阅读了模板类并看到了一个使用 std::string 的演示

std::string s = "Boris Schäling"; 
boost::algorithm::contains(s, "is");

我认为我对 boost 的使用没有任何不同,除了我使用的是不同的类型。知道为什么 boost::algorithm::contains(std::vector&lt;long&gt;, long) 无法编译吗?

【问题讨论】:

  • 请显示导致此错误的代码。
  • boost::algorithm::contains(std::vector&lt;long&gt;, long) 是导致错误的代码。尝试编译导致错误的附加语句see reference to function template instantiation 'bool boost::algorithm::contains&lt;std::_Vector_iterator&lt;_Myvec&gt;,long&gt;(const Range1T &amp;,const Range2T &amp;)' being compiled。从模板的角度来看,我看不出我做错了什么。
  • 请实际显示代码,不要只是描述它。
  • @KyleLutz boost::algorithm::contains(instances, byteIndex)instances 是 std::vectorbyteIndex 是 long 的代码。这是一个模板函数调用,因此没有更多可显示的内容。
  • 您可以展示更多内容。提供一个实际的可编译示例来演示您的问题会更有帮助。根据您提供的信息,我们都只是猜测。

标签: c++ boost contains


【解决方案1】:

boost::algorithm::contains 需要两个范围,输入范围和要搜索的范围。您收到错误是因为您提供了第一个范围 (std::vector&lt;long&gt;) 而不是第二个范围(您只提供了一个 long 值)。

你最好使用std::find

std::find(vector.begin(), vector.end(), value) != vector.end()

【讨论】:

  • 我不喜欢 std::find 这个实现,因为它不返回布尔值,而是返回找到的对象的迭代器,并且会不必要地膨胀代码。这就是我想要boost::algorithm::contains 的原因,因为它允许最接近Vector.contains in Java. 的等价物我需要做的就是std::vector&lt;long&gt;(byteIndex) 并且比较会起作用。不是最优雅的,但可以完成工作。
  • @DrewDormann 更正的是std::find(vector.begin(), vector.end(), value) != vector.end()?
  • @KyleLutz 在boost::algorithm::compare.hpp 中发现了另一个错误,其中编译器基于执行boost::algorithm::contains(std::vector&lt;std::string&gt;, "null") 不喜欢error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const char'
  • @Mushy:我认为你误解了boost::algorithm::contains 的作用。你希望std::string("hello") == 'h' 编译吗?
  • @KyleLutz 我理解你的意思;现在一切都编译好了。
【解决方案2】:

boost::algorithm::contains 有两个范围。您正在向量中搜索一个值。

在您的字符串示例中,您正在搜索序列"is"。如果您要搜索'i'(不是一个序列,一个值),您会得到与您在vector&lt;long&gt; 中描述的相同的错误。

代码:

std::vector<long> v { 1,2,3,4,5 };
std::vector<long> v1 { 3, 4 };
boost::algorithm::contains(v, v1);

编译得很好。

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2022-11-29
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多