
Hello to all, i want to search a container which copied from file with certain regular expression and then display it to stdout; Here is my code: [code] std::cout << "\n\nEnter Search designation : "; std::string userDesignation; getline(std::cin, userDesignation); std::ifstream inHandle("C:\\Staff.txt"); std::vector<std::string> data; data.reserve(100); std::copy(std::istream_iterator<std::string>(inHandle), std::istream_iterator<std::string>(), std::back_inserter(data) ); boost::regex searchExpression(userDesignation); boost::match_results<std::vector<std::string>::iterator > result; boost::regex_match(data.begin(), data.end(), result, searchExpression); [/code] The error is error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char' error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char' I think the error cause by vector string iterator rather than string iterator. Any workaround solution to this problem ? Thanks. -- Linux

Wong Peter a écrit :
Here is my code:
It isn't complete. I had to add some boilerplate to compile it. Please provide a compilable source in the future.
[code]
boost::regex searchExpression(userDesignation); boost::match_results<std::vector<std::string>::iterator > result;
Why would you like to use a vector here ? First, the boost::smatch do the trick. Second, regex_match doesn't find all matches in a row. You have to forward through the subject to seek all matching.
boost::regex_match(data.begin(), data.end(), result, searchExpression);
First and second arguments take an iterator on subject string, not on result !
[/code]
The error is
error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
-- Linux
It supposed to be a joke ? :D -- Mickaël Wolff aka Lupus Michaelis Racine <http://lupusmic.org> Blog <http://blog.lupusmic.org>

AMDG Wong Peter wrote:
Hello to all, i want to search a container which copied from file with certain regular expression and then display it to stdout;
Here is my code:
[code]
std::cout << "\n\nEnter Search designation : "; std::string userDesignation; getline(std::cin, userDesignation);
std::ifstream inHandle("C:\\Staff.txt"); std::vector<std::string> data; data.reserve(100); std::copy(std::istream_iterator<std::string>(inHandle), std::istream_iterator<std::string>(), std::back_inserter(data) );
This breaks the input file at whitespace. Is this really what you want?
boost::regex searchExpression(userDesignation); boost::match_results<std::vector<std::string>::iterator > result;
boost::regex_match(data.begin(), data.end(), result, searchExpression);
[/code]
The error is
error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
I think the error cause by vector string iterator rather than string iterator.
Any workaround solution to this problem ?
If you really need to have vector<string> instead of just string, then you need to run regex_search in a loop: BOOST_FOREACH(const std::string& s, data) { boost::regex_match(s, result, search_expression); } In Christ, Steven Watanabe

Thanks. I clearly understand that i need vector<string> and not string.

Hello to all, i want to search a container which copied from file with certain regular expression and then display it to stdout;
Here is my code:
[code]
std::cout << "\n\nEnter Search designation : "; std::string userDesignation; getline(std::cin, userDesignation);
std::ifstream inHandle("C:\\Staff.txt"); std::vector<std::string> data; data.reserve(100); std::copy(std::istream_iterator<std::string>(inHandle), std::istream_iterator<std::string>(), std::back_inserter(data) );
boost::regex searchExpression(userDesignation); boost::match_results<std::vector<std::string>::iterator > result;
boost::regex_match(data.begin(), data.end(), result, searchExpression);
[/code]
The error is
error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool) const' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
I think the error cause by vector string iterator rather than string iterator.
Any workaround solution to this problem ?
In your code you are trying to treat a string as the character type, which is not supported, and I'm fairly sure not what you wanted to do, did you mean to use a vector<char> ? HTH, John.
participants (4)
-
John Maddock
-
Mickael Wolff
-
Steven Watanabe
-
Wong Peter