gpt4 book ai didi

c++ - boost::algorithm::boyer_moore_search 面向对象示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:34 25 4
gpt4 key购买 nike

请帮助我获得面向对象的 boost::algorithm::boyer_moore_search 接口(interface)工作的基本示例。

程序界面对我有用,例如编译并打印“找到模式”:

#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>

int main() {
std::string corpus("hello world");
std::string pattern("hello");

if (corpus.end() != boost::algorithm::boyer_moore_search(corpus.begin(), corpus.end(),
pattern.begin(), pattern.end()))
{
std::cout << "pattern found" << std::endl;
}
return 0;
}

但是以下使用面向对象的接口(interface)会产生编译错误,例如

#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>

int main() {
std::string corpus("hello world");
std::string pattern("hello");

boost::algorithm::boyer_moore<std::string::const_iterator, std::string::const_iterator>
search(pattern.begin(), pattern.end());

if (corpus.end() != search(corpus.begin(), corpus.end()))
{
std::cout << "pattern found" << std::endl;
}
return 0;
}

这会产生以下错误,很容易在 ideone 上重现:

$ g++ test2.cpp 
In file included from test2.cpp:3:0:
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘class boost::algorithm::boyer_moore<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >’:
test2.cpp:10:14: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:104:39: error: no type named ‘skip_table_t’ in ‘class __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’
typename traits::skip_table_t skip_;
^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘boost::algorithm::boyer_moore<patIter, traits>::boyer_moore(patIter, patIter) [with patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
test2.cpp:10:44: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:63:50: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
suffix_ ( k_pattern_length + 1 )
^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::do_search(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
/usr/include/boost/algorithm/searching/boyer_moore.hpp:92:66: required from ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::operator()(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’
test2.cpp:12:60: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:133:27: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
k = skip_ [ curPos [ j - 1 ]];
^

我使用的是 gcc 4.8.2 和 boost 1.54。

我正在尝试使用 OO 接口(interface),因为我要在不同的语料库中重复搜索相同的模式,不想重复计算相同的跳表。

最佳答案

boyer_moore_search 构造函数采用单个模板类型参数,因为构造函数的两个参数是同一类型。您提供了两个。

search 的声明更改为:

boost::algorithm::boyer_moore<std::string::const_iterator>
search(pattern.begin(), pattern.end());

它会起作用。

关于c++ - boost::algorithm::boyer_moore_search 面向对象示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28660066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com