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
135
votes
5 answers

How to get error message when ifstream open fails

ifstream f; f.open(fileName); if ( f.fail() ) { // I need error message here, like "File not found" etc. - // the reason of the failure } How to get error message as string?
Alex F
  • 42,307
  • 41
  • 144
  • 212
125
votes
3 answers

Why is there no std::stou?

C++11 added some new string conversion functions: http://en.cppreference.com/w/cpp/string/basic_string/stoul It includes stoi (string to int), stol (string to long), stoll (string to long long), stoul (string to unsigned long), stoull (string to…
David Stone
  • 26,872
  • 14
  • 68
  • 84
122
votes
9 answers

When using C headers in C++, should we use functions from std:: or the global namespace?

C is somewhat, not exactly, a subset of C++. So we can use most of the C functions/headers in C++ by changing the name a little bit (stdio.h to cstdio, stdlib.h to cstdlib). My question is actually kind of semantic. In C++ code (using newest…
DeiDei
  • 10,205
  • 6
  • 55
  • 80
113
votes
8 answers

Is there a range class in C++11 for use with range based for loops?

I found myself writing this just a bit ago: template class range_class { public: class iterator { friend class range_class; public: long int operator *() const { return i_; } const…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
112
votes
5 answers

Why is rand()%6 biased?

When reading how to use std::rand, I found this code on cppreference.com int x = 7; while(x > 6) x = 1 + std::rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is biased What is wrong with the expression on the right? Tried it and it works…
yO_
  • 1,135
  • 2
  • 12
  • 18
108
votes
8 answers

How do I combine hash values in C++0x?

C++0x adds hash<...>(...). I could not find a hash_combine function though, as presented in boost. What is the cleanest way to implement something like this? Perhaps, using C++0x xor_combine?
Neil G
  • 32,138
  • 39
  • 156
  • 257
108
votes
10 answers

How do I Search/Find and Replace in a standard string?

How do I replace all occurrences of a substring with another string, for std::strings? std::string s ("One hello, two hellos."); s = s.replace("hello", "world"); // something like this
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
105
votes
5 answers

In C++ check if std::vector contains a certain value

Is there any built in function which tells me that my vector contains a certain element or not e.g. std::vector v; v.push_back("abc"); v.push_back("xyz"); if (v.contains("abc")) // I am looking for one such feature, is there any …
Jame
  • 21,150
  • 37
  • 80
  • 107
105
votes
3 answers

Prevent function taking const std::string& from accepting 0

Worth a thousand words: #include #include class SayWhat { public: SayWhat& operator[](const std::string& s) { std::cout << s << "\n"; return *this; } }; int main() { SayWhat ohNo; // ohNo[1];…
kabanus
  • 24,623
  • 6
  • 41
  • 74
103
votes
6 answers

Why isn't std::initializer_list a language built-in?

Why isn't std::initializer_list a core-language built-in? It seems to me that it's quite an important feature of C++11 and yet it doesn't have its own reserved keyword (or something alike). Instead, initializer_list it's just a template class from…
emesx
  • 12,555
  • 10
  • 58
  • 91
103
votes
3 answers

Passing std::string by Value or Reference

Possible Duplicate: Are the days of passing const std::string & as a parameter over? Should I pass std::string by value or by reference (to a un-inlined function) if move semantics is supported? And what about implementations using small string…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
97
votes
4 answers

cc1plus: error: unrecognized command line option "-std=c++11" with g++

I'm trying to compile using g++ and either the -std=c++11 or c++0x flags. However, I get this error cc1plus: error: unrecognized command line option "-std=c++11" g++ --version g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) Copyright (C) 2006 Free…
user1382306
96
votes
2 answers

Can a declaration affect the std namespace?

#include #include /* Intentionally incorrect abs() which seems to override std::abs() */ int abs(int a) { return a > 0? -a : a; } int main() { int a = abs(-5); int b = std::abs(-5); std::cout<< a << std::endl <<…
Peter
  • 743
  • 5
  • 6
92
votes
5 answers

What is the performance overhead of std::function?

I heard on a forum using std::function<> causes performance drop. Is it true? If true, is it a big performance drop?
user408141
90
votes
2 answers

How is std::is_function implemented?

How is the following an implementation for std::is_function? template struct is_function : std::integral_constant< bool, !std::is_const::value && !std::is_reference::value > {}; (from CPP Reference) Seems to me, an int…
Rian Quinn
  • 1,766
  • 12
  • 22