本文最后更新于39 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com
#include <iostream>
#include <vector>
#include <string>
// 程序员示例代码:简易用户数据处理类
class User {
private:
int id;
std::string name;
public:
// 构造函数
User(int _id, std::string _name) : id(_id), name(_name) {}
// 打印用户信息
void showInfo() const {
std::cout << "用户ID: " << id
<< " | 昵称: " << name
<< std::endl;
}
};
int main() {
// 存储用户容器
std::vector<User> userList;
// 插入测试数据
userList.emplace_back(1, "Nova");
userList.emplace_back(2, "Habit");
userList.emplace_back(3, "Hack");
// 范围for遍历
for (const auto& user : userList) {
user.showInfo();
}
return 0;
}

真棒!😍