1

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

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

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

4

0 回答 0