如何初始化正则表达式的向量?

时间:2013-01-04 20:03:50

标签: c++ regex vector c++11 mingw

我的所作所为:

#include <vector>
#include <regex>
using namespace std;
int main() {
  vector<regex> regexes = { "[a-z]+", "[0-5]+" };
}

它给了我一堆错误:

In file included from c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/vector:63:0,
                 from c:\cpp\test.cpp:1:
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::basic_regex<char>; _Args = {const char&}]':
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_uninitialized.h:77:3:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*; _ForwardIterator = std::basic_regex<char>*; bool _TrivialValueTypes = false]'
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_uninitialized.h:119:41:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*; _ForwardIterator = std::basic_regex<char>*]'
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_uninitialized.h:260:63:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*; _ForwardIterator = std::basic_regex<char>*; _Tp = std::basic_regex<char>]'
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_vector.h:1170:4:   required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*; _Tp = std::basic_regex<char>; _Alloc = std::allocator<std::basic_regex<char> >]'
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_vector.h:1148:4:   required from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*; _Tp = std::basic_regex<char>; _Alloc = std::allocator<std::basic_regex<char> >]'
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_vector.h:393:4:   required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = const char*; _Tp = std::basic_regex<char>; _Alloc = std::allocator<std::basic_regex<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_regex<char> >]'
c:\cpp\test.cpp:5:48:   required from here
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/stl_construct.h:77:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/regex:62:0,
                 from c:\cpp\test.cpp:2:
c:\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/include/c++/bits/regex.h:402:7: error:   initializing argument 1 of 'std::basic_regex<_Ch_type, _Rx_traits>::basic_regex(const _Ch_type*, std::basic_regex<_Ch_type, _Rx_traits>::flag_type) [with _Ch_type = char; _Rx_traits = std::regex_traits<char>; std::basic_regex<_Ch_type, _Rx_traits>::flag_type = unsigned int]' [-fpermissive]

但是使用字符串以上述方式初始化矢量可以正常工作。

1 个答案:

答案 0 :(得分:4)

正则表达式构造函数是显式的(参见http://en.cppreference.com/w/cpp/regex/basic_regex/basic_regex),因此拒绝从字符串静默构造正则表达式。我不熟悉正则表达式对象,但是

vector<regex> regexes = { regex("[a-z]+"), regex("[0-5]+") }

应该这样做。