boost::regex_search拒绝接受我的论点
boost::regex_search refuses to take my arguments
我在这件事上很挣扎,我已经到了没有任何进展的地步,是时候寻求帮助了。我对boost库的熟悉程度只比肤浅的要好一点。我正试着对一根相当大的绳子进行逐行扫描。事实上,它是读取到std::string对象中的文件的全部内容(文件不会那么大,它是命令行程序的输出)。
这个程序的输出pnputil是重复的。我正在寻找某些模式,以努力找到我想要的"oemNNN.inf"文件。从本质上讲,我的算法是找到第一个"oemNNN.inf",搜索该文件的识别特征。如果它不是我想要的,继续下一个。
在代码中,它类似于:
std::string filesContents;
std::string::size_type index(filesContents.find_first_of("oem"));
std::string::iterator start(filesContents.begin() + index);
boost::match_results<std::string::const_iterator> matches;
while(!found) {
if(boost::regex_search(start, filesContents.end(), matches, re))
{
// do important stuff with the matches
found = true; // found is used outside of loop too
break;
}
index = filesContents.find_first_of("oem", index + 1);
if(std::string::npos == index) break;
start = filesContents.being() + index;
}
我使用的这个例子来自1.47的boost库文档(我使用的版本)。请有人向我解释一下我的用法与本例的用法有何不同(除了我没有将内容存储到地图等中之外)。
据我所知,我使用的迭代器类型与示例中使用的相同。然而,当我编译代码时,微软的编译器告诉我:没有重载函数boost::regex_search的实例匹配参数列表。然而,intellisense用我正在使用的参数显示了这个函数,尽管迭代器的名称是BidiIterator。我不知道这有什么意义,但在给定的例子中,我假设无论BidiIterator是什么,它都需要一个std::string::迭代器来进行构建(也许这是一个糟糕的假设,但在给出这个例子时似乎是有意义的)。该示例确实显示了第五个参数match_flags,但该参数默认为值:boost::match_default。因此,这应该是不必要的。然而,只是为了好玩,我添加了第五个论点,但它仍然不起作用。我怎么会滥用这些论点?尤其是在考虑示例时。
下面是一个简单的程序,它演示了没有循环算法的问题。
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main() {
std::string haystack("This is a string which contains stuff I want to find");
boost::regex needle("stuff");
boost::match_results<std::string::const_iterator> what;
if(boost::regex_search(haystack.begin(), haystack.end(), what, needle, boost::match_default)) {
std::cout << "Found some matches" << std::endl;
std::cout << what[0].first << std::endl;
}
return 0;
}
如果您决定编译,我将针对boost库的1.47进行编译和链接。我正在处理的项目广泛使用这个版本,更新不是由我决定的。
谢谢你的帮助。这是最令人沮丧的。
Andy
通常迭代器的类型是不同的。
std::string haystack("This is a string which contains stuff I want to find");
从CCD_ 1和CCD_ 2返回的值将是CCD_。但你的匹配类型是
boost::match_results<std::string::const_iterator> what;
CCD_ 4和CCD_。所以的变体很少
- 将字符串声明为常量(即
const std::string haystack;
) - 将迭代器声明为const_iterator(即
std::string::const_iterator begin = haystack.begin(), end = haystack.end();
),并将它们传递给regex_search
- 使用
boost::match_results<std::string::iterator> what;
- 如果您有C++11,则可以使用
begin()
0和haystack.cend()
的工作示例
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 为什么VS2017拒绝我的功能模板专业化,而不是VS2015拒绝
- 为什么我的C 函数拒绝返回const引用
- boost::regex_search拒绝接受我的论点
- 在线法官拒绝我的回答
- Xcode 拒绝构建我的一个 OpenCL 项目,但成功构建了另一个项目
- Mac OS X拒绝创建我的OpenGL窗口
- Codechef拒绝我的解决方案
- C++11:clang 拒绝在我的模板定义中numeric_limits<>,而 gcc 接受它 - 哪个是对的?
- 尝试让我的程序拒绝数字输入
- 我不断收到一个错误,指出对输出文件的权限被拒绝,因此我的代码无法运行