stderr: terminate called after throwing an instance of 'std::length_error' what(): vector::_M_range_insert

0


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.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

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

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


    //vector of the current month
    vector<vector<string> > current_month(31);
    vector<vector<string> > next_month;
    
    cin >> q;
    
    //for q operations:
    for(int i = 0;i< q;i++){
        cin >> operation;
        if(operation == "NEXT"){
            
            //change next_month index
            if(c_ind == 11){
                n_ind = 0;
            } else{
                n_ind +=1;
            }
            next_month.resize(day_mon[n_ind]);
            days_diff = day_mon[c_ind] - day_mon[n_ind];
            
            //if next month has less days as current month, write days into the last day 
            //of the next months
            if(days_diff > 0){
                copy(current_month.begin(),current_month.begin() + day_mon[n_ind],next_month.begin());
                for(int i = 0; i < days_diff;i++){
                    if(current_month[day_mon[n_ind]+i].empty() == false){
                        next_month[day_mon[n_ind]-1].insert(end(next_month[day_mon[n_ind]-1]), begin(current_month[day_mon[n_ind]+i]), end(current_month[day_mon[n_ind]+i]));
                    }
                    
                }
            } else{
                copy(current_month.begin(),current_month.begin() + day_mon[c_ind],next_month.begin());
            }

            current_month.clear();
            c_ind +=1;
            current_month.resize(next_month.size());
            current_month = next_month;   
            next_month.clear();

            
          
        } else if(operation == "ADD"){
            cin >> day >> to_do;
            current_month[day-1].push_back(to_do);
        } else if(operation == "DUMP"){
            cin >> day;
            cout << current_month[day-1].size() << ' ';
            for(int i = 0; i < current_month[day-1].size(); i++){
                cout << current_month[day-1][i] << ' ';

            }
            cout << endl;
        }

    }

    return 0;
    
    
}


But checker throws me an error: stderr: terminate called after throwing an instance of 'std::length_error'
what(): vector::_M_range_insert

SO the problem is in insert statement. I thought the problem was in the insert of empty vector, but checking empty() vector didn't help.

Where is my mistake?
do you have a line # or input that crashes it? I ran it fine twice ...
Use the debugger to trace through the code so see where the error occurs under what circumstances.
Topic archived. No new replies allowed.