811,386
questions
27347
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()
{
// ...
10180
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. ...
4228
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 ...
3929
votes
44
answers
1.3m
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?
3710
votes
11
answers
1.2m
views
What does the explicit keyword mean?
What does the explicit keyword mean in C++?
3424
votes
41
answers
1.3m
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 ...
3370
votes
84
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 ...
3160
votes
27
answers
1.7m
views
How to set, clear, and toggle a single bit
How can I set, clear, and toggle a bit?
3149
votes
12
answers
796k
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 ...
3122
votes
30
answers
926k
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"
2544
votes
8
answers
390k
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?
2475
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++ ...
2445
votes
11
answers
260k
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];
}
...
2404
votes
5
answers
513k
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 ...
2309
votes
19
answers
815k
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?
(...