const int = int const?

They are both valid code and they are both equivalent. For a pointer type though they are both valid code but not equivalent. Declares 2 ints which are constant: Declares a pointer whose data cannot be changed through the pointer: Declares a pointer who cannot be changed to point to something else:

How to concatenate two strings in C++?

First of all, don’t use char* or char[N]. Use std::string, then everything else becomes so easy! Examples, Easy, isn’t it? Now if you need char const * for some reason, such as when you want to pass to some function, then you can do this: assuming this function is declared as: Explore std::string yourself starting from here: Documentation of std::string Hope that helps.

Segmentation fault error 11 C++

So I’m getting a segmentation fault error in the beginning of the code. I’ve tried running some tests at the different points and the error seems to be when i allocate memory for the array. Ive just started learning about heap and stack memory so I’m not really sure if I’m doing something wrong there. … Read more

Iterate through a C++ Vector using a ‘for’ loop

Is there any reason I don’t see this in C++? Is it bad practice? No. It is not a bad practice, but the following approach renders your code certain flexibility. Usually, pre-C++11 the code for iterating over container elements uses iterators, something like: This is because it makes the code more flexible. All standard library containers … Read more

Fastest JSON reader/writer for C++

http://lloyd.github.com/yajl/ http://www.digip.org/jansson/ Don’t really know how they compare for speed, but the first one looks like the right idea for scaling to really big JSON data, since it parses only a small chunk at a time so they don’t need to hold all the data in memory at once (This can be faster or slower … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to and then include the necessary header file: and the value of pi can be accessed via: In my math.h (2014) it is defined as: but check your math.h for more. An extract from the “old” math.h (in 2009): However: on newer platforms (at least on my 64 bit … Read more

‘cout’ was not declared in this scope

Put the following code before int main(): And you will be able to use cout. For example: Now take a moment and read up on what cout is and what is going on here: http://www.cplusplus.com/reference/iostream/cout/ Further, while its quick to do and it works, this is not exactly a good advice to simply add using namespace std; at the top … Read more

streambuf::xsgetn and state flags

This is what I found on libstdc++: When the characters extracted are less than the number of characters requested, this function calls uflow() to obtain more characters. If that function returns Traits::eof() then it will simply return whether or not 0 characters were extracted. The result of the function call is picked up by the higher-level stream operations that … Read more