1

I wonder how to use ICU library iterators with STL. For instance, what if we decided to output all permutations of a string?

With std::string it looks like the following:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

static void _usage(const char *executable)
{
    cout << "Usage: " << executable << " <string>" << endl;
}

int main (int argc, char const* argv[]) {
    if (argc < 2) {
        cerr << "Target string expected" << endl;
        _usage(argv[0]);
        return 1;
    }

    string s(argv[1]);

    do {
        cout << s << endl;
    } while (next_permutation(s.begin(), s.end()));

    return 0;
}

I tried to do the same using ICU:

#include <unicode/unistr.h>
#include <unicode/uchriter.h>
#include <unicode/ustdio.h>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

static void _usage(const char *executable)
{
    cout << "Usage: " << executable << " <string>" << endl;
}

int main (int argc, char const* argv[]) {
    if (argc < 2) {
        cerr << "Target string expected" << endl;
        _usage(argv[0]);
        return 1;
    }

    UnicodeString ustr(argv[1]);
    UChar *uc = ustr.getBuffer(-1);

    int32_t len = u_strlen(uc);
    UCharCharacterIterator iter_start(uc, len);
    UCharCharacterIterator iter_end(uc, len, len - 1);

    do {
        // XXX
    } while (next_permutation(iter_start, iter_end ));

    return 0;
}

But it fails to compile:

x86_64-pc-linux-gnu-g++     -I/usr/include  -licuio -licui18n -licuuc -licudata   permute2.C   -o permute2
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/algorithm:63:0,
                 from permute2.C:4:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h: In function ‘bool std::next_permutation(_BIter, _BIter) [with _BIter = icu_49::
UCharCharacterIterator]’:
permute2.C:31:49:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3531:7: error: no match for ‘operator++’ in ‘++__i’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3535:7: error: no match for ‘operator--’ in ‘--__i’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3540:4: error: no match for ‘operator--’ in ‘--__i’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3541:4: error: no match for ‘operator*’ in ‘*__ii’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3541:4: error: no match for ‘operator*’ in ‘*__i’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3544:8: error: no match for ‘operator--’ in ‘--__j’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/include/g++-v4/bits/stl_algo.h:3544:8: error: no match for ‘operator*’ in ‘*__i’
...

What's the proper way to make use of STL with ICU? Extend the UCharCharacterIterator class and provide code for all these operators?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • possible duplicate of [Is there an STL and UTF-8 friendly C++ Wrapper for ICU, or other powerful Unicode library](http://stackoverflow.com/questions/511280/is-there-an-stl-and-utf-8-friendly-c-wrapper-for-icu-or-other-powerful-unicod) – pmr May 13 '13 at 07:42
  • The duplicate answers your question: There is no way, besides wrapping ICU yourself or using an already existing wrapper. – pmr May 13 '13 at 07:43

0 Answers0