Questions tagged [c++-standard-library]

The C++ Standard Library is a collection of classes and functions, which are nearly all written in the core language and part of the C++.

The C++ Standard library should not be confused with C++ standard template library. The was the inspiration for a big subset of C++ Standard Library, specifically containers, algorithms, iterators, and function-objects.

Online-reference:

Books

1021 questions
982
votes
31 answers

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::string would require iterating over each…
Konrad
  • 39,751
  • 32
  • 78
  • 114
610
votes
44 answers

std::string formatting like sprintf

I have to format std::string with sprintf and send it into file stream. How can I do this?
Max Frai
  • 61,946
  • 78
  • 197
  • 306
595
votes
6 answers

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 that started all of this, not here.)
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
509
votes
7 answers

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. (...) it refers to the "STL", despite the fact that…
Pieter
  • 31,619
  • 76
  • 167
  • 242
228
votes
13 answers

Why is the STL so heavily based on templates instead of inheritance?

I mean, aside from its name the Standard Template Library (which evolved into the C++ standard library). C++ initially introduce OOP concepts into C. That is: you could tell what a specific entity could and couldn't do (regardless of how it does it)…
OB OB
  • 3,289
  • 6
  • 21
  • 16
191
votes
8 answers

Deleting elements from std::set while iterating

I need to go through a set and remove elements that meet a predefined criteria. This is the test code I wrote: #include #include void printElement(int value) { std::cout << value << " "; } int main() { int initNum[] = {…
pedromanoel
  • 3,232
  • 2
  • 24
  • 23
188
votes
10 answers

C++ valarray vs. vector

I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other than that, when are they useful?
rlbond
  • 65,341
  • 56
  • 178
  • 228
142
votes
3 answers

Writing your own STL Container

Are there guidelines on how one should write new container which will behave like any STL container?
Avinash
  • 12,851
  • 32
  • 116
  • 186
129
votes
13 answers

What does string::npos mean in this code?

What does the phrase std::string::npos mean in the following snippet of code? found = str.find(str2); if (found != std::string::npos) std::cout << "first 'needle' found at: " << int(found) << std::endl;
boom
  • 5,856
  • 24
  • 61
  • 96
127
votes
2 answers

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 short does the string have to be in order to…
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
106
votes
11 answers

Why is there no transform_if in the C++ standard library?

A use case emerged when wanting to do a contitional copy (1. doable with copy_if) but from a container of values to a container of pointers to those values (2. doable with transform). With the available tools I can't do it in less than two steps :…
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
104
votes
10 answers

std::queue iteration

I need to iterate over std::queue. www.cplusplus.com says: By default, if no container class is specified for a particular queue class, the standard container class template deque is used. So can I somehow get to the queue's underlying deque and…
jackhab
  • 17,128
  • 37
  • 99
  • 136
100
votes
2 answers

How to get the file size in bytes with C++17

Are there pitfalls for specific operating systems, I should know of? There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today. Methods…
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
92
votes
4 answers

Are the experimental features of modern C++ reliable for long-term projects?

I have a project that currently uses C++11/14, but it requires something like std::filesystem, which is only available in C++17, and hence I don't have a chance to currently use it. I see, however, that it's available in my current compiler as…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
92
votes
1 answer

Why is libc++'s vector::const_reference not bool?

Section 23.3.7 Class vector [vector.bool], paragraph 1 states: template class vector { public: // types: typedef bool const_reference; ... However this program fails to compile when…
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1
2 3
68 69