我是c ++的新手,我想知道你是否可以在数组中存储对象名,以便稍后使用for循环访问它们。例如:
SELECT s3.userName, s3.taskName, AVG(s3.failCount) AS avgFailCount
FROM (
SELECT s1.userName, s1.taskName, s1.ts, s1.PreviousTS, COALESCE(s2.failCount,0) AS failCount
FROM (
SELECT t1.userName, t1.taskName, t1.ts, LAG(t1.ts) OVER (PARTITION BY t1.userName, t1.taskName ORDER BY t1.ts) AS PreviousTS --ROW_NUMBER() OVER (PARTITION BY t1.userName ORDER BY t1.ts) AS rn
FROM #taskevents t1
WHERE t1.eventName = 'success'
) s1
OUTER APPLY (
SELECT t2.userName, t2.taskName, COUNT(*) AS failCount
FROM #taskevents t2
WHERE t2.eventName = 'fail'
AND t2.userName = s1.userName
AND t2.taskName = s1.taskName
AND t2.ts < s1.ts
AND ( t2.ts >= s1.PreviousTS OR s1.PreviousTS IS NULL )
GROUP BY t2.userName, t2.taskName
) s2
) s3
GROUP BY s3.userName, s3.taskName
答案 0 :(得分:2)
我能想到的最简单的C ++方法是使用std::map<std::string,Class>
:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Class {
public:
string name; // the name of the object
int age; // the age of the object
};
int main() {
std::map<std::string,Class> objrefMap {
{ "Obj1", Class() },
{ "Obj2", Class() } ,
{ "Obj3", Class() }
};
objrefMap["Obj1"].name = "Judy";
objrefMap["Obj1"].age = 29;
objrefMap["Obj2"].name = "Mike";
objrefMap["Obj2"].age = 38;
objrefMap["Obj3"].name = "Dorothy";
objrefMap["Obj3"].age = 19;
for(auto objitem : objrefMap) {
cout << objitem.second.name << endl;
cout << objitem.second.age << endl;
}
}
请参阅live demo。
答案 1 :(得分:0)
我尝试编辑您的代码以满足您的需求。然而,这不是最佳解决方案,需要付出很多努力。你可以改用地图。希望这很有用。
#include <iostream>
using namespace std;
class Class {
public:
string name; // the name of the object
int age; // the age of the object
}; //don't forget the semicolon here.
int main()
{
int nameCount = 0;
int ageCount = 0;
string nameArray[10];
int ageArray[10];
Class Obj1;
Class Obj2;
Class Obj3;
nameArray[nameCount] = Obj1.name = "Judy"; nameCount++;
ageArray[ageCount] = Obj1.age = 29; ageCount++;
nameArray[nameCount] = Obj2.name = "Mike"; nameCount++;
ageArray[ageCount] = Obj2.age = 38; ageCount++;
nameArray[nameCount] = Obj3.name = "Dorothy"; nameCount++;
ageArray[ageCount] = Obj3.age = 19; ageCount++;
for(int i = 0, j = 0; i < nameCount && j < ageCount; i++, j++)
{
cout << nameArray[i] << endl;
cout << ageArray[i] << endl;
}
}
输出:
Judy
29
Mike
38
Dorothy
19