3

私がやること :

#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]

ただし、上記の方法で文字列を使用してベクトルを初期化しても問題ありません。

4

1 に答える 1

7

正規表現コンストラクターは明示的であるため(http://en.cppreference.com/w/cpp/regex/basic_regex/basic_regexを参照)、文字列から正規表現をサイレントに構築することを拒否します。私は正規表現オブジェクトに精通していませんが、

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

トリックを行う必要があります。

于 2013-01-04T20:17:30.307 に答える