Mam taki oto fragment kodu
#include <iostream>
#include <vector>
#include <regex>
#include <fstream>
#include <string>
#include <map>
struct Guard
{
int ID;
std::map<int, int> shiftStartingTime;
std::map<int, int> sleepStartingTime;
std::map<int, int> wakesUpTime;
int timeAsleep = 0;
int timeOnShift = 0;
};
int main()
{
std::vector<std::string> Strings;
std::vector<Guard> Guards;
std::ifstream inputFileStream("input.txt");
if (inputFileStream.is_open())
{
std::string buf;
while (getline(inputFileStream, buf))
Strings.push_back(buf);
inputFileStream.close();
}
std::sort(Strings.begin(), Strings.end());
for (const auto &p : Strings)
std::cout << p << std::endl;
std::smatch match;
std::regex regex(R"(\[(\d{4})\-(\d{2})\-(\d{2})\ (\d{2})\:(\d{2})\]\ (wakes up|falls asleep|Guard #(\d{1,4})))");
int currentGuardId = -1;
bool toCreateGuard = true;
std::unique_ptr<Guard> currentGuard = nullptr;
auto currentG = Guards.begin();
for (auto s : Strings)
{
if (std::regex_search(s, match, regex))
{
if (match[6].str().find("Guard") != match[6].str().npos)
{
currentGuardId = stoi(match[7]);
std::pair<int, int> p(std::stoi(match[4]), std::stoi(match[5]));
// lets check is there any guard with loaded ID
for (auto itr = Guards.begin(); itr != Guards.end(); itr++)
{
if (currentGuardId == itr->ID) // if yes, break the loop
{
toCreateGuard = false;
currentGuard = std::make_unique<Guard>(*itr);
currentG = itr;
break;
}
else // if no, add new one
toCreateGuard = true;
}
if (toCreateGuard == true)
{
Guard g{ currentGuardId };
g.shiftStartingTime.insert(p);
Guards.push_back(std::move(g));
currentGuard = std::make_unique<Guard>(*(Guards.rbegin()));
currentG = Guards.end() - 1;
toCreateGuard = false;
}
std::cout << "Guard #" << currentG->ID << " starts shift at: "
<< currentG->shiftStartingTime.rbegin()->first << ":";
if (currentG->shiftStartingTime.rbegin()->second < 10)
std::cout << "0" << currentG->shiftStartingTime.rbegin()->second << std::endl;
else
std::cout << currentG->shiftStartingTime.rbegin()->second << std::endl;
}
else if (match[6] == "falls asleep")
{
std::cout << "Guard #" << currentG->ID << " goes sleep at: " << match[4] << ":";
if (match[5] < 10)
std::cout << "0" << match[5] << std::endl;
else
std::cout << match[5] << std::endl;
// currentG->sleepStartingTime.insert(std::pair<int, int>(std::stoi(match[4]), std::stoi(match[5])));
currentG->sleepStartingTime[std::stoi(match[4])] = std::stoi(match[5]);
}
else if (match[6] == "wakes up")
{
std::cout << "Guard #" << currentG->ID << " wakes up at: " << match[4] << ":";
if (match[5] < 10)
std::cout << "0" << match[5] << std::endl;
else
std::cout << match[5] << std::endl;
//currentG->wakesUpTime.insert(std::pair<int, int>(std::stoi(match[4]), std::stoi(match[5])));
currentG->wakesUpTime[std::stoi(match[4])] = std::stoi(match[5]);
if (currentG->sleepStartingTime.rbegin()->first == currentG->wakesUpTime.rbegin()->first)
currentG->timeAsleep += (currentG->wakesUpTime.rbegin()->second - currentG->sleepStartingTime.rbegin()->second);
else
currentG->timeAsleep += ((59 - currentG->wakesUpTime.rbegin()->second) + currentG->sleepStartingTime.rbegin()->second);
std::cout << "Guard #" << currentG->ID << " has " << currentG->timeAsleep << std::endl;
}
}
}
for (auto g : Guards)
{
std::cout << g.ID << " has " << g.timeAsleep << std::endl;
}
auto g = std::max_element(Guards.begin(), Guards.end(), [](const Guard& x, const Guard& y) {return x.timeAsleep < y.timeAsleep; });
std::cout << g->ID * g->timeAsleep;
std::cin.get();
}
Tworząc strukturę Guard
i potem dodając do listy, tworzę wskaźnik na tą strukturę, problem w tym, że potem nie zmienia to wartości w wektorze tylko wartość wskaźnika jakby. Zmieniłem to na odwołanie się do iteratora i nagle działa.
Druga część, dlaczego nie działa wykomentowana linijka?
//currentG->wakesUpTime.insert(std::pair<int, int>(std::stoi(match[4]), std::stoi(match[5])));
currentG->wakesUpTime[std::stoi(match[4])] = std::stoi(match[5]);