没有匹配的函数来调用“ regex_search(...)” [英] no matching function for call to ‘regex_search(...)'

查看:570
本文介绍了没有匹配的函数来调用“ regex_search(...)”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个老式的 const char * 指针和一个长度,有没有办法调用 std :: regex_search(),而不先将缓冲区的内容复制到 std :: string 上?这是我遇到的问题的一个简单示例:

Given an old-style const char * pointer and a length, is there a way to call std::regex_search() on it without first copying the contents of the buffer into a std::string? Here is a simple example of the problem I have:

#include <regex>

int main()
{
    const char *text = "123 foobar 456";
    const size_t len = strlen(text);

    const std::regex rx(" (.+)bar");

    std::smatch what;
    std::regex_search( text, text+len, what, rx); // <- problematic line

    return 0;
}

我认为第5个 std :: regex_search(),但是我不完全了解如何将指针转换为迭代器。当我尝试编译上面的代码时,我得到以下信息:

I thought the 5th std::regex_search() that takes two iterators is what I need, but I'm not fully understanding how to convert pointers to iterators. When I try to compile the code above, I get this:

g++ -std=c++11 test.cpp
test.cpp:11:45: error: no matching function for call to ‘regex_search(const char*&, const char*, std::smatch&, const regex&)’
/usr/include/c++/4.9/bits/regex.h:2131:5: note: template<class _Bi_iter, class _Alloc, class _Ch_type, class _Rx_traits> bool std::regex_search(_Bi_iter, _Bi_iter, std::match_results<_BiIter, _Alloc>&, const std::basic_regex<_CharT, _TraitsT>&, std::regex_constants::match_flag_type)
 regex_search(_Bi_iter __s, _Bi_iter __e,

...还有更多错误!

...and a lot more errors!

const char * 可以转换为必要的迭代器吗?我做错了吗?我误解了它的工作原理吗?

Can const char * be converted to the necessary iterator? Did I do it wrong? Am I misunderstanding how this works?

推荐答案

代码中的错误是您使用了错误的 match_results 类型。 smatch 应该是当您有 std :: string 对象并且将 std :: string :: iterator s传递给 regex 函数。当您具有原始的 char const * 时,请使用 cmatch

The error in your code is that you're using the wrong match_results type. smatch is supposed to be used when you have an std::string object and you're passing std::string::iterators to the regex function. When you have raw char const *s use cmatch instead.

更改

std::smatch what;

std::cmatch what;

实时演示

这篇关于没有匹配的函数来调用“ regex_search(...)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆