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)可以解决此问题?
让我们看看非常基本的实现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正在做类似的事情,但我不知道如何做.