1
\$\begingroup\$

I need to add an offset to a time variable inside my class. The code below works, how do i make it more efficient, and elegant.

#include <array>
#include <iostream>
#include <string>
#include <vector>

class Pressure
{
    public:

    Pressure(int date, int time):m_date(date),m_time(time)
    {

    }

    int GetDate()
    {
        return m_date;
    }

    int GetTime()
    {
        return m_time;
    }

    void AddTime(int val)
    {
        m_time += val;
    }

    private:
    int m_date;
    int m_time;

};

int main()
{  
  std::vector<Pressure> p;

  // populate the vector with values.
  for(int i=0; i < 10; i++)
  {
    Pressure x(i,i);
    p.push_back(x);
  }

  int offset = 20000;

  // add the offset 
  for(int i=0; i < 10; i++)
  {
      p[i].AddTime(offset);
  }

  // display the data
  for(int i=0; i < 10; i++)
  {
    std::cout << p[i].GetDate() << " "  << p[i].GetTime() << "\n";
  }

}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ This sounds like an XY problem. Which is going to be difficult to help with unless when know the actual problem you are trying to optimize for. \$\endgroup\$ Oct 16 '19 at 12:31
  • \$\begingroup\$ there is probably a one liner way using stl to add a data of type T to a member variable of a class stored in a vector. \$\endgroup\$ Oct 16 '19 at 12:36
  • 2
    \$\begingroup\$ What are the units and valid ranges of date and time here? There are no clues in the class nor in the test program, so it's virtually impossible to review. \$\endgroup\$ Oct 16 '19 at 12:57
  • \$\begingroup\$ its just seconds since epoch represented as int. \$\endgroup\$ Oct 16 '19 at 13:00
  • \$\begingroup\$ Do you mean that days is number of days since some epoch, and that time is number of seconds in range 0-86400? (Where 86400 is used only when there's a leap second). Are we supposed to carry to the day field when the seconds overflows that range? I don't see code to do that, and it's not clear what the requirements are. \$\endgroup\$ Oct 16 '19 at 13:59
3
\$\begingroup\$

You could use a range-based for loop and push_back( Pressure(i,i) ).

#include <iostream>
#include <vector>

class Pressure {
public:
    Pressure(int date, int time) : m_date(date), m_time(time) {

    }

    int GetDate() const {
        return m_date;
    }

    int GetTime() const {
        return m_time;
    }

    void AddTime(int val) {
        m_time += val;
    }
private:
    int m_date;
    int m_time;
};

int main() {
    std::vector<Pressure> p;

    // populate the vector with values.
    p.reserve(10); // you can reserve space if you know the size ahead of time to prevent extra reallocations, but it isnt necessary for a vector this small.
    for(int i = 0; i < 10; ++i) {
        p.push_back( Pressure(i,i) );
    }

    for(Pressure& pressure : p) {
        // add offset
        pressure.AddTime(20000);

        // display data
        std::cout << pressure.GetDate() << " " << pressure.GetTime() << "\n";
    }

    return 0;
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.