Rating changes for the last round are temporarily rolled back. They will be returned soon. ×

Implementation using structs

Revision en1, by X-O__O-X, 2020-11-08 16:56:19

Recently, I use the following code a lot while solving graph problems with multiple test cases.

#include <bits/stdc++.h>
using namespace std; 
struct problem {
    // ... 
    void init() {}
    void solve() {}
};
int main () {
    int tc; 
    cin >> tc; 
    while (tc--) {
        problem p; 
        p.init(); 
        p.solve(); 
    }
}

I also use struct to represent graphs for example. It helps me organize my thoughts while coding. also I don't have to worry about what data to initialize before each test case. The problem is:

1- I don't know exactly what the code does. For example, will memory allocated at the beginning of a test case be automatically deallocated at the end of the test case? if not, can it cause TLE ?

2- In what situations may this way of coding cause problems ? and generally should I keep doing this?

Thanks

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English X-O__O-X 2020-11-08 16:56:19 928 Initial revision (published)