¿Cómo actualizar std:: map después de usar el método find?
¿Cómo actualizar el valor de una clave en std::map
después de usar el método find
?
Tengo un mapa y una declaración iteradora como esta:
map <char, int> m1;
map <char, int>::iterator m1_it;
typedef pair <char, int> count_pair;
Estoy usando el mapa para almacenar el número de ocurrencias de un personaje.
Estoy usando Visual C++ 2010.
64
4 answers
std::map::find
devuelve un iterador al elemento encontrado (o al end()
si el elemento no fue encontrado). Mientras el map
no sea const, puede modificar el elemento apuntado por el iterador:
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;
91
Author: James McNellis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-24 18:07:40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-24 18:07:40
Usaría el operador [].
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'
Así que usando esta técnica se vuelve muy fácil leer todos los caracteres de una secuencia y contarlos:
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
}
27
Author: Martin York,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-24 18:20:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-24 18:20:02
Puede usar la función miembro std::map::at
, devuelve una referencia al valor asignado del elemento identificado con la clave k.
std::map<char,int> mymap = {
{ 'a', 0 },
{ 'b', 0 },
};
mymap.at('a') = 10;
mymap.at('b') = 20;
3
Author: Manish Sogi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-27 10:38:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-27 10:38:35
También puedes hacer esto -
std::map<char, int>::iterator it = m.find('c');
if (it != m.end())
(*it).second = 42;
0
Author: chun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-18 19:58:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-18 19:58:31