stl

How to overload std::swap()

std::swap() is used by many std containers (such as std::list and std::vector) during sorting and even assignment. But the std implementation of swap() is very generalized and rather inefficient for custom types. Thus efficiency can be gained by overloading std::swap() with a custom type specific implementation. But how can you impleme...

Is this code an abuse of STL's find_if?

Let's say I have a list of server names stored in a vector, and I would like to contact them one at a time until one has successfully responded. I was thinking about using STL's find_if algorithm in the following way: find_if(serverNames.begin(), serverNames.end(), ContactServer()); Where ContactServer is a predicate function object. ...

What C++ pitfalls should I avoid ?

I remember first learning about vectors in the STL and after some time, I wanted to use a vector of bools for one of my projects. After seeing some strange behavior and doing some research, I learned that a vector of bools is not really a vector of bools. Anyone out there have any other common pitfalls to avoid in C++? ...

Why don't the std::fstream classes take a std::string?

This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples so: #include <iostream> #include <fstream> #include <string> int main() { ...

C++ STL question: allocators

I have a (potentially dumb) question about the C++ STL. When I make a container (vector, set, map, etc), is it allocated on the stack or on the heap? If I make a set and put 5 million strings, will I have to worry about a stack overflow? ...

Why can't a forward declaration be used for a std::vector?

If I create a class like so: // B.h #ifndef _B_H_ #define _B_H_ class B { private: int x; int y; }; #endif // _B_H_ and use it like this: // main.cpp #include <iostream> #include <vector> class B; // Forward declaration. class A { public: A() { std::cout << v.size() << std::endl; } private: std::vec...

Good book for learning the C++ standard template library?

I'm looking for a good tutorial book on the C++ Standard Template Library. I don't want a reference, there are plenty of them online, but rather a book that will lead me through using the various parts of the STL and give some insight into why it is the way it is. Does anybody know of such a book? ...

How do I remove an item from a stl vector with a certain value?

I was looking at the API documentation for stl vector, and noticed there was no method on the vector class that allowed the removal of an element with a certain value. This seems like a common operation, and it seems odd that there's no built in way to do this. ...

Can I have polymorphic containers with value semantics in C++?

As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector<Class> instead of vector<Class*>). Usually the slight loss in performance is more than made up for by not having to remember to delete dynamically allocated objects. Unfortunately, value collections don't work when you want to store a variety o...

STL vector vs map erase

In the STL almost all containers have an erase function. The question I have is in a vector, the erase function returns an iterator pointing to the next element in the vector. The map container does not do this. Instead it returns a void. Anyone know why there is this inconsistancy? ...

Is a pop_back() in a std::vector really invalidates *all* iterators on the vector?

std::vector<int> ints; // ... fill ints with random values for(std::vector<int>::iterator it = ints.begin(); it != ints.end(); ) { if(*it < 10) { *it = ints.back(); ints.pop_back(); continue; } it++; } This code is not working because when pop_back() is called, the it is invalidate. But I don't...

Dynamically sorted STL containers

I'm fairly new to the STL, so I was wondering whether there are any dynamically sortable containers? At the moment my current thinking is to use a vector in conjunction with the various sort algorithms, but I'm not sure whether there's a more appropriate selection given the (presumably) linear complexity of inserting entries into a sort...

stringstream manipulators & vstudio 2003

I am trying to use a stringstream object in VC++ (VStudio 2003) butI am getting an error when I use the overloaded << operator to try and set some manipulators. I am trying the following: int SomeInt = 1; stringstream StrStream; StrStream << std::setw(2) << SomeInt; This will not compile (error C2593: 'operator <<' is ambiguous)...

Remove C++-STL/Boost debug symbols (... or do not create them)

Linux/Gcc/LD - Toolchain. I would like to remove STL/Boost debug symbols from libraries and executable, for two reasons: Linking gets very slow for big programs Debugging jumps into stl/boost code, which is annoying For 1. incremental linking would be a big improvement, but AFAIK ld does not support incremental linking. There is a w...

Remove spaces from std::string in C++

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way? ...

C++ does begin/end/rbegin/rend execute in constant time for std::set, std::map, etc?

For data types such as std::set and std::map where lookup occurs in logarithmic time, is the implementation required to maintain the begin and end iterators? Does accessing begin and end imply a lookup that could occur in logarithmic time? I have always assumed that begin and end always occur in constant time, however I can't find any c...

STL Alternative

I really hate using STL containers because they make the debug version of my code run really slowly. What do other people use instead of STL that has reasonable performance for debug builds? I'm a game programmer and this has been a problem on many of the projects I've worked on. It's pretty hard to get 60 fps when you use STL container...

Best container for double-indexing

What is the best way (in C++) to set up a container allowing for double-indexing? Specifically, I have a list of objects, each indexed by a key (possibly multiple per key). This implies a multimap. The problem with this, however, is that it means a possibly worse-than-linear lookup to find the location of an object. I'd rather avoid dupl...

STL vectors with uninitialized storage?

I'm writing an inner loop that needs to place structs in contiguous storage. I don't know how many of these structs there will be ahead of time. My problem is that STL's vector initializes its values to 0, so no matter what I do, I incur the cost of the initialization plus the cost of setting the struct's members to their values. Is t...

std::map insert or std::map find?

Assuming a map where you want to preserve existing entries. 20% of the time, the entry you are inserting is new data. Is there an advantage to doing std::map::find then std::map::insert using that returned iterator? Or is it quicker to attempt the insert and then act based on whether or not the iterator indicates the record was or was ...