1,036 questions
3463
votes
39
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 ...
594
votes
6
answers
190k
views
Iterator invalidation rules for C++ containers
What are the iterator invalidation rules for C++ containers?
(Note: This Q&A is an entry in Stack Overflow's C++ FAQ. Meta-discussion about the question itself should be posted on the Meta question ...
344
votes
17
answers
619k
views
How do I sort a vector of custom objects?
How does one go about sorting a std::vector containing custom (i.e. user-defined) objects?
Probably, you can use the standard library algorithm std::sort along with a predicate (a function or a ...
529
votes
7
answers
128k
views
What's the difference between "STL" and "C++ Standard Library"?
Someone brought this article to my attention that claims (I'm paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL.
(...)...
1039
votes
32
answers
1.5m
views
How to convert an instance of std::string to lower case
I want to convert a std::string to lowercase. I am aware of the function tolower(). However, in the past I have had issues with this function and it is hardly ideal anyway as using it with a std::...
1236
votes
8
answers
796k
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 ...
290
votes
20
answers
514k
views
Replace part of a string with another string
How do I replace part of a string with another string using the standard C++ libraries?
QString s("hello $name"); // Example using Qt.
s.replace("$name", "Somename");
659
votes
43
answers
1.5m
views
std::string formatting like sprintf
I have to format std::string with sprintf and send it into file stream. How can I do this?
846
votes
21
answers
1.6m
views
How to find out if an item is present in a std::vector?
All I want to do is to check whether an element exists in the vector or not, so I can deal with each case.
if ( item_present )
do_this();
else
do_that();
296
votes
15
answers
316k
views
Can you remove elements from a std::list while iterating through it?
I've got code that looks like this:
for (std::list<item*>::iterator i = items.begin(); i != items.end(); i++)
{
bool isActive = (*i)->update();
//if (!isActive)
// items.remove(*...
739
votes
29
answers
1.2m
views
What is the easiest way to initialize a std::vector with hardcoded elements?
I can create an array and initialize it like this:
int a[] = {10, 20, 30};
How do I create a std::vector and initialize it similarly elegant?
The best way I know is:
std::vector<int> ints;
...
1033
votes
30
answers
755k
views
Concatenating two std::vectors
How do I concatenate two std::vectors?
93
votes
9
answers
184k
views
Converting std::__cxx11::string to std::string
I use c++11, but also some libraries that are not configured for it, and need some type conversion. In particular I need a way to convert std::__cxx11::string to regular std::string, but googling I ...
52
votes
1
answer
4k
views
Can I take the address of a function defined in standard library?
Consider the following code:
#include <cctype>
#include <functional>
#include <iostream>
int main()
{
std::invoke(std::boolalpha, std::cout); // #1
using ctype_func = int(*...
137
votes
2
answers
72k
views
What are the mechanics of short string optimization in libc++?
This answer gives a nice high-level overview of short string optimization (SSO). However, I would like to know in more detail how it works in practice, specifically in the libc++ implementation:
How ...