boost::algorithm::boyer_moore_search OO example - c++

Please help me get a basic example of the object oriented boost::algorithm::boyer_moore_search interface working.
The procedural interface works for me, e.g this compiles and prints "pattern found":
#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>
int main() {
std::string corpus("hello world");
std::string pattern("hello");
if (corpus.end() != boost::algorithm::boyer_moore_search(corpus.begin(), corpus.end(),
pattern.begin(), pattern.end()))
{
std::cout << "pattern found" << std::endl;
}
return 0;
}
But the following using the object oriented interface gives a compiler error, e.g.
#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>
int main() {
std::string corpus("hello world");
std::string pattern("hello");
boost::algorithm::boyer_moore<std::string::const_iterator, std::string::const_iterator>
search(pattern.begin(), pattern.end());
if (corpus.end() != search(corpus.begin(), corpus.end()))
{
std::cout << "pattern found" << std::endl;
}
return 0;
}
This gives the following error, which is easily reproducible on ideone:
$ g++ test2.cpp
In file included from test2.cpp:3:0:
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘class boost::algorithm::boyer_moore<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >’:
test2.cpp:10:14: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:104:39: error: no type named ‘skip_table_t’ in ‘class __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’
typename traits::skip_table_t skip_;
^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘boost::algorithm::boyer_moore<patIter, traits>::boyer_moore(patIter, patIter) [with patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
test2.cpp:10:44: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:63:50: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
suffix_ ( k_pattern_length + 1 )
^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::do_search(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
/usr/include/boost/algorithm/searching/boyer_moore.hpp:92:66: required from ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::operator()(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’
test2.cpp:12:60: required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:133:27: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
k = skip_ [ curPos [ j - 1 ]];
^
I'm using gcc 4.8.2 and boost 1.54.
I'm trying to use the OO interface, because I am going to repeatedly search the same pattern in different corpuses and don't want to repeatedly calculate the same skip table.

The boyer_moore_search constructor takes a single template type parameter because both arguments to the constructor are the same type. You've provided two.
Change your declaration of search to:
boost::algorithm::boyer_moore<std::string::const_iterator>
search(pattern.begin(), pattern.end());
and it will work.

Related

C++ Assignment not compiling with G++ but is on Visual Studio

This is a very rushed assignment, just a few days ago my laptop had died with all of my progress so I had little time to make everything with this work.
I am having a (what feels like big) error with compiling my project with G++. Especially when i did all of my testing on visual studio (and had no errors)
I would run
g++ -std=c++11 ksim.cpp -o test
I believe that the error is connected to my two-dimensional arrays, but am not 100%.
Is there a setting inside of Visual Studio that I am missing to check how its compiling everything?
// Nathan Vassell
// CS 245 - Assignment 4
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <sstream>
/*#include <locale>*/
using namespace std;
// Global variables
int ticks = 0;
string userInput;
string processes[256];
string blockedProcesses[10][3];
queue <string[10][3]> blockedQ2;
queue<string> newQ;
queue<string> readyQ;
queue<string> runningQ;
queue<string> blockedQ;
queue<string> exitQ;
int main()
{
return 0;
}
The Error im getting
In file included from /usr/include/c++/4.8.2/deque:62:0,
from /usr/include/c++/4.8.2/queue:60,
from ksim.cpp:8:
/usr/include/c++/4.8.2/bits/stl_construct.h: In instantiation of ‘void std::_Des troy(_Tp*) [with _Tp = std::basic_string<char> [10][3]]’:
/usr/include/c++/4.8.2/bits/stl_construct.h:103:46: required from ‘static void std::_Destroy_aux<<anonymous> >::__destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::basic_string<char> (*)[10][3]; bool <anonymous> = false]’
/usr/include/c++/4.8.2/bits/stl_construct.h:127:27: required from ‘void std::_ Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::basic_ string<char> (*)[10][3]]’
/usr/include/c++/4.8.2/bits/stl_construct.h:151:31: required from ‘void std::_ Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _Forward Iterator = std::basic_string<char> (*)[10][3]; _Tp = std::basic_string<char> [10 ][3]]’
/usr/include/c++/4.8.2/bits/deque.tcc:807:31: required from ‘void std::deque<_ Tp, _Alloc>::_M_destroy_data_aux(std::deque<_Tp, _Alloc>::iterator, std::deque<_ Tp, _Alloc>::iterator) [with _Tp = std::basic_string<char> [10][3]; _Alloc = std ::allocator<std::basic_string<char> [10][3]>; std::deque<_Tp, _Alloc>::iterator = std::_Deque_iterator<std::basic_string<char> [10][3], std::basic_string<char> (&)[10][3], std::basic_string<char> (*)[10][3]>]’
/usr/include/c++/4.8.2/bits/stl_deque.h:1853:39: required from ‘void std::dequ e<_Tp, _Alloc>::_M_destroy_data(std::deque<_Tp, _Alloc>::iterator, std::deque<_T p, _Alloc>::iterator, const std::allocator<_CharT>&) [with _Tp = std::basic_stri ng<char> [10][3]; _Alloc = std::allocator<std::basic_string<char> [10][3]>; std: :deque<_Tp, _Alloc>::iterator = std::_Deque_iterator<std::basic_string<char> [10 ][3], std::basic_string<char> (&)[10][3], std::basic_string<char> (*)[10][3]>]’
/usr/include/c++/4.8.2/bits/stl_deque.h:918:62: required from ‘std::deque<_Tp, _Alloc>::~deque() [with _Tp = std::basic_string<char> [10][3]; _Alloc = std::al locator<std::basic_string<char> [10][3]>]’
ksim.cpp:19:23: required from here
/usr/include/c++/4.8.2/bits/stl_construct.h:93:7: error: request for member ‘~st d::basic_string<char> [10][3]’ in ‘* __pointer’, which is of non-class type ‘std ::basic_string<char> [10][3]’
{ __pointer->~_Tp(); }
^
-bash-4.2$

C++ checking if an element is in my vector but getting failed to convert errors from eclipse

So below is my code. I am giving it several inputs but the important one is symbol and symbolList. symbolList is a 2D vector of strings. and the intent here is to check if symbol exists in symbolList and I am using the example from here: https://www.techiedelight.com/check-vector-contains-given-element-cpp/
Here is the code, error is on the if statement.
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
#include "windows.h"
#include <vector>
#include <chrono>
#include <algorithm>
using namespace std;
bool check_input(string &orderID, char buysell, string &symbol, int &qty, string &error, vector<vector<string>> &symbolList) {
error="None";
//omitted some irrelevant stuff
if (std::find(symbolList.begin(), symbolList.end(), symbol) != symbolList.end()) {
error= "wrong symbol";
return false;
}
return true;
}
But eclipse gives this error with my code and I don't see why:
could not convert 'std::find<__gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >, std::basic_string<char> >((& symbolList)->std::vector<std::vector<std::basic_string<char> > >::begin(), (& symbolList)->std::vector<std::vector<std::basic_string<char> > >::end(), (*(const std::basic_string<char>*)(& symbol)))' from '__gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >' to 'bool'
The compile error if I try to compile:
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Value = const std::basic_string<char>]':
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/stl_algo.h:120:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::basic_string<char> >]'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/stl_algo.h:161:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const std::basic_string<char> >]'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/stl_algo.h:3938:28: required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Tp = std::basic_string<char>]'
../src/oncemore.cpp:37:60: required from here
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/predefined_ops.h:241:17: error: no match for 'operator==' (operand types are 'std::vector<std::basic_string<char> >' and 'const std::basic_string<char>')
You can find a string in a vector<string> but you are trying to find a string in a vector<vector<string>>, find can't do that.
It's not the only way to do it but my advice would be to use find in a loop.
for (const auto& row : symbolList) {
if (std::find(row.begin(), row.end(), symbol) != row.end()) {
error= "wrong symbol";
return false;
}
}
row is a reference to a vector<string> so you can find a string in that.

Do const class methods prevent assigning variables outside of the class?

I have solved the problem of getting this code to compile by removing the "const". However, why does it seem I cannot assign non-class members in a const class method in this specific situation? It probably is part of the class; though, I do not understand why.
I got my code to compile, but I am confused at this situation.
Below, are some declarations in the class.
using twoDList = multimap<string,string>;
twoDList SomeMultiMap;
This will work when I take off the "const". Or at least compile. Though, here I am just assigning iterators declared only in this function. Using namespace std by the way.
bool object::foo(string a, string b) const
{
pair<object::twoDList::iterator,object::twoDList::iterator> wordRange;
wordRange = SomeMultiMap.equal_range(a);
object::twoDList::iterator it = wordRange.first;
//...
//...
//...
}
I expected this function to compile without removing the const, but it does not compile.
Edit: here are the compilation errors, and I am using this on linux command line.
g++ -g -DDEBUG -std=c++11 -c test1.cpp
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test1.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _U2 = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _T1 = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _T2 = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >]’:
test1.cpp:15:15: required from here
/usr/include/c++/4.8/bits/stl_pair.h:188:10: error: no match for ‘operator=’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ and ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’)
first = std::forward<_U1>(__p.first);
^
/usr/include/c++/4.8/bits/stl_pair.h:188:10: note: candidates are:
In file included from /usr/include/c++/4.8/map:60:0,
from test1.cpp:2:
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)
struct _Rb_tree_iterator
^
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&’
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&)
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&’
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test1.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h:189:11: error: no match for ‘operator=’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ and ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’)
second = std::forward<_U2>(__p.second);
^
/usr/include/c++/4.8/bits/stl_pair.h:189:11: note: candidates are:
In file included from /usr/include/c++/4.8/map:60:0,
from test1.cpp:2:
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)
struct _Rb_tree_iterator
^
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&’
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&)
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&’
makefile:10: recipe for target 'test1.o' failed
make: *** [test1.o] Error 1
Edit: more edits to fix scope operators. Also, added the fact that I am using namespace std if that helps.
A const after a member function's parameter list is a promise not to change the class object via the this pointer. The type of this becomes const ClassType* instead of just ClassType*. (Like any pointer-to-const or reference-to-const, this only means the thing can't be changed using that pointer or reference, not that the object is permanently const or can't be changed by other means.)
To say a class object is treated as const means that all its members (except any marked mutable) are treated as const. And since in a member function definition a plain non-static member name N actually means the same as this->N, members named that way are treated as const when the function declaration has a const after the parameter list.
So inside bool object::foo(string a, string b) const, the name SomeMultiMap, being a member of object, is treated as a const SomeMultiMap. And equal_range gives a pair<const_iterator, const_iterator> when the multimap object expression is const, or a pair<iterator, iterator> only when the object expression is not const.
So this may work instead (depending on what you need to do afterward):
std::pair<twoDList::const_iterator,twoDList::const_iterator> wordRange;
wordRange = SomeMultiMap.equal_range(a);
twoDList::const_iterator it = wordRange.first;
(Note you usually don't need the object:: prefixes, since the member function body is in its class's scope.)
Although I would just define these variables with auto instead, avoiding the need to get the type exactly right, and saving on typing out those long iterator names.
auto wordRange = SomeMultiMap.equal_range(a);
auto it = wordRange.first;

boost locale incomplete type boundary_indexing<char32_t>

I am first converting an utf-8 string to utf-32 and then I want unique words to be mapped with their positions. I started with boost locale.
#include <iostream>
#include <string>
#include <chrono>
#include <boost/shared_ptr.hpp>
#include <map>
#include <list>
#include <boost/locale.hpp>
typedef std::u32string string_type;
typedef std::pair<unsigned long, unsigned long> range_type;
typedef std::map<std::string, std::list<range_type>> wordref_type;
struct parser{
/**
* returns a map of "words" with its positions list
*/
static wordref_type parse(string_type::const_iterator b, string_type::const_iterator e){
wordref_type wordrefs;
range_type sentence;
boost::locale::boundary::segment_index<string_type::const_iterator> index(boost::locale::boundary::word, b, e);
//TODO: iterate index
return wordrefs;
}
};
int main(int argc, char** argv){
std::string input_str = "Some UTF-8 texts";
string_type buffer = boost::locale::conv::utf_to_utf<string_type::value_type>(input_str); // convert utf-8 to utf-32
wordref_type wordrefs = parser::parse(buffer.cbegin(), buffer.cend());
return 0;
}
It complains invalid use of incomplete type ‘const class boost::locale::boundary::boundary_indexing<char32_t>’. What is wrong ? I have Ideone'ed it
In file included from /usr/include/boost/locale/boundary.hpp:15:0,
from /usr/include/boost/locale.hpp:11,
from prog.cpp:7:
/usr/include/boost/locale/boundary/index.hpp: In instantiation of ‘static boost::locale::boundary::index_type boost::locale::boundary::details::mapping_traits<IteratorType, std::random_access_iterator_tag>::map(boost::locale::boundary::boundary_type, IteratorType, IteratorType, const std::locale&) [with IteratorType = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::index_type = std::vector<boost::locale::boundary::break_info>]’:
/usr/include/boost/locale/boundary/index.hpp:126:83: required from ‘boost::locale::boundary::details::mapping<BaseIterator>::mapping(boost::locale::boundary::boundary_type, boost::locale::boundary::details::mapping<BaseIterator>::base_iterator, boost::locale::boundary::details::mapping<BaseIterator>::base_iterator, const std::locale&) [with BaseIterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::details::mapping<BaseIterator>::base_iterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >]’
/usr/include/boost/locale/boundary/index.hpp:631:43: required from ‘boost::locale::boundary::segment_index<BaseIterator>::segment_index(boost::locale::boundary::boundary_type, boost::locale::boundary::segment_index<BaseIterator>::base_iterator, boost::locale::boundary::segment_index<BaseIterator>::base_iterator, const std::locale&) [with BaseIterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::segment_index<BaseIterator>::base_iterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >]’
prog.cpp:21:118: required from here
/usr/include/boost/locale/boundary/index.hpp:98:93: error: invalid use of incomplete type ‘const class boost::locale::boundary::boundary_indexing<char32_t>’
index_type tmp=std::use_facet<boundary_indexing<char_type> >(l).map(t,begin,end);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/boost/locale/boundary.hpp:12:0,
from /usr/include/boost/locale.hpp:11,
from prog.cpp:7:
/usr/include/boost/locale/boundary/facets.hpp:90:19: note: declaration of ‘class boost::locale::boundary::boundary_indexing<char32_t>’
class boundary_indexing;
^~~~~~~~~~~~~~~~~
In file included from /usr/include/boost/locale/boundary.hpp:15:0,
from /usr/include/boost/locale.hpp:11,
from prog.cpp:7:
/usr/include/boost/locale/boundary/index.hpp:103:95: error: invalid use of incomplete type ‘const class boost::locale::boundary::boundary_indexing<char32_t>’
index_type tmp = std::use_facet<boundary_indexing<char_type> >(l).map(t,str.c_str(),str.c_str()+str.size());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/boost/locale/boundary.hpp:12:0,
from /usr/include/boost/locale.hpp:11,
from prog.cpp:7:
/usr/include/boost/locale/boundary/facets.hpp:90:19: note: declaration of ‘class boost::locale::boundary::boundary_indexing<char32_t>’
class boundary_indexing;
^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/locale_classes.h:850:0,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/locale_classes.tcc: In instantiation of ‘const _Facet& std::use_facet(const std::locale&) [with _Facet = boost::locale::boundary::boundary_indexing<char32_t>]’:
/usr/include/boost/locale/boundary/index.hpp:98:89: required from ‘static boost::locale::boundary::index_type boost::locale::boundary::details::mapping_traits<IteratorType, std::random_access_iterator_tag>::map(boost::locale::boundary::boundary_type, IteratorType, IteratorType, const std::locale&) [with IteratorType = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::index_type = std::vector<boost::locale::boundary::break_info>]’
/usr/include/boost/locale/boundary/index.hpp:126:83: required from ‘boost::locale::boundary::details::mapping<BaseIterator>::mapping(boost::locale::boundary::boundary_type, boost::locale::boundary::details::mapping<BaseIterator>::base_iterator, boost::locale::boundary::details::mapping<BaseIterator>::base_iterator, const std::locale&) [with BaseIterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::details::mapping<BaseIterator>::base_iterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >]’
/usr/include/boost/locale/boundary/index.hpp:631:43: required from ‘boost::locale::boundary::segment_index<BaseIterator>::segment_index(boost::locale::boundary::boundary_type, boost::locale::boundary::segment_index<BaseIterator>::base_iterator, boost::locale::boundary::segment_index<BaseIterator>::base_iterator, const std::locale&) [with BaseIterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >; boost::locale::boundary::segment_index<BaseIterator>::base_iterator = __gnu_cxx::__normal_iterator<const char32_t*, std::__cxx11::basic_string<char32_t> >]’
prog.cpp:21:118: required from here
/usr/include/c++/6/bits/locale_classes.tcc:134:37: error: incomplete type ‘boost::locale::boundary::boundary_indexing<char32_t>’ used in nested name specifier
const size_t __i = _Facet::id._M_id();
~~~~~~~~~~~^~~~~
It complains invalid use of incomplete type ‘const class boost::locale::boundary::boundary_indexing’ What's wrong?
char32_t is not supported. Use
#define BOOST_LOCALE_ENABLE_CHAR32_T
But you need to enable it during building the library, as well:
Experimental support for C++0x char16_t and char32_t strings and streams.
Further looking: http://www.boost.org/doc/libs/1_64_0/libs/locale/doc/html/status_of_cpp0x_characters_support.html
If you want to build or test Boost.Locale with C++11 char16_t and char32_t support you should pass cxxflags="-DBOOST_LOCALE_ENABLE_CHAR32_T -DBOOST_LOCALE_ENABLE_CHAR16_T" to b2 during build and define BOOST_LOCALE_ENABLE_CHAR32_T and BOOST_LOCALE_ENABLE_CHAR32_T when using Boost.Locale
You need to compile boost with BOOST_LOCALE_ENABLE_CHAR32_T to include the boundary_indexing<char32_t> but ...
Status of C++11 char16_t/char32_t support
The support of C++11 char16_t and char32_t is experimental, mostly does not work and not intended to be used in production with current latest compilers: GCC-4.5, MSVC10 till major compiler's flaws would be fixed.
[....]
If you want to build or test Boost.Locale with C++11 char16_t and char32_t support you should pass cxxflags="-DBOOST_LOCALE_ENABLE_CHAR32_T -DBOOST_LOCALE_ENABLE_CHAR16_T" to b2 during build and define BOOST_LOCALE_ENABLE_CHAR32_T and BOOST_LOCALE_ENABLE_CHAR32_T when using Boost.Locale
[emphasis mine]
http://www.boost.org/doc/libs/1_66_0/libs/locale/doc/html/facets_8hpp_source.html
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
template<>
class BOOST_LOCALE_DECL boundary_indexing<char32_t> : public std::locale::facet {

Use boost::tokenizer with boost::iterator_range

I'm using boost::tokenizer to read a CSV-like file. I'm storing the the tokens in a std::vector. It works well, but I want to store only a boost::iterator for each token.
I tried:
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/range/iterator_range.hpp>
typedef std::string::const_iterator string_iter;
typedef boost::iterator_range<string_iter> string_view;
int main(){
std::string line;
std::vector<string_view> contents;
boost::tokenizer<boost::escaped_list_separator<char>, string_iter, string_view> tok(line.begin(), line.end());
contents.assing(tok.begin(), tok.end());
}
But it fails to compile:
/usr/include/boost/token_functions.hpp: In instantiation of ‘bool
boost::escaped_list_separator::operator()(InputIterator&, InputIterator, Token&) [with
InputIterator = __gnu_cxx::__normal_iterator >; Token =
boost::iterator_range<__gnu_cxx::__normal_iterator > >; Char = char; Traits =
std::char_traits]’: /usr/include/boost/token_iterator.hpp:70:11:
required from ‘void boost::token_iterator::initialize() [with TokenizerFunc =
boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >]’
/usr/include/boost/token_iterator.hpp:77:63: required from
‘boost::token_iterator::token_iterator(TokenizerFunc, Iterator, Iterator) [with
TokenizerFunc = boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >]’ /usr/include/boost/tokenizer.hpp:86:53:
required from ‘boost::tokenizer::iter
boost::tokenizer::begin() const [with
TokenizerFunc = boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >; boost::tokenizer::iter =
boost::token_iterator,
__gnu_cxx::__normal_iterator >, boost::iterator_range<__gnu_cxx::__normal_iterator > > >]’
/home/wichtounet/dev/gooda-to-afdo-converter/src/gooda_reader.cpp:58:37:
required from here /usr/include/boost/token_functions.hpp:187:16:
error: no match for ‘operator+=’ in ‘tok += (&
next)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* >()’ /usr/include/boost/token_functions.hpp:193:11: error: no match for
‘operator+=’ in ‘tok += (&
next)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* >()’ /usr/include/boost/token_functions.hpp: In instantiation of ‘void
boost::escaped_list_separator::do_escape(iterator&,
iterator, Token&) [with iterator = __gnu_cxx::__normal_iterator >; Token =
boost::iterator_range<__gnu_cxx::__normal_iterator > >; Char = char; Traits =
std::char_traits]’:
/usr/include/boost/token_functions.hpp:176:11: required from ‘bool
boost::escaped_list_separator::operator()(InputIterator&, InputIterator, Token&) [with
InputIterator = __gnu_cxx::__normal_iterator >; Token =
boost::iterator_range<__gnu_cxx::__normal_iterator > >; Char = char; Traits =
std::char_traits]’ /usr/include/boost/token_iterator.hpp:70:11:
required from ‘void boost::token_iterator::initialize() [with TokenizerFunc =
boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >]’
/usr/include/boost/token_iterator.hpp:77:63: required from
‘boost::token_iterator::token_iterator(TokenizerFunc, Iterator, Iterator) [with
TokenizerFunc = boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >]’ /usr/include/boost/tokenizer.hpp:86:53:
required from ‘boost::tokenizer::iter
boost::tokenizer::begin() const [with
TokenizerFunc = boost::escaped_list_separator; Iterator =
__gnu_cxx::__normal_iterator >; Type = boost::iterator_range<__gnu_cxx::__normal_iterator > >; boost::tokenizer::iter =
boost::token_iterator,
__gnu_cxx::__normal_iterator >, boost::iterator_range<__gnu_cxx::__normal_iterator > > >]’
/home/wichtounet/dev/gooda-to-afdo-converter/src/gooda_reader.cpp:58:37:
required from here /usr/include/boost/token_functions.hpp:130:9:
error: no match for ‘operator+=’ in ‘tok += '\012'’
/usr/include/boost/token_functions.hpp:134:9: error: no match for
‘operator+=’ in ‘tok += (&
next)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* >()’ /usr/include/boost/token_functions.hpp:138:9: error: no match for
‘operator+=’ in ‘tok += (&
next)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* >()’ /usr/include/boost/token_functions.hpp:142:9: error: no match for
‘operator+=’ in ‘tok += (&
next)->__gnu_cxx::__normal_iterator<_Iterator,
_Container>::operator* >()’
I also simply tried to compute the two iterators by myself using the boost::token_iterator, but I haven't been successful so far.
Is there a solution to get only the iterator range of each token instead of the string in order to save some performances ?
This can't work. The tokenizer expects a type (the third template argument) which can be appended with the results of the tokenizer function. Specifically, it must provide the operator += ( tokenizer<...>::iterator::value_type ). The code snippet below should take you a step further, though I am not sure if it's worth the effort...
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <cstddef>
typedef std::string::const_iterator string_iter;
typedef boost::iterator_range<string_iter> string_view;
// a constant size character buffer, skips anything beyond CSize...
template< std::size_t CSize >
class assignable_view {
std::size_t m_size;
char m_buffer[CSize];
friend std::ostream& operator << (std::ostream& p_out, assignable_view const & p_view)
{
if (p_view.m_size > 0u) {
std::copy(p_view.m_buffer, p_view.m_buffer + p_view.m_size, std::ostream_iterator<char>(p_out));
}
return p_out;
}
public:
template <class TIter>
void operator += (TIter p_input)
{
if (m_size < CSize) {
m_buffer[m_size++] = p_input;
}
}
assignable_view()
: m_size(0u) {}
};
int main(){
std::string line
= "Field 1,\"putting quotes around fields, allows commas\",Field 3";
std::vector<string_view> contents;
boost::tokenizer<
boost::escaped_list_separator<char>,
string_iter,
assignable_view<11>
> tok(line.begin(), line.end());
for (auto const & t_s : tok) {
std::cout << t_s << std::endl;
}
//contents.assing(tok.begin(), tok.end());
}
Ah! You'd need an include:
#include <iostream>
#include <boost/tokenizer.hpp>
#include <boost/range/iterator_range.hpp>
#include <string>
int main()
{
std::string line;
typedef std::string::const_iterator string_iter;
typedef boost::iterator_range<string_iter> string_view;
boost::tokenizer<boost::escaped_list_separator<char>, string_iter, string_view> tok(line.begin(), line.end());
}
compiles fine