How to generate a random number in C++?

The most fundamental problem of your test application is that you call srand once and then call rand one time and exit. The whole point of srand function is to initialize the sequence of pseudo-random numbers with a random seed. It means that if you pass the same value to srand in two different applications (with the same srand/rand implementation) then you will get exactly the same sequence of rand() values read after … Read more

std::string to char*

It won’t automatically convert (thank god). You’ll have to use the method c_str() to get the C string version. Note that it returns a const char *; you aren’t allowed to change the C-style string returned by c_str(). If you want to process it you’ll have to copy it first: Or in modern C++:

How to throw a C++ exception

Simple: The Standard Library comes with a nice collection of built-in exception objects you can throw. Keep in mind that you should always throw by value and catch by reference: You can have multiple catch() statements after each try, so you can handle different exception types separately if you want. You can also re-throw exceptions: … Read more

What is a lambda expression in C++11?

The problem C++ includes useful generic functions like std::for_each and std::transform, which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. If you only use f once and in that specific place it seems overkill to … Read more

Iterating over unordered_map C++

From the cplusplus.com page about the begin member function of unordered_map (link): Notice that an unordered_map object makes no guarantees on which specific element is considered its first element. So no, there is no guarantee the elements will be iterated over in the order they were inserted. FYI, you can iterate over an unordered_map more … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)