Questions tagged [c++]

C++ is a general-purpose programming language. Use this tag for questions about/utilizing C++. Do not also tag questions with [c] unless you have a good reason. C and C++ are different languages. Use a versioned tag such as [c++11], [c++20] etc. for questions specific to a standard revision.

Filter by
Sorted by
Tagged with
27241 votes
25 answers
1.9m views

Why is processing a sorted array faster than processing an unsorted array?

In this C++ code, sorting the data (before the timed region) makes the primary loop ~6x faster: #include <algorithm> #include <ctime> #include <iostream> int main() { // ...
GManNickG's user avatar
  • 499k
10154 votes
26 answers
1.0m views

What is the '-->' operator in C/C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. ...
4230 votes
1 answer
3.2m views

The Definitive C++ Book Guide and List

This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from ...
3911 votes
44 answers
1.2m views

What are the differences between a pointer variable and a reference variable?

What is the difference between a pointer variable and a reference variable?
prakash's user avatar
  • 59.4k
3664 votes
11 answers
1.2m views

What does the explicit keyword mean?

What does the explicit keyword mean in C++?
Skizz's user avatar
  • 70.3k
3385 votes
41 answers
1.2m views

What's the problem with "using namespace std;"?

I have heard using namespace std; is wrong, and that I should use std::cout and std::cin directly instead. Why is this? Does it risk declaring variables that share the same name as something in the ...
akbiggs's user avatar
  • 34.6k
3358 votes
83 answers
2.4m views

How do I iterate over the words of a string?

How do I iterate over the words of a string composed of words separated by whitespace? Note that I'm not interested in C string functions or that kind of character manipulation/access. I prefer ...
3142 votes
27 answers
1.7m views

How to set, clear, and toggle a single bit

How can I set, clear, and toggle a bit?
JeffV's user avatar
  • 53.8k
3125 votes
12 answers
775k views

When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast (type)value (C-style cast) type(value) (function-style cast) How does one decide which to use in which specific ...
e.James's user avatar
  • 118k
3095 votes
30 answers
905k views

What is the difference between #include <filename> and #include "filename"?

What is the difference between using angle brackets and quotes in an include directive? #include <filename> #include "filename"
quest49's user avatar
  • 50.3k
2531 votes
8 answers
382k views

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
fredoverflow's user avatar
2459 votes
10 answers
1.0m views

What are the basic rules and idioms for operator overloading?

Note: This question and the original answers are from 2010 and partially outdated. Most of it is still good and helpful, but the original text no longer covers everything there is to know about C++ ...
sbi's user avatar
  • 222k
2439 votes
11 answers
259k views

Why are elementwise additions much faster in separate loops than in a combined loop?

Suppose a1, b1, c1, and d1 point to heap memory, and my numerical code has the following core loop. const int n = 100000; for (int j = 0; j < n; j++) { a1[j] += b1[j]; c1[j] += d1[j]; } ...
Johannes Gerer's user avatar
2387 votes
5 answers
501k views

What is the copy-and-swap idiom?

What is the copy-and-swap idiom and when should it be used? What problems does it solve? Does it change for C++11? Related: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor ...
GManNickG's user avatar
  • 499k
2281 votes
19 answers
796k views

Why can templates only be implemented in the header file?

Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (...
MainID's user avatar
  • 29.6k
2204 votes
8 answers
295k views

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming? This article (by Gavin Clarke who quotes Herb Sutter) says that, The ...
Sarfaraz Nawaz's user avatar
2180 votes
10 answers
314k views

Why is reading lines from stdin much slower in C++ than Python?

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is ...
2164 votes
14 answers
732k views

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one?
Alex Reynolds's user avatar
2150 votes
18 answers
1.2m views

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum's user avatar
  • 23.1k
2149 votes
20 answers
715k views

How do I profile C++ code running on Linux?

How do I find areas of my code that run slowly in a C++ application running on Linux?
Gabriel Isenberg's user avatar
2129 votes
29 answers
4.7m views

How to convert int to string in C++?

How can I convert from int to the equivalent string in C++? I am aware of two methods. Is there another way? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; ...
Nemo's user avatar
  • 24.9k
2094 votes
12 answers
606k views

What is move semantics?

I've just finished listening to the Software Engineering radio podcast interview with Scott Meyers regarding C++11. Most of the new features made sense to me, with the exception of one. I still don't ...
dicroce's user avatar
  • 46.1k
2046 votes
8 answers
911k views

Regular cast vs. static_cast vs. dynamic_cast [duplicate]

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e. MyClass *m = (MyClass *)...
Graeme Perrow's user avatar
1910 votes
23 answers
241k views

Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

One of the most interesting projects I've worked on in the past couple of years was a project about image processing. The goal was to develop a system to be able to recognize Coca-Cola 'cans' (note ...
Charles Menguy's user avatar
1896 votes
21 answers
923k views

When to use virtual destructors?

I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object in ...
Lodle's user avatar
  • 31.7k
1894 votes
24 answers
425k views

Why should I use a pointer rather than the object itself?

I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for ...
gEdringer's user avatar
  • 16.4k
1809 votes
23 answers
733k views

What is the difference between const int*, const int * const, and int const *?

I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do? I want to know all the do's and all don'ts in terms ...
user avatar
1803 votes
40 answers
973k views

What is an undefined reference/unresolved external symbol error and how do I fix it?

What are undefined reference/unresolved external symbol errors? What are common causes, and how do I fix and prevent these errors?
Luchian Grigore's user avatar
1772 votes
15 answers
151k views

Is < faster than <=?

Is if (a < 901) faster than if (a <= 900)? Not exactly as in this simple example, but there are slight performance changes on loop complex code. I suppose this has to do something with generated ...
Vinícius's user avatar
  • 15.6k
1760 votes
10 answers
670k views

What is a lambda expression, and when should I use one?

What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn't possible prior to their introduction? A few examples, and use cases would be useful.
Sarfaraz Nawaz's user avatar
1672 votes
7 answers
164k views

Why does changing 0.1f to 0 slow down performance by 10x?

Why does this bit of code, const float x[16] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6}; const float z[...
GlassFish's user avatar
  • 14.9k
1668 votes
14 answers
265k views

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

In C++03, an expression is either an rvalue or an lvalue. In C++11, an expression can be an: rvalue lvalue xvalue glvalue prvalue Two categories have become five categories. What are these new ...
James McNellis's user avatar
1650 votes
28 answers
699k views

Why do we need virtual functions in C++?

From what I've read, virtual functions are functions in the base class that you can override in its derived classes. But earlier, when learning about basic inheritance, I was able to override base ...
Jake Wilson's user avatar
  • 89.9k
1638 votes
11 answers
197k views

Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs

I was looking for the fastest way to popcount large arrays of data. I encountered a very weird effect: Changing the loop variable from unsigned to uint64_t made the performance drop by 50% on my PC. ...
gexicide's user avatar
  • 39.2k
1630 votes
23 answers
380k views

Compiling an application for use in highly radioactive environments

We are compiling an embedded C++ application that is deployed in a shielded device in an environment bombarded with ionizing radiation. We are using GCC and cross-compiling for ARM. When deployed, our ...
rook's user avatar
  • 66.7k
1590 votes
18 answers
291k views

Cycles in family tree software

I am the developer of some family tree software (written in C++ and Qt). I had no problems until one of my customers mailed me a bug report. The problem is that the customer has two children with ...
Partick Höse's user avatar
1385 votes
10 answers
267k views

Where and why do I have to put the "template" and "typename" keywords?

In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // ...
MSalters's user avatar
  • 177k
1268 votes
27 answers
623k views

When should you use a class vs a struct in C++? [duplicate]

In what scenarios is it better to use a struct vs a class in C++?
Alan Hinchcliffe's user avatar
1259 votes
17 answers
777k views

What is the difference between public, private, and protected inheritance?

What is the difference between public, private, and protected inheritance in C++?
user avatar
1259 votes
8 answers
448k views

What is the difference between 'typedef' and 'using'?

I know that in C++11 we can now use using to write type alias, like typedefs: typedef int MyInt; Is, from what I understand, equivalent to: using MyInt = int; And that new syntax emerged from the ...
Klaim's user avatar
  • 68.4k
1249 votes
19 answers
989k views

How do I use extern to share variables between source files?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source ...
user avatar
1221 votes
9 answers
450k views

What are POD types in C++? [duplicate]

I've come across this term POD-type a few times. What does it mean?
oz10's user avatar
  • 156k
1182 votes
7 answers
688k views

push_back vs emplace_back

I'm a bit confused regarding the difference between push_back and emplace_back. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); As ...
ronag's user avatar
  • 50.5k
1172 votes
11 answers
640k views

What is the difference between g++ and gcc?

What is the difference between g++ and gcc? Which one of them should be used for general c++ development?
Brian R. Bondy's user avatar
1163 votes
21 answers
305k views

Can a local variable's memory be accessed outside its scope?

I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *...
1153 votes
15 answers
640k views

Can I call a constructor from another constructor (do constructor chaining) in C++?

As a C# developer I'm used to running through constructors: class Test { public Test() { DoSomething(); } public Test(int count) : this() { DoSomethingWithCount(count); ...
Stormenet's user avatar
  • 26.3k
1134 votes
9 answers
572k views

What is std::move(), and when should it be used?

What is it? What does it do? When should it be used? Good links are appreciated.
Basilevs's user avatar
  • 23.2k
1131 votes
23 answers
395k views

Why can't variables be declared in a switch statement?

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is ...
Rob's user avatar
  • 77.6k
1116 votes
8 answers
140k views

Do the parentheses after the type name make a difference with new?

If 'Test' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test();
David Read's user avatar
  • 11.1k
1089 votes
4 answers
348k views

What does T&& (double ampersand) mean in C++11?

I've been looking into some of the new features of C++11 and one I've noticed is the double ampersand in declaring variables, like T&& var. For a start, what is this beast called? I wish ...
paxdiablo's user avatar
  • 869k

1
2 3 4 5
16147