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 allocators have always been a feature of the Standard Library that I haven’t had much need for. I was … Read more

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 <iostream> #include “add.h” int main() { int x = readNumber(); int y = readNumber(); writeAnswer(x + y); return(0); } io.cpp int readNumber() { int x; … Read more

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

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(*i); //else other_code_involving(*i); } items.remove_if(CheckItemNotActive); I’d like remove inactive items immediately after update them, inorder to avoid walking the list again. But if I add the commented-out lines, I get an error when I get to i++: … Read more

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 to me that emplace_back does everything push_back can do, but some of the time it will do it better … Read more

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 very few people still use the STL (which was designed at … Read more

Why is “using namespace std;” considered bad practice?

I’ve been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? … Read more