X-O__O-X's blog

By X-O__O-X, history, 4 years ago, In English

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

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Well, you can reduce the time complexity by passing (constant reference variable) or (reference variable) when struct is big enough then your chance of being TLE will be reduced too

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Destructors of all STL containers are called automatically. The only things you need to delete are any pointers you may have used.