How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

Use rfind overload that takes the search position pos parameter, and pass zero for it: Who needs anything else? Pure STL! Many have misread this to mean “search backwards through the whole string looking for the prefix”. That would give the wrong result (e.g. string(“tititito”).rfind(“titi”) returns 2 so when compared against == 0 would return false) and it would be inefficient (looking through … Read more

Random not declared in scope

random is not a standard C++ function; it’s a POSIX function, so it’s not available on Windows. Use rand instead, or better, the new C++11 randomness library.

Function definition not found for a function declared inside unnamed namespace – how to resolve? (Visual Studio 2015)

The green squiggles don’t tell you that there’s an error, they tell you that there’s an opportunity for the new refactoring tools to do something. (Red squiggles indicate an error.) In this case, they’re informing you that there is no definition matching the declaration of test2, so the IDE is offering to generate one. This happens … Read more

to_string not declared in scope

There could be different reasons why it doesn’t work for you: perhaps you need to qualify the name with std::, or perhaps you do not have C++11 support. This works, provided you have C++11 support: To enable C++11 support with g++ or clang, you need to pass the option -std=c++0x. You can also use -std=c++11 on the newer versions … Read more

Reading string by char till end of line C/C++

You want to use single quotes: Double quotes (“) are for strings, which are sequences of characters. Single quotes (‘) are for individual characters. However, the end-of-line is represented by the newline character, which is ‘\n’. Note that in both cases, the backslash is not part of the character, but just a way you represent … Read more

Resizing dynamic array in c++

A() is not modifying nums to point at the new array. Even if it were, it is deleting the new array, so nums would end up pointing at invalid memory. You need to declare the arr parameter as a reference, and delete the old array instead of the new array: For what you are attempting, I think you have too much indirection. … Read more

Hash function for a string

First, it usually does not matter that much in practice. Most hash functions are “good enough”. But if you really care, you should know that it is a research subject by itself. There are thousand of papers about that. You can still get a PhD today by studying & designing hashing algorithms. Your second hash … Read more

How to get current time in milliseconds?

Simply use std::chrono. The general example below times the task “of printing 1000 stars”: Instead of printing the stars, you will place your sorting algorithm there and time measure it. Do not forget to enable the optimization flags for your compiler, if you intend to do some benchmarking, e.g. for g++, you need -O3. This is serious, check … Read more

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