0

In the book "Competitive programming 3", by the Halim brothers, it is stated that it would be best to insert all your macro's, includes and typedefs in a seperate file called 'competitive.h'. Then all you would have to do is include 'competitive.h' when you start coding. As I see it, this would work on my machine, but since I can only submit 1 file at once to a judge, it wouldn't work there. Is there any way I would go about doing this in C++?

Thanks.

Nico Ekkart
  • 33
  • 1
  • 5

5 Answers5

2

Just run your c++ file through the c preprocessor using:

cpp myfile.cpp > myfileprocessed.cpp

to embed any included headers into the file directly.

EDIT:

Sorry, just noticed another similar answer was posted at the same time, shall leave this here just as it highlights both ways of invoking the preprocessor.

Vality
  • 6,577
  • 3
  • 27
  • 48
  • 1
    That might work with one particular compiler, depending on how it is installed, but it's not a general solution. (Not to mention that submitting the preprocessed output isn't going to work anyway.) – James Kanze Aug 13 '14 at 17:41
2

I don't think that's a good approach. gcc -E can sometimes output a very large file and quite confusing. You should find a decent text editor which supports insertion of a skeleton code. Here's how to do it with Vim:

It might be nice to hide the skeleton part with a kind of "folding" feature of your editor.

I think Emacs supports both of them. No idea for Sublime.

The #include approach is not flexible in that you cannot easily modify some part of the codes in the common file specifically for only one of the problems set.

Community
  • 1
  • 1
nodakai
  • 7,773
  • 3
  • 30
  • 60
  • Any editor can use a template with the following simple approach: (1) Open template. (2) "Save as" new filename. – Ben Voigt Aug 13 '14 at 17:17
  • This is what I have been doing with Sublime Text. I was just wondering because of the book. – Nico Ekkart Aug 13 '14 at 18:37
  • With Sublime Text, you can play with snippets though. This allow for example to navigate through some fields that need to be customized with each new files. That's what I did for LaTeX, I can then tab through the name of the file, do I want a title page, such things... There are also packages like FileHeader that can probably be modified to do what you want. – Xælias Aug 13 '14 at 19:39
  • The only thing I can't seem to do with ST2 is automatically folding the top of the snippet. – Nico Ekkart Aug 13 '14 at 20:46
0
#include<bits/stdc++.h>

This header file includes the most frequently used header files. But it's a bit slow since it includes a lot of header files.

In competitive programming, I generally use this header file for simplicity.
But for faster IO you can add the code.

ios_base::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0) ;
Nur Bijoy
  • 70
  • 2
  • 7
0

I would do a

#ifdef LOCAL
#include "competitive.h"
#else
#include <bits/stdc++.h>
#endif

This way, only on your local machine does it run

#include "competitive.h"

Make sure to add

-DLOCAL

to your compiler flags.

Greg
  • 69
  • 6
  • Presumably you want to actually use the contents of `competitive.h` when you submit, though. I imagine it'd contain macros not available in bits, otherwise we'd just include bits and be done with it. – ggorlen Jun 08 '22 at 21:20
0

you can use #ifdef & #ifndef to solve this problem. if you write something inside #ifdef and import some condition it will run that command only when the condition fulfil.In code mention below it will include competitive.h file only it will run in local machine and whenver you will submit it online it will not include that file. ``

#ifndef ONLINE_JUDGE
#include "competitive.h"
#endif
#ifdef ONLINE_JUDGE
#include<bits/stdc++.h>
#endif