[2015-04-06] Challenge #209 [Easy] The Button can be pressed but once...

Here's my solution in C++: //entry.h #ifndef ENTRY_H #define ENTRY_H #include <string> class Entry { private: std::string name; double secsFromStart; int flair; public: Entry(std::string n, double sec) : name{n}, secsFromStart{sec}, flair{0} {}

    void setFlair(int f) {flair = f;}
    std::string toString() const {
        std::string result{name};
        result.push_back(' ');
        result += std::to_string(flair);
        return result;
    }
    double getSecs() const {return secsFromStart;}
    bool operator <(const Entry& otherEntry) const {
        return secsFromStart < otherEntry.secsFromStart;
    }
};

#endif
//209.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "entry.h"

void readEntries(std::vector<Entry>& entries) {
    unsigned int numberOfEntries{};
    std::cin >> numberOfEntries;
    for (size_t i {}; i < numberOfEntries; i++) {
        std::string name{};
        double secs{};
        std::cin >> name >> secs;
        entries.push_back(Entry(name, secs));
    }
}

void setFlairs(std::vector<Entry>& entries) {
    double lastTime{};
    double currentTime{};
    for (Entry& entry : entries) {
        currentTime = entry.getSecs();
        entry.setFlair(static_cast<int>(60.0 - (currentTime - lastTime)));
        lastTime = currentTime;
    }
}

int main() {
    std::vector<Entry> entries{};
    readEntries(entries);
    std::sort(entries.begin(), entries.end());
    setFlairs(entries);
    auto printEntries = [] (std::vector<Entry>& data) {
        for (const Entry& datum : data)
            std::cout << datum.toString() << std::endl;
    };
    printEntries(entries);
}
/r/dailyprogrammer Thread