0

I'm trying to create a vector that contains various class times. Afterwards, I would compare these times to see which one is earlier through a sorting function.

Edit: after some people mentioned, I do wish to do this with an older version of C++ (prior to 11) because it's what my instructor requested

Would there be a way to do this with push_back?

So far, I have this in my main file:

std::vector<Time> times (Time t1(4,5,4), Time t2(3,5,4));
std::sort(times.begin(), times.end(), IsEarlierThan);

and this in my Time.cpp file:

#include <iostream>
#include "Time.h"

Time::Time() {
    hour = 0;
    minute = 0;
    second = 0;
}

Time::Time(int theHour, int theMinute, int theSecond) {
    hour = theHour;
    minute = theMinute;
    second = theSecond;
}

int Time::getHour() const {
    return hour;
}

int Time::getMinute() const {
    return minute;
}

int Time::getSecond() const {
    return second;
}

bool IsEarlierThan(const Time& t1, const Time& t2){
    if (t1.getHour() < t2.getHour()) return true;
    else if (t1.getHour() == t2.getHour()){
        if (t1.getMinute() < t2.getMinute()) return true;
        else if (t1.getMinute() == t2.getMinute()){
            if(t1.getSecond() < t2.getSecond()) return true;
        }
    } 
    return false;
}

The vector declaration is not correct, so my question would be how would I add these times (including hour, minute, and second) as separate vector values and compare them to each other (eg is 17:23:56 earlier than 19:49:50).

The IsEarlierThan function works, though I am unsure of how to implement it with a vector.

Thanks for any help!

Mariankka
  • 89
  • 3
  • 10
  • 1
    Is this for a class assignment? Because if not, you're needlessly reinventing the wheel, where [``](http://en.cppreference.com/w/cpp/chrono) or [``](http://www.boost.org/doc/libs/1_64_0/doc/html/date_time.html) would be far more elegant choices for Time Representation. – Xirema Jun 23 '17 at 18:37
  • Yes, it is. Hence, why I also have to stick to not using C++ 11 – Mariankka Jun 23 '17 at 19:02

1 Answers1

2

Vector declaration is correct, vector construction is incorrect. std::vector does not have a constructor which accepts two arguments of vector's element type.

If you want to initialize vector with the values from your code - change this line to:

std::vector<Time> times {Time(4,5,4), Time(3,5,4)};

See list initialization for detailed explanation how it works under the hood.

Edit:

For earlier than C++11 stardard - see this post.

Or if you don't care about this explicitly to be a single-statement assingment - just use push_back:

std::vector<Time> times;      // create an empty vector
times.push_back(Time(4,5,4)); // append element to vector
times.push_back(Time(3,5,3));
k.v.
  • 1,193
  • 8
  • 11
  • Using this, my compiler gives me this warning ` warning: extended initializer lists only available with -std=c++11 or -std=gnu++11` - can I only do this with C++ 11? – Mariankka Jun 23 '17 at 18:42
  • @Mariankka Yes, list-initialization is supported only since C++11. If you have no specific reason to stick to earlier standards - I suggest to move to modern version. At this moment C++11 is 'current' and default in most of applications. Nevetheless, if you have to do it oldschool - see [this](https://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) post. Basically you have a choise - either to do a trick with arrays or use `boost::assign` or write your own impl of something `boost::assign`-like. – k.v. Jun 23 '17 at 18:49
  • My instructor requested us to use the older version of C++ for compatibility reasons...Looking at the link you provided, I wonder if there is a way to do this with push_back? – Mariankka Jun 23 '17 at 19:05
  • @Mariankka of course. just contruct the vector with default constructor and then `push_back`. – k.v. Jun 23 '17 at 19:11
  • I tried this but compiler doesn't agree. Any ideas? `std::vector – Mariankka Jun 23 '17 at 19:24
  • @Mariankka edited an answer. you should use *default* constructor - constructor with no arguments - to create an empty `vector`. – k.v. Jun 23 '17 at 19:29
  • Assuming you mean `std::vector – Mariankka Jun 23 '17 at 19:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147489/discussion-between-k-v-and-mariankka). – k.v. Jun 23 '17 at 19:43