InRainbows's blog

By InRainbows, history, 12 months ago, In English

Hello everyone! Today I want to write about common C++ mistakes that may seem correct while writing code, but are actually wrong upon deeper inspection. There are only few right now because finding one requires deep experience in coding and often quite specific to certain problems. If you have any suggestions, I can add them to this blog.

1. size_t type is unsigned

When you get the size of some container it returns unsigned integer, not regular integer. It may lead to mistakes like this:

for (int i = 0; i < Ar.size() - 3; i++)

If size of Ar is less than 3, Ar.size() - 3 won't be some negative integer, it will be large positive integer because of how unsigned integers work. You can just turn it to integer using (int)t.size() or ssize(t) (proposed by glowbigger).

2. Common mistake with pointers

Let's say you wanted to create a vector of multiple pointers, and you wrote it like this:

vector<node*> T(N, new node());

This might seem correct but it isn't. Keep in mind that all those pointers will point into one node, not N nodes, so if you change one, it will affect all of them. Instead, you have to add them one by one:

vector<node*> T;
for (int i = 0; i < N; i++) T.push_back(new node());

3. Pragmas may cause WA's (proposed by feev1x)

Remember to add pragmas only when your code is getting TLE, as a possible optimization. But keep in mind it might make your code work incorrectly.

Useful blogs with the same goal:

Some bugs and features looks weird but really exists by OtterZ

Full text and comments »

  • Vote: I like it
  • +54
  • Vote: I do not like it

By InRainbows, history, 2 years ago, In English

Does anybody know how to get variables in TopCoder and return answer using c++? Like in almost in every other judge i use std::cin but in TopCoder it gives you variables like in function. Can anybody send example code?

Thanks to nilucf19 for giving me link to tutorial! Let's consider next example:

Class:	XMarksTheSpot
Method:	countArea
Parameters:	String[]
Returns:	int

So to get variables you should make class with name after "Class: ", within you have to write public function with type written after "Returns: " named after "Method: ", with parameters listed after "Parameters: "

So in this case it should look like this:

class XMarksTheSpot{
public:
    int countArea(vector <string> s){
        
    }
};

Outside class you can write your own templates, defines, global variables, etc. To run your own testcase, you can use Test Panel in web arena and Test Button on applet arena.

More in : https://www.topcoder.com/thrive/articles/competitive-programming-at-topcoder-a-step-by-step-guide

And: https://www.topcoder.com/community/competitive-programming/how-to-compete

Also there's little generator in which you just input first four lines after "Definition" and it creates class named after it:

https://pastebin.com/gSHe6SFU

Sorry for my bad english.

Full text and comments »

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