0
我有一個程序將字符的ctype標識映射到文本表示。我使用std::map
將掩碼的值(ctype_base::mask
)映射到表示字符串中字符類型的字符串。我遇到的麻煩是,當我試圖打印出該值時,沒有任何內容被打印輸出。爲什麼是這樣?從地圖獲取字符分類信息時遇到問題
#include <iostream>
#include <map>
#include <vector>
class mask_attribute
{
private:
typedef std::ctype_base base_type;
public:
mask_attribute()
{
mask_names[base_type::space] = "space";
mask_names[base_type::alpha] = "alpha";
mask_names[base_type::digit] = "digit";
}
std::string get_mask_name(base_type::mask mask) const
{
std::string result = (*mask_names.find(mask)).second;
return result;
}
private:
std::map<base_type::mask, std::string> mask_names;
};
int main()
{
std::string a = "abc123";
std::vector<std::ctype_base::mask> v(a.size());
auto& f = std::use_facet<std::ctype<char>>(std::locale());
f.is(&a[0], &a[0] + a.size(), &v[0]);
for (unsigned i = 0; i < v.size(); ++i)
std::cout << mask_attribute().get_mask_name(v[i]) << std::endl;
}
我預計產量爲:
alpha
alpha
alpha
digit
digit
digit
但是沒有打印。我在這裏做錯了什麼,如何解決?
注意如何從[cppreference示例](http://en.cppreference.com/w/cpp/locale/ctype/is)中的'ctype :: is'填充的向量中提取分類信息, – Cubbi