处理课程数
Handling arrays of classes
我遇到了这个问题,您将不得不创建一个数据库类型的程序,该程序根据他们的胜利和损失点在团队名称为字符串的情况下存储各个团队的分数。但是,拥有一系列课程并搜索整个集合以找到确切的团队名称并修改分数非常慢且耗时。有更好,有效的方法吗?
std::map
对此有效。这样的东西。
#include <iostream>
#include <map>
#include <string>
int main()
{
// team name and scores
// std::map has std::pair as its elements
std::map<std::string, int> teams{ {"team1", 300}, {"team2", 500}, {"team3", 100} };
teams["team2"] = 0; // set team2's score to 0
teams["team4"] = 1000; // new team with score of 1000
for (const std::pair<std::string, int>& pair : teams) // auto will work
std::cout << pair.first << ": " << pair.second << 'n';
}
输出:
team1: 300
team2: 0
team3: 100
team4: 1000
我的c 不是最新的,为什么不创建一个提供存储对象类集合/数组的功能的第二类,并提供了从集合中找到对象的清洁功能,将其作为返回参数传递给?
class teams {
int x;
public:
team getTeamByName(char teamName[20]);
// Add other methods to add or remove teams...
private:
team gTeams[10];
};
team getTeamByName(char pTeamName[20]) {
for(int i = 0; i < gTeams.length; i++) {
if gTeams[i].teamName = pTeamName
return gTeams[i];
}
return null;
};
我说我的c 有点生锈,因为一段时间没有触摸它,但是创建包装程序类以包含搜索功能使代码清洁并减少重复代码。
上面的课程也将使自己也管理团队列表,以添加代码以在数组中添加团队并删除它们也将有助于创建干净易于阅读的代码。
如果存在句法错误,请原谅上述代码
p
相关文章:
- 我可以计算多线程数的平均值吗?
- 是否可以限制C++17并行"for_each"的线程数
- 设置 JVM 通过 JNI 初始化时创建的线程数
- 如何修复 OpenMP 程序的 gdb 运行中的线程数
- 使用 C API 更改张量流推理的线程数
- 如何在 OpenMP 中设置线程数
- 最大线程数 - 如何确定C++线程是否并行运行?
- C++的最大线程数
- OpenBLAS 只为一个例程设置线程数
- 关于线程数的奇怪现象
- 在iOS设备上执行并发任务时如何设置正确的线程数?
- OpenMP 并行编程基于线程数与执行时间的关系
- 多线程文件 IO 程序在线程数增加时行为不可预测
- 防止线程在处理异常后在分离时调用 std::terminate()
- 为什么在某些特定情况下具有多个线程(并行处理)会降低性能
- 使用互斥锁将一次运行的线程数限制为 2
- 杀死后的STD ::线程如何处理
- 为什么OMP_SET_DYNAMIC(1)切勿调整线程数(在Visual C 中)
- 在 for 循环中创建多个线程以处理 2D 数组
- 处理字符串的最佳线程数