1

我想搜索一个正则表达式并用颜色打印它。我使用 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;
  }

但我收到此错误:

来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,来自 /usr/ local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,来自/usr/local/lib/ gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,来自 main3.cc:1: /usr/local/ lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7:注意:void std ::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT =字符;_Traits = std::char_traits; _Alloc = std::allocator; 标准:: basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; 类型名 _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1182:7:注意:候选人需要 3 个参数,提供 2 个 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/。 ./../../../include/c++/4.7.2/bits/basic_string.h:1199:9: 注意:模板无效 std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = 字符;_Traits = std::char_traits; _Alloc = std::allocator] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ basic_string.h:1199:9:注意:43,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,来自/ usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,来自/usr/local/ lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,来自 main3.cc:1: /usr/ local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7:注意: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; 标准:: basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/ c++/4.7.2/bits/basic_string.h:1230:7:注意:没有已知的参数 1 从âconst __gnu_cxx::__normal_iterator >â 到âstd::basic_string::size_type {aka unsigned int}â /usr/local 的转换/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7:注意:std ::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits , _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; 标准:: basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1252:7:注意:候选人需要 4 个参数,提供 2 个在 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7 包含的文件中.2/../../../../include/c++/4.7.2/string:55:0,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2 /../../../../include/c++/4.7.2/bits/locale_classes.h:42,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2 /../../../../include/c++/4.7.2/bits/ios_base.h:43,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2 /../../../../include/c++/4.7.2/ios:43,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../ ../../../include/c++/4.7.2/ostream:40, 来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,来自 main3.cc :1: /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc: 361:6: 注意:std::basic_string<_CharT, _Traits, _Alloc> & std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT *, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.tcc:361:6:注意:候选人需要3个参数,_Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1293:7:注意:没有已知的参数 1 从âconst __gnu_cxx::__normal_iterator >â 到âstd::basic_string::size_type {aka unsigned int}â / usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7:注意:std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std:: basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1316:7:注意:候选人需要 3 个参数,提供 2 个 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/。 ./../../../include/c++/4.7.2/bits/basic_string.h:1334:7: 注意:std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string< _CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = 标准:: 分配器;std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1334:7:注意:参数 1 从 âconst __gnu_cxx::__normal_iterator >â 到 âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator > 没有已知转换}â main3.cc:37:51: error: no matching function for call to âstd::basic_string::insert(__gnu_cxx::__normal_iterator >, const char [5])â main3.cc:37:51: 注意:候选是:在 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54 包含的文件中:0,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h :42, 来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,来自 /usr/ local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,来自/usr/local/lib/ gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,来自 main3.cc:1: /usr/local/ lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7:注意:void std ::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT =字符;_Traits = std::char_traits; _Alloc = std::allocator; 标准:: basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; 类型名 _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1182:7:注意:候选人需要 3 个参数,提供 2 个 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/。 ./../../../include/c++/4.7.2/bits/basic_string.h:1199:9: 注意:模板无效 std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = 字符;_Traits = std::char_traits; _Alloc = std::allocator] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ basic_string.h:1199:9:注意:分配>::长度,std::分配器>())))。/include/c++/4.7.2/iostream:40,来自 main3.cc:1:/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../ ../include/c++/4.7.2/bits/basic_string.h:1230:7: 注意:std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert( std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1230:7:注意:没有已知的参数 1 从 â__gnu_cxx::__normal_iterator >â 到 âstd::basic_string:: 的转换 size_type {aka unsigned int}â /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ basic_string.h:1252:7: 注意:std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>:: size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT =字符;_Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1252:7:注意:候选人需要 4 个参数,_Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc> ::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.tcc:361:6:注意:候选人需要 3 个参数,提供 2 个在 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7 包含的文件中.2/../../../../include/c++/4.7.2/string:54:0,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2 /../../../../include/c++/4.7.2/bits/locale_classes.h:42,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2 /../../../../include/c++/4.7. 2/bits/ios_base.h:43,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7。 2/ios:43,来自/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream: 40,来自 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,来自 main3 .cc:1:/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string。 h:1293:7: 注意:std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ bits/basic_string.h:1293:7: 注意:没有已知的参数 1 从â__gnu_cxx::__normal_iterator >â 到âstd::basic_string::size_type {aka unsigned int}â /usr/local/lib/gcc/i686-的转换pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: 注意:std::basic_string<_CharT, _Traits , _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT ) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>:: size_type = unsigned int] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string。 h:1316:7:注意:候选人需要 3 个参数,提供 2 个 /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/ c++/4.7.2/bits/basic_string.h:1334:7: 注意:std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std:: basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*] /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../.. /include/c++/4.7.2/bits/basic_string.h:1334:7:注意:

我知道类型p->first不是插入函数接受的类型。但我不知道如何将它们相互转换。

4

2 回答 2

2

问题可能是您对std::string::insert. 在众多重载中,我看不到 insert(iterator, string) 版本。

您可能想通过明确说明 char 数组的长度来调用所需的版本:

void insert (iterator p, size_t n, char c)
于 2013-10-16T13:50:29.320 回答
1

问题似乎是 boost 配置为与不同版本的编译器一起使用,而不是您当前使用的编译器。

您能否添加有关如何编译它的详细信息?

于 2013-02-27T09:11:31.183 回答