In certain problems we can use an array of integers to solve the problem. But those can also be solved by using array of vectors. On which factors should I decide whether to use plain array of integers or array of vectors? If vectors, then what are the advantages?
Can function write in C++ return more than one values?
Can anyone tell me whats exact stack limit for c++ and can we increase it in some way from code?
Is true that vector increasing size by 2,4,8,16...? If it is are vector still increasing after will write resize?
What is complexity of memset(C++)? Is that faster then BF method?
In what type of data do you keep binary tree at your C++ program? And what is the best way to do it?
Is there any difference between char, string, vector in time complexity of processing(input, output...)?
I read about function nth_element on STL. Can anyone help me to implement it because how I can understand it's cool function in linear time complexity.
What is the difference between I write double pi = 3.14159; or #define pi 3.14159?
Is there any link on the Internet on page or book title like: "Hasing for dummies "
2.Yes
actually it's one value .
2.No C++ functions return at most one variable , but there is some tricks :
a-passing variable by reference , change them in the function as you want .
b-return pair<type1,type2> or tuple
c-pointers [to an array for example]
(and of course vectors and maps and ... any collection ) and like so .
Thank you! I didn't know about tuple
Welcome ,
tuple
is since c++11 only , using templates list of types[this list is infinite like in printf] which is also new in c++11.Let's say that you have function that need to calculate sum and product of two numbers. You can do that by using pointers: http://pastebin.com/KACm8QNT With that, you are not returning values with function (you are actually pointing at memory), but it's can be useful when you need to do several operation in the function like i mention above.
What is the difference between I write int pi = 3.14159; or #define pi 3.14159?
Easy. The first pi equals 3, the second could possibly be used as a floating point value. :P
I think he mean double pi = 3.14159
I think ffao knows that but he was joking
9.
double pi = 3.14159
: this is a variable that reserve a 64bit memory , and you can change it inside the program [in short : it's a variable]#define pi 3.14159
: this is a compiler command , that substitute every wordpi
by 3.14159 , it's the same to make an CTRL+H to replace everypi
with 3.14159 , think about it like a shortcuthave a look for this :
Hope I helped
What is difference about memory use?
no memory use for #define it's one of the very first steps of compilation
It's A PRE-processor's job , no memory used because no variable reserve space .
10 . this link
In problems when you need to manipulate with sequence without adding and removing items, I think that it's same if you use vector or array.
I think that you don't like to read books and sources...
4) No, vector increasing size by 2,22,228, and not increasing more
deleted -_-
then why not 228228 ?
I don't know, ask C++ developers
Vectors offer you the ability to resize. If you are uncertain how much memory you will need beforehand, or you need something like pop_back, insert or erase, you should go for a vector. Otherwise, if you only need a plain array with a fixed number of elements, an array works faster than a vector and you should use it.
Return a vector or a pair
I don't believe you can increase it from code (at least for these competitions). This differs between every programming competition website. CodeForces has 256 MB available (source)
Yes. When vector needs more memory than it has currently reserved, it resizes and allocates double the memory it currently uses. If you resize it manually, it can still resize itself if you need more memory (example if you resize it to 10,000, you insert 10,000 members and you decide to push_back again).
Memset operates on a very low level and is therefore much faster, but I am not completely certain how stable is it. If there's any case when you can't use memset, std::fill is faster than a BF with for.
Just like vector vs array, more primitive types work faster (which means char would be a bit faster here)
The first one (double pi = 3.14159) defines it as a variable of type double and stores it in memory while the second one just replaces all instances of pi with 3.14159 in the text. I doubt you could see any difference between the two.
4) Nope. That depends on compiler which growth factor to use. For example for Visual Studio C++ compiler it is 1.5.
Some times ago I've read the following idea on Usenet. By doing some maths, it might look better to use a growth factor less than the golden ratio φ. Let C be the initial allocation and x the growth factor. Then the allocated memory will be C, Cx, Cx2, ... At the n-th allocation, we know that there are of previously freed memory. We would like to reuse that memory for the next allocation of Cxn + 1. The inequation is equivalent to , and thus xn - 1 - xn + 2 + xn + 1 ≥ 0. By dividing by xn we get . As n becomes large, becomes negligible, so that we get the well known inequation - x2 + x + 1 ≥ 0. Finally . In pratice the growth factor 1.5 is quite easy to code (just a statement like
size += size / 2
). Of course it's still theory.For #6, it's really up to you. I would use something like the following:
For #10, I think the Wikipedia article is pretty good: http://en.wikipedia.org/wiki/Hash_table
Vectors are generally used when you don't know the number of elements in advance (for example generate all prime numbers between 1 and 100000 and other similar problems). Vectors are also used (at least by me) in graph representation. Suppose you have 100000 vertices in a graph and the graph has about 500000 edges in total. Then to represent adjacencies in the graph you'd need a matrix of size 100000 x 100000 or you can simply use 100000 vectors each of varying size (one vertex can have 5 adjacencies, another one 500, another 12424...).
It can return only one variable, however that variable can also be container for other variables. For example a vector or a pair.
Depends on your system, but default usually isn't more than 8MB (on codeforces it's 256MB). To increase it you can either increase in the OS itself, or at compile time (depending on what compiler you use). For example in g++ you can use --stack=X, where X is the number of bytes. It's similar in visual c++.
Vector has size (current number of elements in it) and capacity (number of storage spaces allocated to it). If you want to push_back a new element and size < capacity then that element is simply added to the vector. If size == capacity then vector's capacity increase by about 2X (compiler implementation dependant) and the old stuff get's copied to the new vector. If you call resize(x, n) that would simply mean that vector's size (not capacity) becomes n, and any index larger than the current vectors size becomes filled with x. If you know in advance the number of elements that should be in a vector then you should call reserve() function (which changes vector capacity).
Memory in a computer is represented in binary system bit by bit. If you have an array of short ints(2 bytes) for example arr[] = {1, 3} than in binary that is written in memory as "00000000000000010000000000000011". Note that the entire length of this memory segment is 4bytes. So if you call memset(arr, x, 4), 4 bytes will be filled, byte by byte with value x. Value x is in range 0-255 (if it's not it gets converted to unsigned char). So for example if we call memset(arr, 127, 3) (127 = 01111111 in binary) our array becomes "01111111011111110111111100000011" (arr[] = {32639, 32515}. This is way faster than changing numbers individually. You should also read about representations of signed and unsigned numbers here.
Usually people(including me) use array (indexed from 1 to N, N = number of nodes in a complete binary tree) where index i is a parent of 2*i and 2*(i+1). However you can also use an approach with dynamic memory, but many people don't like dynamic memory in programming competitions because it is fairly hard to debug.
String is a class where char[] is, well array of chars. There might be some small overhead in using string vs char[], because it is a class and it has to implement it's own methods and so on... but really for all intents and purpose there shouldn't be any real difference.
Look here and here.
First one replaces every occurrence of pi in your code with 3.14159. So if you have "#define pi 2+1.14159" and then somewhere in your code you write "2*pi" the result is going to be 2*2+1.14159=5.14159 which probably isn't what you've expected. Where as if you write "double pi = 2+1.14159", result of "2*pi" is going to be 6.28318.
This approach is used in programming competitions when it comes to hashing. If you wan't to know more general info about hashing then google and wikipedia.
Apologizes for spelling\grammar errors and I hope i helped :)
2) Function in C can return only one value, but nobody said about structures:
Note that in this case compiller will copy memory of struct.