我正在阅读这个问题的公认答案C++ Loop through Map
该答案中的一个示例:
for (auto const& x : symbolTable)
{
std::cout << x.first // string (key)
<< ':'
<< x.second // string's value
<< std::endl ;
}
Run Code Online (Sandbox Code Playgroud)
auto const&
在这种情况下是什么意思?
我有一个字符串,a, banana
我想从字符串中删除空格和逗号。我的代码:
for(auto it = s.begin(); it != s.end(); ++it)
if(!isalpha(*it)) s.erase(it);
Run Code Online (Sandbox Code Playgroud)
但我得到的是A banana
. 如果我只是尝试A banana
然后我得到Abanana
。我不知道为什么前面有逗号时它不删除空格?
我遇到了这个用于计算向量中频率的 C++ 代码。
std::map<std::string, int> countMap;
// Iterate over the vector and store the frequency of each element in map
for (auto & elem : vecOfStrings)
{
auto result = countMap.insert(std::pair<std::string, int>(elem, 1));
if (result.second == false)
result.first->second++;
}
Run Code Online (Sandbox Code Playgroud)
来自https://thispointer.com/c-how-to-find-duplicates-in-a-vector/。我想问一下有什么作用
result.second == false
意思?