I've been trying to get my head around maps having never used them before but I'm confused at to how they work. I have a map that stores a string, a start time and an end time, so that you can call start or stop repeatedly on my timer class with a certain name for that timer, and it will keep track of the total time that has passed for the timer with that name
the first item in the map is the timer name, the second is to store the time when StartTimer() was called, and the last item will accumulate a total amount of time passed for this timer.
I want to say in the StartTimer() function something equivalent to "if a timer with this given name exists, set the start time, otherwise add a new element to the map."
and in the StopTimer() function, something equivalent to "if the timer exists, add the amount of time that has passed onto the accumulated time."
The problem I'm having is actually getting at the items in the map, since they're stored in a vector, I can't use std::find on them....very confused >:(
Why do you have a vector of maps? You can store more than one set of data in a map. I think your constructor for the map is wrong; in my reference, the acceptable constructors are
map<key, elem>
and
map <key, elem, sortingOperation>
If you want to store more than one piece of data per key, use a structure.
Just have a single map container. Something like
1 2 3 4 5 6 7
struct times
{
long startTime;
long finishTime;
};
std::map<std::string, times> m_timers;
1) The 3rd template param for map is not what you seem to think. If you want the map to have multiple values for data, you'll need to put those values in a struct:
1 2 3 4 5 6 7 8 9 10
struct Timer
{
std::string name;
long starttime;
long totaltime;
}
//...
std::map< std::string, Timer > m_timers;
2) Why do you have a vector here? Why not just use a single map? Do you have several different groups of timers that you need to keep separate?
void CProfiler::StartTimer(std::string timerName)
{
std::map<std::string,timerInfo>::iterator it = m_timers.find(timerName);
if ( it == m_timers.end() ) // if the name cannot be found in the map
{
m_timers.insert(std::pair<std::string,timerInfo>(timerName, timerInfo(GetTime(),0)));
}
else
{
it->second.startTime = GetTime();
}
std::cout << "starting timer: " << timerName << " - total time is " << it->second.totalTime << "\n";
}
void CProfiler::StopTimer(std::string timerName)
{
std::map<std::string,timerInfo>::iterator it = m_timers.find(timerName);
if ( it != m_timers.end() )
{
it->second.totalTime += GetTime() - it->second.startTime;
std::cout << timerName << " total is " << it->second.totalTime << "\n";
}
std::cout << "stopping timer: " << timerName << " - total time is " << it->second.totalTime << "\n";
}
but the only error I get is an unresolved external
One last thing, when I print out the time in StartTimer(), if it is a new timer the time is printed as some garbage value like -41837251928, but when i stop the timer the time is correct every time, and upon starting a timer the second or third or any other time the time is printed correctly too.