使用结构绑定更改自定义结构的值
Using structure binding to change the value of a custom structure
我正在尝试找到一种使用结构化绑定更改自定义结构值的方法。我能够用std::map
.我参考了一些材料 结构化绑定
在下面的代码中,我能够更改地图的值。我想将unsigned salary
的值从默认值 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 the change"<<'n';
for (const auto &[species, count] : animal_population)
{
std::cout << "There are " << count << " " << species
<< " on this planet.n";
}
for (const auto &[species, count] : animal_population)
{
if (species=="humans")
{
animal_population[species]=2000;
}
}
std::cout<<"After the change"<<'n';
for (const auto &[species, count] : animal_population)
{
std::cout << "There are " << count << " " << species
<< " on this planet.n";
}
std::vector<employee> employees(4);
employees[0].id = 1;
employees[0].name = "hari";
employees[1].id = 2;
employees[1].name = "om";
for (const auto &[id,roll,name,role,salary] : employees) {
std::cout << "Name: " << name<<'n'
<< "Role: " << role<<'n'
<< "Salary: " << salary << 'n';
}
}
输出
Before the change
There are 12 camels on this planet.
There are 11 chickens on this planet.
There are 10 humans on this planet.
There are 13 sheep on this planet.
After the change
There are 12 camels on this planet.
There are 11 chickens on this planet.
There are 2000 humans on this planet.
There are 13 sheep on this planet.
Name: hari
Role:
Salary: 1000
Name: om
Role:
Salary: 1000
Name:
Role:
Salary: 1000
Name:
Role:
Salary: 1000
更改我试图获得预期的输出
我得到的错误
不能分配给具有常量合格类型"const 的变量"薪水" 国际'
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';
}
预期输出
Before the change
There are 12 camels on this planet.
There are 11 chickens on this planet.
There are 10 humans on this planet.
There are 13 sheep on this planet.
After the change
There are 12 camels on this planet.
There are 11 chickens on this planet.
There are 2000 humans on this planet.
There are 13 sheep on this planet.
Name: hari
Role:
Salary: 10000
Name: om
Role:
Salary: 10000
Name:
Role:
Salary: 10000
Name:
Role:
Salary: 10000
提前感谢任何解决方案和建议
问题是你的值const
cvalifier。它们是不可修改的。
删除const
并使用引用&
以便可以修改这些变量。
for (auto &[id,roll,name,role,salary] : employees) {
salary = 10000;
std::cout << "Name: " << name<<'n'
<< "Role: " << role<<'n'
<< "Salary: " << salary << 'n';
}
相关文章:
- 通过 NIF 从C++返回自定义数据结构
- C++ 通过函数传递自定义结构
- 在C++中循环访问自定义结构列表的小问题
- 自定义数据结构的优点是什么?
- 如何在Qt中使用QDataStream将自定义结构保存/加载到二进制文件中?
- 为什么在类或结构中传递自定义比较器函数?
- 如何在自定义 LLVM 传递之间正确传递数据结构
- 结构向量中自定义结构函数的内存使用
- 类型转换自定义结构
- 使用结构绑定更改自定义结构的值
- 信号槽自定义结构问题
- 如何将unordered_set与自定义结构一起使用?
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- 通过自定义结构和链接列表类中的C 中的哈希表
- 从自定义结构数组中提取数据
- 将 OpenGL VBO 与自定义类/数据结构一起使用
- 无法使用自定义结构的属性
- 通过引用将自定义结构的向量传递给 boost::compute 闭包或函数
- 自定义链表创建 RtlValidateHeap 错误,其结构具有链表
- 如何为C++类和结构自定义 STAssertEquals 输出