我有一个包含以下代码的 C++ 程序。我想知道是否timeval
像这样从迭代器访问结构的成员:
time_iter->second.tv_sec
是否正确。
static std::map<std::string,struct timeval> name_time_map;
std::map<std::string,struct timeval>::iterator time_iter;
time_iter = name_time_map.find(name);
if (time_iter == name_time_map.end()) {
gettimeofday(&tvPrev, NULL);
name_time_map[name]=tvPrev;
}
else {
print("what inserted values have");
gettimeofday(&tvCurrent, NULL);
diff =
(tvCurrent.tv_usec + 1000000 * tvCurrent.tv_sec) -
(time_iter->second.tv_usec + 1000000 * time_iter->second.tv_sec);
tvDiff.tv_sec = diff / 1000000;
tvDiff.tv_usec = diff % 1000000; // micro seconds
}
我担心的是,鉴于指向预定义结构time_iter->second.tv_sec
,可能不允许使用访问插入的值。second
struct timeval