我想搜索一个正则表达式并用一种颜色打印它。我使用boost::sregex_token_iterator来实现这一点。这是我的密码
boost::regex re("ab.");
string s="";
string buf;
string infile("retest.txt");
//string color="green";
ifstream in(infile.c_str());
int lcount=0;
while (getline(in,buf))
{
boost::sregex_token_iterator p(buf.begin(), buf.end(), re, 0);
boost::sregex_token_iterator end;
lcount++;
cout <<"line : "<<lcount<<endl;
for (;p != end;++p)
{
string m(p->first, p->second);
cout<< m <<endl;
//cout <<*(p->first)<<endl;
//cout <<*(p->second)<<endl;
//unsigned int pos = buf.find(m);
buf = buf.insert(p->first,"\e[0;32m");
buf = buf.insert(p->second+m.length()+7,"\e[0m");
}
cout<<"\n";
s.append(buf);
s.append("\n");
}
in.close();
cout <<"s is: "<<s<<endl;
return 0;
}
但我得到了这个错误:
null
我知道p->first
的类型不是insert函数接受的类型。但我不知道如何将这些相互转换。
问题可能是std::string::insert
的用法。在它的众多重载中,我看不到一个insert(迭代器,字符串)版本。
您可能希望通过显式声明char数组的长度来调用所需版本:
void insert (iterator p, size_t n, char c)
问题似乎是boost被配置为与不同版本的编译器一起使用,而不是与您当前使用的编译器一起使用。
你能补充一下你是如何编撰它的细节吗?