Search code examples
c++vectorstdvector

Copy one vector to another in for loop c++


I need to implement a simple version of schedule for monthly tasks. For example payment of electricity bills, subscription fees for communications, etc. I want to implement a set of the following operations:

  • ADD(i,s) - assign a case with name s to day i of the current month.
  • DUMP(i) - Display all tasks scheduled for day i of the current month.
  • NEXT - Go to the to-do list for the new month. When this command is executed, instead of the current (old) to-do list for the current month, a (new) to-do list for the next month is created and becomes active: all tasks from the old to-do list are copied to the new list. After executing this command, the new to-do list and the next month become the current month, and work with the old to-do list is stopped. When moving to a new month, you need to pay attention to the different number of days in months:

if the next month has more days than the current one, the "additional" days must be left empty (not containing cases);

if the next month has fewer days than the current one, cases from all "extra" days must be moved to the last day of the next month.

Basically I have a problem with "NEXT" step. Namely copying from old vector month to the new one. Here is my code:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;



int main(){
    //q - number of operations to perform.
    //day - on which day to plan operation.
    //to_do - what exactly to do on some day.
    //operation - which kind of operation to perform.

    vector<int>day_mon = {31,28,31,30,31,30,31,31,30,31,30,31};
    //m_ind - month index 
    int m_ind = 0;
    int q,day;
    string operation;
    string to_do; 
    
  


    //vector of the current month
    vector<vector<string>> current_month(31);
    
    

    cin >> q;
    
    //for q operations:
    for(int i = 0;i< q;i++){
        cin >> operation;
        if(operation == "NEXT"){
            //insert days in the next months
            vector<vector<string>> next_month = current_month;

            int days_diff = day_mon[m_ind] - day_mon[m_ind+1];
            //if next month has less days as current month, write days into the last day 
            //of the next months
            if(days_diff > 0){
                for(int i = 0; i < days_diff;i++){
                    next_month[day_mon[m_ind]-1].insert(end(next_month[day_mon[m_ind]-1]), begin(next_month[day_mon[m_ind]+i]), end(next_month[day_mon[m_ind]+i]));
                }
            }               
          
        } else if(operation == "ADD"){
            cin >> day >> to_do;
            current_month[day].push_back(to_do);
        } else if(operation == "DUMP"){
            cin >> day;
            for(int i = 0; i < current_month[day].size(); i++){
                cout << current_month[day][i] << ' ';
            }
            cout << endl;
            current_month[day].clear();
        }

    }

    return 0;
    
    
}

My problem is, that I don't know how to convert next_month to current_month(efficiently). How can it be done in c++?


Solution

  • What you need is a set of vectors for each month. Either another vector if you want to number the months 0 - 11, or maybe a map of month name to vector. Lets go with the former

    Also too many nested vector is hard to read. Lets define some types.

    typedef vector<string> day; // the tasks for a day 
    typedef vector<day> month;  // a month
    

    ok so now lets create a year

    vector<month> year(12);
    for (int days : day_mon) {
        year.emplace_back(month(days));
    }
    

    now lets pick january as our current month

    month& current_month = year[0];
    

    and add a task to jan 4th

    current_month[4].push_back("do dishes");
    

    now lets switch month

    current_month = year[5];
    

    .....