Añadir a un vector de par


Tengo un vector de pair como tal:

vector<pair<string,double>> revenue;

Quiero añadir una cadena y un doble desde un mapa como este:

revenue[i].first = "string";
revenue[i].second = map[i].second;

Pero como los ingresos no se inicializan, se produce un error fuera de límites. Así que intenté usar vector::push_back así:

revenue.push_back("string",map[i].second);

Pero que dice no puede tomar dos argumentos. Entonces, ¿cómo puedo agregar a esto vector de pair?

Author: bltxd, 2011-10-26

7 answers

Uso std::make_pair:

revenue.push_back(std::make_pair("string",map[i].second));
 83
Author: avakar,
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
2013-08-28 21:34:06

En mi humilde opinión, una solución muy buena es usar c++11 emplace_back función:

revenue.emplace_back("string", map[i].second);

Simplemente crea un nuevo elemento en su lugar.

 23
Author: m47h,
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
2014-03-04 15:05:21
revenue.pushback("string",map[i].second);

Pero que dice no puede tomar dos argumentos. Entonces, ¿cómo puedo agregar a este par de vectores?

Estás en el camino correcto, pero piénsalo; ¿qué contiene tu vector? Ciertamente no sostiene una cadena y un int en una posición, sostiene un Pair. Tan...

revenue.push_back( std::make_pair( "string", map[i].second ) );     
 10
Author: Ed S.,
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
2013-04-16 20:01:06

Lea la siguiente documentación:

Http://cplusplus.com/reference/std/utility/make_pair /

O

Http://en.cppreference.com/w/cpp/utility/pair/make_pair

Creo que eso ayudará. Esos sitios son buenos recursos para C++, aunque este último parece ser la referencia preferida en estos días.

 6
Author: hochl,
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
2013-09-26 10:38:06

O puede usar inicializar lista:

revenue.push_back({"string", map[i].second});
 6
Author: Hsu Hau,
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
2015-05-05 14:16:51
revenue.push_back(pair<string,double> ("String",map[i].second));

Esto funcionará.

 2
Author: Caner SAYGIN,
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-01-11 22:22:45

Intenta usar otro par temporal:

pair<string,double> temp;
vector<pair<string,double>> revenue;

// Inside the loop
temp.first = "string";
temp.second = map[i].second;
revenue[i].push_back(temp);
 -1
Author: Dheeraj Kumar,
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
2016-01-17 09:56:39