如何将STL算法与ICU的迭代器一起使用?

时间:2013-05-13 07:38:46

标签: c++ algorithm stl icu

我想知道如何在STL中使用ICU库迭代器。例如,如果我们决定输出字符串的所有排列怎么办?

使用std::string,如下所示:

#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;
}

我尝试使用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;
}

但它无法编译:

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’
...

STL使用ICU的正确方法是什么?扩展UCharCharacterIterator类并为所有这些运算符提供代码?

0 个答案:

没有答案