Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Use the standard library

I tried using vector and map/pairs STL, but since I am not good in STL, I will be more than happy if a non STL solution is presented.

You should rather ask for a detailed standard library based solution ("STL" refers to "standard template library" on which some parts of the standard library are based but which is not the standard library). Honestly, it may take some time in the beginning, but it is totally worth it. Currently, appart from std::min, you are not using anything that is C++-specific.

using namespace std; is evil

using namespace std; is probably the biggest single mistake that you will see in many tutorials all around the internet. It is not something you should do is not something you should do since it may create name problems and very unexpected errors from time to time. Just put std:: in front of every component you import from the standard library.

Order your headers

That's not that important, but you will probably find it easier to check whether you already included some header if you order them in alphabetical ordering:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>

Get rid of dead code

You should try to avoid keeping code that you commented out. You will probably end up deleting it at some point anyway since you won't remember what you wanted to do with it or why it worked/didn't work.

You don't need to return 0; at the end of main

When you program reaches the end of the funciton main, if it did not find any return statement, it will automagically return 0;. Note that this is only true for main, but in your case, that means that you can get rid of this last line.

Use the standard library

I tried using vector and map/pairs STL, but since I am not good in STL, I will be more than happy if a non STL solution is presented.

You should rather ask for a detailed standard library based solution ("STL" refers to "standard template library" on which some parts of the standard library are based but which is not the standard library). Honestly, it may take some time in the beginning, but it is totally worth it. Currently, appart from std::min, you are not using anything that is C++-specific.

using namespace std; is evil

using namespace std; is probably the biggest single mistake that you will see in many tutorials all around the internet. It is not something you should do since it may create name problems and very unexpected errors from time to time. Just put std:: in front of every component you import from the standard library.

Order your headers

That's not that important, but you will probably find it easier to check whether you already included some header if you order them in alphabetical ordering:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>

Get rid of dead code

You should try to avoid keeping code that you commented out. You will probably end up deleting it at some point anyway since you won't remember what you wanted to do with it or why it worked/didn't work.

You don't need to return 0; at the end of main

When you program reaches the end of the funciton main, if it did not find any return statement, it will automagically return 0;. Note that this is only true for main, but in your case, that means that you can get rid of this last line.

Use the standard library

I tried using vector and map/pairs STL, but since I am not good in STL, I will be more than happy if a non STL solution is presented.

You should rather ask for a detailed standard library based solution ("STL" refers to "standard template library" on which some parts of the standard library are based but which is not the standard library). Honestly, it may take some time in the beginning, but it is totally worth it. Currently, appart from std::min, you are not using anything that is C++-specific.

using namespace std; is evil

using namespace std; is probably the biggest single mistake that you will see in many tutorials all around the internet. It is not something you should do since it may create name problems and very unexpected errors from time to time. Just put std:: in front of every component you import from the standard library.

Order your headers

That's not that important, but you will probably find it easier to check whether you already included some header if you order them in alphabetical ordering:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>

Get rid of dead code

You should try to avoid keeping code that you commented out. You will probably end up deleting it at some point anyway since you won't remember what you wanted to do with it or why it worked/didn't work.

You don't need to return 0; at the end of main

When you program reaches the end of the funciton main, if it did not find any return statement, it will automagically return 0;. Note that this is only true for main, but in your case, that means that you can get rid of this last line.

Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132

Use the standard library

I tried using vector and map/pairs STL, but since I am not good in STL, I will be more than happy if a non STL solution is presented.

You should rather ask for a detailed standard library based solution ("STL" refers to "standard template library" on which some parts of the standard library are based but which is not the standard library). Honestly, it may take some time in the beginning, but it is totally worth it. Currently, appart from std::min, you are not using anything that is C++-specific.

using namespace std; is evil

using namespace std; is probably the biggest single mistake that you will see in many tutorials all around the internet. It is not something you should do since it may create name problems and very unexpected errors from time to time. Just put std:: in front of every component you import from the standard library.

Order your headers

That's not that important, but you will probably find it easier to check whether you already included some header if you order them in alphabetical ordering:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>

Get rid of dead code

You should try to avoid keeping code that you commented out. You will probably end up deleting it at some point anyway since you won't remember what you wanted to do with it or why it worked/didn't work.

You don't need to return 0; at the end of main

When you program reaches the end of the funciton main, if it did not find any return statement, it will automagically return 0;. Note that this is only true for main, but in your case, that means that you can get rid of this last line.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /