598 Answered Questions for the topic C++

C++

07/03/19

Is there any advantage of using map over unordered_map in case of trivial keys?

A recent talk about `unordered_map` in C++ made me realize, that I should use `unordered_map` for most cases where I used `map` before, because of the efficiency of lookup ( *amortized O(1)* vs.... more
C++

07/03/19

Printing 1 to 1000 without loop or conditionals?

**Task**: Print numbers from 1 to 1000 without using any loop or conditional statements. Don't just write the `printf()` or `cout` statement 1000 times. How would you do that using C or C++?
C++

07/01/19

Why use static_cast<int>(x) instead of (int)x?

I've heard that the `static_cast` function should be preferred to C-style or simple function-style casting. Is this true? Why?
C++

06/30/19

What are POD types in C++?

I've come across this term POD-type a few times. What does it mean?
C++

06/30/19

Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wiki/Sign_function It's easy enough to write my own, but it seems like something that... more

06/29/19

Force all && to be executed?

Consider the following variadic function template <typename Type, typename... Types> bool f(Type& arg, Types&... args) { return f(arg) && f(args...); } template... more
C++

06/29/19

Why is enum class preferred over plain enum?

I heard a few people recommending to use enum *classes* in C++ because of their **type safety**. But what does that really mean?
C++

06/28/19

Where to put default parameter value in C++?

What's the place for the default parameter value? Just in function definition, or declaration, or both places?
C++

06/27/19

In C++, what is a virtual base class?

I want to know what a "*virtual base class*" is and what it means. Let me show an example: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo ... more

06/26/19

Searching an Array for an Exact Match in C++

Ensure the provided code file named MichiganCities.cpp is open. Study the prewritten code to make sure you understand it. Write a loop statement that examines the names of cities stored in the... more

06/26/19

Digit-increasing number test?

A number is called digit-increasing if it is equal n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2). Actually, a friend... more
C++

06/25/19

Debugging with command-line parameters in Visual Studio?

I'm developing a C++ command-line application in Visual Studio and need to debug it with command-line arguments. At the moment I just run the generated EXE file with the arguments I need (like this... more
C++

06/25/19

Is it possible to print a variable's type in standard C++?

For example: int a = 12; cout << typeof(a) << endl; Expected output: int
C++

06/25/19

Difference between `constexpr` and `const`?

What's the difference between `constexpr` and `const`? - When can I use only one of them? - When can I use both and how should I choose one?
C++

06/22/19

Meaning of 'const' last in a function declaration of a class?

What is the meaning of `const` in declarations like these? The `const` confuses me. class foobar { public: operator int () const; const char* foo() const; };

06/21/19

Can we implement a doubly-linked list using a single pointer?

I want to use a structure like: struct node { char[10] tag; struct node *next; }; I want to use the above structure to create a doubly-linked list. Is that possible and if yes, then how I can... more
C++

06/21/19

std::wstring VS std::string?

I am not able to understand the differences between `std::string` and `std::wstring`. I know `wstring` supports wide characters such as Unicode characters. I have got the following questions: 1.... more
C++

06/19/19

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

I would like to find the fastest way to check if a file exist in standard C++11, C++, or C. I have thousands of files and before doing something on them I need to check if all of them exist. What... more
C++

06/18/19

Is < faster than <=?

I'm reading a book where the author says that `if( a < 901 )` is faster than `if( a <= 900 )`. Not exactly as in this simple example, but there are slight performance changes on loop... more
C++

06/16/19

What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this: int main(int argc, char *argv[]) When I code C++ without an IDE, just with a command line... more
C++

06/16/19

Sleep for milliseconds?

I know the POSIX `sleep(x)` function makes the program sleep for x seconds. Is there a function to make the program sleep for x *milliseconds* in C++?
C++

06/11/19

How to initialize private static members in C++?

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors: class foo { private: static int i; ... more
C++

06/09/19

How to implement the factory method pattern in C++ correctly?

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: **How do I implement... more
C++

06/09/19

What is a segmentation fault?

What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
C++

06/08/19

Do I need to explicitly call the base virtual destructor?

When overriding a class in C++ (with a virtual destructor) I am implementing the destructor again as virtual on the inheriting class, but do I need to call the base destructor? If so I imagine... more

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.