小编Jar*_*łka的帖子

boost :: property_tree :: json_parser和两个字节的宽字符

介绍

std::string text = "á";
Run Code Online (Sandbox Code Playgroud)

"á"是双字节字符(假设采用UTF-8编码).
所以下面的行打印2.

std::cout << text.size() << "\n";
Run Code Online (Sandbox Code Playgroud)

std::cout仍然正确打印文本.

std::cout << text << "\n";
Run Code Online (Sandbox Code Playgroud)

我的问题

text转到boost::property_tree::ptree然后去write_json

boost::property_tree::ptree root;
root.put<std::string>("text", text);

std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, root);
std::cout << ss.str() << "\n";
Run Code Online (Sandbox Code Playgroud)

结果是

{
    "text": "\u00C3\u00A1"
}
Run Code Online (Sandbox Code Playgroud)

text等于"¡",与"á"不同.

有没有切换到可以解决这个问题std::wstring?是否有可能更改库(boost::property_tree::ptree)可以解决此问题?

c++ unicode boost boost-propertytree

8
推荐指数
1
解决办法
5023
查看次数

如何编写返回对单个位的引用的bool&operator [](int index)

让我们看看非常基本的实现Bitset.

struct Bitset {
    bool mask[32];

    bool& operator[] (int index) {
        return mask[index];
    }
};
Run Code Online (Sandbox Code Playgroud)

现在我可以写了

Bitset bitset;
bitset[0] = 1;
std::cout << bitset[0] << "\n";
Run Code Online (Sandbox Code Playgroud)

有可能优化.我可以用unsigned int而不是bool mask[32].

struct Bitset {
    unsigned int mask;

    bool& operator[] (int index) {
        // ??
    }
};
Run Code Online (Sandbox Code Playgroud)

是否有可能bool& operator[] (int index)用这样的规范写?我认为std::bitset正在做类似的事情,但我不知道如何做.

c++ operators

1
推荐指数
2
解决办法
620
查看次数

标签 统计

c++ ×2

boost ×1

boost-propertytree ×1

operators ×1

unicode ×1