Блог пользователя X-O__O-X

Автор X-O__O-X, история, 3 года назад, По-английски

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

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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