Questions tagged [std]

The C++ Standard Library, and its namespace. Use in conjunction with [c++].

From Wikipedia

The C++ Standard Library provides several generic containers, functions to utilise and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O).

http://en.wikipedia.org/wiki/C++_Standard_Library

The concept of using namespaces

http://en.wikipedia.org/wiki/Namespace_(computer_science)

Also see

4970 questions
56
votes
1 answer

Why is the destructor of a future returned from `std::async` blocking?

When trying to answer another Stackoverflow question, I realized that this simple C++11 snippet is implicitly blocking the calling thread: std::async(std::launch::async, run_async_task) To me this would have seemed the canonical C++11 way to launch…
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
55
votes
7 answers

Why does the C++ standard algorithm "count" return a difference_type instead of size_t?

Why is the return type of std::count the difference_type of the iterators (often a ptrdiff_t). Since count can never be negative, isn't size_t technically the right choice? And what if the count exceeds the range of ptrdiff_t since the theoretical…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
55
votes
6 answers

Why are std::begin and std::end "not memory safe"?

In this blog post, Eric Niebler states that: What is wrong with std::begin and std::end? Surprise! they are not memory safe. Consider what this code does: extern std::vector get_data(); auto it = std::begin(get_data()); int i = *it; //…
Davidbrcz
  • 2,335
  • 18
  • 27
55
votes
3 answers

Search a vector of objects by object attribute

I'm trying to figure out a nice way to find the index of a certain object in a vector - by comparing a string to a member field in the object. Like this: find(vector.begin(), vector.end(), [object where obj.getName() == myString]) I have searched…
Christoffer
  • 7,470
  • 9
  • 39
  • 55
54
votes
9 answers

Element at index in a std::set?

I've stumbled upon this problem: I can't seem to select the item at the index' position in a normal std::set. Is this a bug in STD? Below a simple example: #include #include int main() { std::set my_set; …
hauron
  • 4,550
  • 5
  • 35
  • 52
52
votes
3 answers

C++ std::tuple order of destruction

Is there a rule which states in which order the members of an std::tuple are destroyed? For example if Function1 returns an std::tuple, std::unique_ptr> to Function2, then can I be sure that (when the scope of…
z32a7ul
  • 3,695
  • 3
  • 21
  • 45
51
votes
3 answers

Is std::vector or boost::vector thread safe?

I have multiple threads simultaneously calling push_back() on a shared object of std::vector. Is std::vector thread safe? Or do I need to implement the mechanism myself to make it thread safe? I want to avoid doing extra "locking and freeing" work…
Jacky Lee
  • 1,193
  • 3
  • 13
  • 22
51
votes
6 answers

Why does the string returned by ctime() contain a line feed?

Why does the string returned by ctime() have a line feed (0x0A) as its final character? For example, this code: #include #include int main(int argc, char* argv[]) { time_t now; time(&now); char* time_str =…
bythescruff
  • 1,869
  • 2
  • 17
  • 33
51
votes
5 answers

Initializing a std::map when the size is known in advance

I would like to initialize a std::map. For now I am using ::insert but I feel I am wasting some computational time since I already know the size I want to allocate. Is there a way to allocate a fixed size map and then fill the map ?
vanna
  • 1,552
  • 5
  • 20
  • 35
50
votes
4 answers

Add same value multiple times to std::vector (repeat)

I want to add a value multiple times to an std::vector. E.g. add the interger value 1 five times to the vector: std::vector vec; vec.add(1, 5); vec should be of the form {1,1,1,1,1} afterwards. Is there a clean c++ way to do so?
Kackao
  • 1,181
  • 3
  • 13
  • 22
48
votes
4 answers

Is specialization of std::to_string for custom types allowed by the C++ standard?

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout <<…
jotik
  • 17,044
  • 13
  • 58
  • 123
48
votes
6 answers

Android ndk std::to_string support

I'm using android NDK r9d and toolchain 4.8 but I'm not able to use std::to_string function, compiler throws this error: error: 'to_string' is not a member of 'std' Is this function not supported on android ndk? I try APP_CPPFLAGS := -std=c++11…
albciff
  • 18,112
  • 4
  • 64
  • 89
48
votes
5 answers

Sorting zipped (locked) containers in C++ using boost or the STL

What I want to do: I want to sort 2, or 3, or N vectors, locked together, without copying them into a tuple. That is, leaving verbosity aside, something like: vector v1 = { 1, 2, 3, 4, 5}; vector v2 = { 11, 22, 33, 44, …
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
47
votes
2 answers

How std::bind works with member functions

I'm working with std::bind but I still don't get how it works when we use it with member class functions. If we have the following function: double my_divide (double x, double y) {return x/y;} I understand perfectly well the next lines of…
Tarod
  • 6,732
  • 5
  • 44
  • 50
47
votes
9 answers

Is there "magic" in the STL?

Let me start with explaining what I mean with "magic". I will use two examples from Java: Every class inherits (directly or indirectly) the Object class. Operator overloading is not supported by Java but the + operator is defined for String…
mtvec
  • 17,846
  • 5
  • 52
  • 83