Skip to main content
3463 votes
39 answers
1.3m views

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
  • 35.4k
594 votes
6 answers
190k views

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 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 ...
Ankur's user avatar
  • 11.8k
529 votes
7 answers
128k views

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. (...)...
Pieter's user avatar
  • 33.1k
1039 votes
32 answers
1.5m views

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::...
Konrad's user avatar
  • 41.2k
1236 votes
8 answers
796k views

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
  • 51.7k
290 votes
20 answers
514k views

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");
Tom Leese's user avatar
  • 19.7k
659 votes
43 answers
1.5m views

I have to format std::string with sprintf and send it into file stream. How can I do this?
Max Frai's user avatar
  • 64.7k
846 votes
21 answers
1.6m views

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();
Joan Venge's user avatar
  • 334k
296 votes
15 answers
316k views

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(*...
AShelly's user avatar
  • 35.9k
739 votes
29 answers
1.2m views

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; ...
Agnel Kurian's user avatar
  • 59.9k
1033 votes
30 answers
755k views

How do I concatenate two std::vectors?
user avatar
93 votes
9 answers
184k views

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 ...
jorgen's user avatar
  • 3,613
52 votes
1 answer
4k views

Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*...
L. F.'s user avatar
  • 20.9k
137 votes
2 answers
72k views

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 ...
ValarDohaeris-fuck-OpenAI's user avatar

15 30 50 per page
1
2 3 4 5
70