C++ 使用find方法后如何更新std::map?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4527686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:39:06  来源:igfitidea点击:

How to update std::map after using the find method?

c++mapstlstdmap

提问by jaykumarark

How to update the value of a key in std::mapafter using the findmethod?

std::map使用find方法后如何更新key的值?

I have a map and iterator declaration like this:

我有一个像这样的地图和迭代器声明:

map <char, int> m1;
map <char, int>::iterator m1_it;
typedef pair <char, int> count_pair;

I'm using the map to store the number of occurrences of a character.

我正在使用地图来存储字符出现的次数。

I'm using Visual C++ 2010.

我正在使用 Visual C++ 2010。

回答by James McNellis

std::map::findreturns an iterator to the found element (or to the end()if the element was not found). So long as the mapis not const, you can modify the element pointed to by the iterator:

std::map::find将迭代器返回到找到的元素(end()如果未找到元素,则返回到 )。只要map不是const,就可以修改迭代器指向的元素:

std::map<char, int> m;
m.insert(std::make_pair('c', 0));  // c is for cookie

std::map<char, int>::iterator it = m.find('c'); 
if (it != m.end())
    it->second = 42;

回答by Martin York

I would use the operator[].

我会使用运算符 []。

map <char, int> m1;

m1['G'] ++;  // If the element 'G' does not exist then it is created and 
             // initialized to zero. A reference to the internal value
             // is returned. so that the ++ operator can be applied.

// If 'G' did not exist it now exist and is 1.
// If 'G' had a value of 'n' it now has a value of 'n+1'

So using this technique it becomes really easy to read all the character from a stream and count them:

因此,使用这种技术,从流中读取所有字符并计算它们变得非常容易:

map <char, int>                m1;
std::ifstream                  file("Plop");
std::istreambuf_iterator<char> end;

for(std::istreambuf_iterator<char> loop(file); loop != end; ++loop)
{
    ++m1[*loop]; // prefer prefix increment out of habbit
}

回答by Manish Sogi

You can use std::map::atmember function, it returns a reference to the mapped value of the element identified with key k.

您可以使用std::map::at成员函数,它返回对以键 k 标识的元素的映射值的引用。

std::map<char,int> mymap = {
                               { 'a', 0 },
                               { 'b', 0 },
                           };

  mymap.at('a') = 10;
  mymap.at('b') = 20;

回答by abhinav1602

If you already know the key, you can directly update the value at that key using m[key] = new_value

如果您已经知道密钥,则可以使用直接更新该密钥处的值 m[key] = new_value

Here is a sample code that might help:

这是一个可能有帮助的示例代码:

map<int, int> m;

for(int i=0; i<5; i++)
    m[i] = i;

for(auto it=m.begin(); it!=m.end(); it++)
    cout<<it->second<<" ";
//Output: 0 1 2 3 4

m[4] = 7;  //updating value at key 4 here

cout<<"\n"; //Change line

for(auto it=m.begin(); it!=m.end(); it++)
    cout<<it->second<<" ";
// Output: 0 1 2 3 7    

回答by ZAFIR AHMAD

You can update the value like following

您可以像下面这样更新值

   auto itr = m.find('ch'); 
     if (itr != m.end()){
           (*itr).second = 98;
     }

回答by chunky

You can also do like this-

你也可以这样做——

 std::map<char, int>::iterator it = m.find('c'); 
 if (it != m.end())
 (*it).second = 42;