C++ C++;:为什么我能';t将一对迭代器传递给regex_search?

C++ C++;:为什么我能';t将一对迭代器传递给regex_search?,c++,C++,我尝试传递一对迭代器来表示字符串序列: #include<regex> #include<string> using namespace std; int main(int argc,char *argv[]) { smatch results; string temp("abc"); string test("abc"); regex r(temp);

我尝试传递一对迭代器来表示字符串序列:

    #include<regex>
    #include<string>
    using namespace std;
    int main(int argc,char *argv[])
    {
        smatch results;
        string temp("abc");
        string test("abc");

        regex r(temp);
        auto iter = test.begin();
        auto end = test.end();

        if(regex_search(iter,end,results,r))
    cout<<results.str()<<endl;
        return 0;
    }
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
smatch结果;
字符串温度(“abc”);
字符串测试(“abc”);
正则表达式r(temp);
auto iter=test.begin();
自动结束=test.end();
if(正则表达式搜索(iter,end,results,r))

cout此处存在的问题是,传递到
regex_search
的迭代器与为
smatch
定义的迭代器之间的类型不匹配定义为:

typedef match_results<string::const_iterator> smatch;
调用迭代器时,版本定义为

现在,所有内容都具有
string::const_iterator
类型,它将进行编译

#include<regex>
#include<string>
#include <iostream>
using namespace std;
int main()
{
    smatch results;
    string temp("abc");
    string test("abc");

    regex r(temp);
    auto iter = test.cbegin();
    auto end = test.cend();

    if (regex_search(iter, end, results, r))
        cout << results.str() << endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
smatch结果;
字符串温度(“abc”);
字符串测试(“abc”);
正则表达式r(temp);
auto iter=test.cbegin();
自动结束=test.cend();
if(正则表达式搜索(iter,end,results,r))

cout
regex\u search
需要一个
const\u迭代器
,但您正在传递一个
std::string::iterator


使字符串
const

const string test("abc");
声明一个
常量迭代器

std::string::const_iterator iter = test.begin();
std::string::const_iterator end = test.end();
或者使用
cbegin
cend

auto iter = test.cbegin();
auto end = test.cend();

尝试将iter和end的值从test.begin()和test.end()更改为
test.cbegin()
test.cend()
#include<regex>
#include<string>
#include <iostream>
using namespace std;
int main()
{
    smatch results;
    string temp("abc");
    string test("abc");

    regex r(temp);
    auto iter = test.cbegin();
    auto end = test.cend();

    if (regex_search(iter, end, results, r))
        cout << results.str() << endl;
    return 0;
}
const string test("abc");
std::string::const_iterator iter = test.begin();
std::string::const_iterator end = test.end();
auto iter = test.cbegin();
auto end = test.cend();