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
3326
votes
42 answers

Why is "using namespace std;" considered bad practice?

I have heard using namespace std; is bad practice, 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 std namespace?
akbiggs
  • 34,001
  • 6
  • 25
  • 36
798
votes
19 answers

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();
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
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
312
votes
6 answers

Why would I ever use push_back instead of emplace_back?

C++11 vectors have the new function emplace_back. Unlike push_back, which relies on compiler optimizations to avoid copies, emplace_back uses perfect forwarding to send the arguments directly to the constructor to create an object in-place. It seems…
David Stone
  • 26,872
  • 14
  • 68
  • 84
284
votes
15 answers

Can you remove elements from a std::list while iterating through it?

I've got code that looks like this: for (std::list::iterator i=items.begin();i!=items.end();i++) { bool isActive = (*i)->update(); //if (!isActive) // items.remove(*i); //else …
AShelly
  • 34,686
  • 15
  • 91
  • 152
262
votes
18 answers

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");
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
259
votes
3 answers

cout is not a member of std

I'm practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple. Here are my files: main.cpp #include #include "add.h" int main() { int x = readNumber(); …
Paul Hannon
  • 2,613
  • 2
  • 13
  • 5
214
votes
5 answers

How do I reverse a C++ vector?

Is there a built-in vector function in C++ to reverse a vector in place? Or do you just have to do it manually?
Dollarslice
  • 9,917
  • 22
  • 59
  • 87
211
votes
17 answers

Compelling examples of custom C++ allocators?

What are some really good reasons to ditch std::allocator in favor of a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom…
Naaff
  • 9,213
  • 3
  • 38
  • 43
210
votes
9 answers

printf with std::string?

My understanding is that string is a member of the std namespace, so why does the following occur? #include int main() { using namespace std; string myString = "Press ENTER to quit program!"; cout << "Come up and C++ me some…
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
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
190
votes
7 answers

Should I use std::function or a function pointer in C++?

When implementing a callback function in C++, should I still use the C-style function pointer: void (*callbackFunc)(int); Or should I make use of std::function: std::function< void(int) > callbackFunc;
Jan Swart
  • 6,761
  • 10
  • 36
  • 45
150
votes
11 answers

declaring a priority_queue in c++ with a custom comparator

I'm trying to declare a priority_queue of nodes, using bool Compare(Node a, Node b) as the comparator function (which is outside the node class). What I currently have is: priority_queue, Compare> openSet; For some reason, I'm…
Steven Morad
  • 2,511
  • 3
  • 19
  • 25
146
votes
15 answers

A std::map that keep track of the order of insertion?

I currently have a std::map that stores an integer value to a unique string identifier, and I do look up with the string. It does mostly what I want, except that it does not keep track of the insertion order. So when I iterate the…
polyglot
  • 9,945
  • 10
  • 49
  • 63
136
votes
4 answers

How to declare std::unique_ptr and what is the use of it?

I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example: #include //declarations of unique_ptr using std::unique_ptr; // default construction unique_ptr up;…
Roman
  • 124,451
  • 167
  • 349
  • 456
1
2 3
99 100