c++ - C++ variable_name.attribute = x
问题描述
我看过这个,但我找不到他在做什么。我特别感兴趣computers[I].name = 'ever'
:你能在 C++ 中有一个 variable_name.attribute 吗?例如 student.grade = 6, student.name = "Tedd" 等等?
解决方案
当然,为什么不:
#include <string>
struct Computer {
std::string name;
};
int main() {
Computer computers[100];
for (auto& cp : computers) {
cp.name = "ever";
}
}
这是您感兴趣的学生示例:
#include <string>
#include <iostream>
struct Student {
std::string name;
double grade;
};
int main() {
Student students[3];
students[0].name = "s1";
students[0].grade = 95.7;
students[1].name = "s2";
students[1].grade = 96.6;
students[2].name = "s3";
students[2].grade = 77.4;
for (Student const& s : students) {
std::cout << s.name << " : " << s.grade << '\n';
}
}
悬停执行
s1:95.7
s2:96.6
s3:77.4
推荐阅读
- javascript - 如何在 symfony 中反序列化查询序列化表单
- android - 了解newrelic响应时间
- javascript - 理解:React 状态更新时机
- php - 日期列是 varchar 如何选择从 01-01-2019 到今天的数据
- php - 向 PHP 添加关键字和描述
- matlab - 如何读取文本文件的所有列,Matlab 中的最后一列除外
- java - 如何使用 DocusignApi 设置签名顺序
- c++ - 如何在 Qt 上实现 GDAL 库?
- javascript - 为什么移动设备在使用语音合成时对相同的语言使用相同的声音?
- mysql - 迁移失败,出现警告“'utf8' 当前是字符集 UTF8MB3 的别名,但在未来版本中将是 UTF8MB4 的别名”