c++ - Using structure binding to change the value of a custom structure -


i trying find out way change value of custom structure using structured binding .i able std::map.i referred few materials structured binding

in below code able change value of map . want change value of unsigned salary defaulted 1000 10000

#include<iostream> #include<string> #include<vector> #include<map>  struct employee {     unsigned id;     int roll;     std::string name;     std::string role;     unsigned salary=1000; }; int main() {    std::map<std::string, int> animal_population {         {"humans", 10},         {"chickens", 11},         {"camels", 12},         {"sheep", 13},     };     std::cout<<"before change"<<'\n';     (const auto &[species, count] : animal_population)     {         std::cout << "there " << count << " " << species         << " on planet.\n";     }     (const auto &[species, count] : animal_population)      {         if (species=="humans")         {             animal_population[species]=2000;         }       }      std::cout<<"after change"<<'\n';      (const auto &[species, count] : animal_population)      {             std::cout << "there " << count << " " << species             << " on planet.\n";      }      std::vector<employee> employees(4);     employees[0].id = 1;     employees[0].name = "hari";     employees[1].id = 2;     employees[1].name = "om";       (const auto &[id,roll,name,role,salary] : employees) {         std::cout << "name: " << name<<'\n'         << "role: " << role<<'\n'         << "salary: " << salary << '\n';     }  } 

output

before change there 12 camels on planet. there 11 chickens on planet. there 10 humans on planet. there 13 sheep on planet. after change there 12 camels on planet. there 11 chickens on planet. there 2000 humans on planet. there 13 sheep on planet. name: hari role:  salary: 1000 name: om role:  salary: 1000 name:  role:  salary: 1000 name:  role:  salary: 1000 

change tried expected output

error got

cannot assign variable 'salary' const-qualified type 'const int'

for (const auto &[id,roll,name,role,salary] : employees) {     //employees[].salary = 10000; //not working     // salary = 10000;            //not working     std::cout << "name: " << name<<'\n'     << "role: " << role<<'\n'     << "salary: " << salary << '\n'; } 

expected output

before change there 12 camels on planet. there 11 chickens on planet. there 10 humans on planet. there 13 sheep on planet. after change there 12 camels on planet. there 11 chickens on planet. there 2000 humans on planet. there 13 sheep on planet. name: hari role:  salary: 10000 name: om role:  salary: 10000 name:  role:  salary: 10000 name:  role:  salary: 10000 

thanks in advance solution , suggestion

problem values have const cvalifier. unmodificable.

remove const , use reference & can modify these variables.

for (auto &[id,roll,name,role,salary] : employees) {     salary = 10000;     std::cout << "name: " << name<<'\n'     << "role: " << role<<'\n'     << "salary: " << salary << '\n'; } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -