C++ 新的std::map条目如何仅通过插入键初始化?
我有一个数据不是唯一的C++ 新的std::map条目如何仅通过插入键初始化?,c++,stdmap,C++,Stdmap,我有一个数据不是唯一的std::vector。实际上,大多数字符串都是重复的。我必须找到唯一的和它们的重复编号 我使用地图: std::map<MyString,unsigned short> stringsMap; ....... if ( stringsMap.find( currentString ) == stringsMap.end() ) { stringsMap[ currentString ] = 0; } stringsMap[ currentString
std::vector
。实际上,大多数字符串都是重复的。我必须找到唯一的和它们的重复编号
我使用地图:
std::map<MyString,unsigned short> stringsMap;
.......
if ( stringsMap.find( currentString ) == stringsMap.end() )
{
stringsMap[ currentString ] = 0;
}
stringsMap[ currentString ]++;
........
std::map stringsMap;
.......
if(stringsMap.find(currentString)=stringsMap.end())
{
stringsMap[currentString]=0;
}
stringsMap[currentString]++;
........
你有没有办法在更少的线路上完成
它可以在一行上完成:stringsMap[currentString]++默认情况下,代码>但短有不确定的值
它可以在一行上完成:stringsMap[currentString]++;但是,默认情况下,short的值不确定
这是不正确的,值定义良好,如中所述:
如果执行了插入,则映射的值为值初始化(默认为类类型构造,零初始化,否则),并返回对它的引用
重点是我的
因此,写一行就可以了:
stringsMap[ currentString ]++;
这是常见的做法,甚至在文档中作为示例给出:
// count the number of occurrences of each word
// (the first call to operator[] initialized the counter with zero)
std::map<std::string, size_t> word_map;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"}) {
++word_map[w];
}
//计算每个单词出现的次数
//(对运算符[]的第一次调用将计数器初始化为零)
std::map word\u map;
for(const auto&w:{“this”、“句子”、“is”、“not”、“a”、“句子”,
“这个”、“句子”、“是”、“a”、“骗局”}){
++单词地图[w];
}
它可以在一行上完成:stringsMap[currentString]++;但是,默认情况下,short的值不确定
这是不正确的,值定义良好,如中所述:
如果执行了插入,则映射的值为值初始化(默认为类类型构造,零初始化,否则),并返回对它的引用
重点是我的
因此,写一行就可以了:
stringsMap[ currentString ]++;
这是常见的做法,甚至在文档中作为示例给出:
// count the number of occurrences of each word
// (the first call to operator[] initialized the counter with zero)
std::map<std::string, size_t> word_map;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"}) {
++word_map[w];
}
//计算每个单词出现的次数
//(对运算符[]的第一次调用将计数器初始化为零)
std::map word\u map;
for(const auto&w:{“this”、“句子”、“is”、“not”、“a”、“句子”,
“这个”、“句子”、“是”、“a”、“骗局”}){
++单词地图[w];
}
但是,默认情况下,short的值不确定
否。对于不存在的键,映射将使用T()
初始化新创建项的值,对于unsigned short
,其有效计算结果为0
见(重点1):
1) 如果键不存在,则插入值类型(键,T())
。这个函数相当于返回insert(std::make_pair(key,T())。first->second代码>
-key\u type
必须满足CopyConstructible
的要求
-mapped\u type
必须满足CopyConstructible
和DefaultConstructible
的要求
如果执行了插入,则映射的值为值初始化(默认为类类型构造,零初始化,否则),并返回对它的引用
因此,写作只是
std::map<MyString,unsigned short> stringsMap;
.......
stringsMap[ currentString ]++;
std::map stringsMap;
.......
stringsMap[currentString]++;
很好。整个if
块是冗余的,不需要
1) 这不是真的,这是我的重点
但是,默认情况下,short的值不确定
否。对于不存在的键,映射将使用T()
初始化新创建项的值,对于unsigned short
,其有效计算结果为0
见(重点1):
1) 如果键不存在,则插入值类型(键,T())
。这个函数相当于返回insert(std::make_pair(key,T())。first->second代码>
-key\u type
必须满足CopyConstructible
的要求
-mapped\u type
必须满足CopyConstructible
和DefaultConstructible
的要求
如果执行了插入,则映射的值为值初始化(默认为类类型构造,零初始化,否则),并返回对它的引用
因此,写作只是
std::map<MyString,unsigned short> stringsMap;
.......
stringsMap[ currentString ]++;
std::map stringsMap;
.......
stringsMap[currentString]++;
很好。整个if
块是冗余的,不需要
1) 这并不是真的,这是他的重点初始值不是不确定的。是0。value初始化任何缺少的键的值。你能给我一些确认吗?只要去掉整个if
语句。如果[currentString]
不存在,则将添加它并将其值初始化为0,然后对其应用运算符+
。初始值不是不确定的。是0。value初始化任何缺少的键的值。你能给我一些确认吗?只要去掉整个if
语句。如果[currentString]
不存在,则将添加它并将其值初始化为0,然后对其应用运算符+
。