Fastest way to check if a file exist using standard C++/C++11,14,17/C?

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn’t. Results for total time to run the 100,000 calls averaged over 5 runs, Method Time exists_test0 (ifstream) 0.485s exists_test1 (FILE fopen) 0.302s exists_test2 (posix access()) 0.202s exists_test3 (posix stat()) 0.134s The stat() function provided the … Read more

How to iterate over a vector?

If you have access to C++11 you can use range-based for loops Otherwise you should use begin() and end() You can also use std::begin and std::end (these require C++11 as well) begin will return an iterator to the first element in your vector. end will return an iterator to one element past the end of your vector. So the order in which you get the elements iterating … Read more

Difference in make_shared and normal shared_ptr in C++

The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two. Where do the heap-allocations happen? std::shared_ptr manages two entities: the control block (stores meta data such as ref-counts, type-erased deleter, etc) the object being managed std::make_shared performs a single heap-allocation accounting for the space necessary for both the control block and the data. In the other … Read more

expected expression in C++?

You problem is this: Here only the cout is part of the if statement. The loop is not. You need to add braces around the whole block.

What does the assignment of ~0u to a variable mean in C++?

It indicates bitwise not; all bits in the integer will be flipped, which in this case produces a number where all bits are 1. Note that since it is unsigned, if the integer is widened during assignment the extended bits would be 0. For example assuming that unsigned short is 2 bytes and unsigned int is 4: If you … Read more

C++ delete vector, objects, free memory

You can call clear, and that will destroy all the objects, but that will not free the memory. Looping through the individual elements will not help either (what action would you even propose to take on the objects?) What you can do is this: That will create an empty vector with no memory allocated and … Read more