我正在使用GetLocalTime(&time)
函数在visual C++
中获取时间。现在我需要在输入变高时保存时间,在另一个结构中,以便我可以对存储的时间进行进一步的计算。
SYSTEMTIME time;
if(input==high)
{
count++; //Incrementing a counter to count the number of times input is high
//Depending upon this count, I need to save value of time in a new variable
//using GetLocalTime to get current time
GetLocalTime(&time);
}
如何根据计数值存储当前时间。就像count为1时,这意味着第一次输入很高,因此将其存储在a1
中。如果count为2,则将时间存储在a2
中。如果count为5,则将时间存储在a5
中。我不能使用开关,因为情况不固定,可能有很多计数。我可以用什么其他逻辑来节省结构中的时间。
答案 0 :(得分:3)
您应该使用容器(例如std::vector
)来存储时间值。
std::vector<SYSTEMTIME> savedTimes;
SYSTEMTIME time;
if (input == high)
{
//count++; //count is redundant now, you can later just check savedTimes.size()
GetLocalTime(&time);
savedTimes.push_back(time); //will add another value to the end of the vector.
}
对您存储的时间做一些事情:
for (auto it = savedTimes.begin(); it != savedTimes.end(); ++it)
{
SYSTEMTIME time = *it;
//whatever...
}
或
for (int i = 0; i < savedTimes.size(); i++)
{
SYSTEMTIME time = savedTimes[i];
//whatever...
}
答案 1 :(得分:-1)
我通过使用结构数组解决了这个问题。
struct foo
{
WORD Year;
WORD Month;
WORD DayOfWeek;
WORD Day;
WORD Hour;
WORD Minute;
WORD Second;
WORD Milliseconds;
};
struct foo Foo[20]
现在只要输入高:
if(input==high)
{
count++;
GetLocalTime(&time);
//This will copy complete time in Foo[0].
//So next time input is high, time will get copied in Foo[1] and this will keep on going..
memcpy(&Foo[count],&time,sizeof(Foo));
}
现在我想知道如何清除结构Foo
。?