stl

using boost::lambda_ to compress whitespaces in a string

Hi, I am using boost::lambda to remove subsequent whitespaces in a string, leaving only one space. I tried this program. #include <algorithm> #include <iostream> #include <string> #include <boost/lambda/lambda.hpp> int main() { std::string s = "str str st st sss"; //s.erase( std::unique(s.begin(), s.end(), (boost::lamb...

Should use an insertion sort or construct a heap to improve performance?

We have large (100,000+ elements) ordered vectors of structs (operator < overloaded to provide ordering): std::vector < MyType > vectorMyTypes; std::sort(vectorMyType.begin(), vectorMyType.end()); My problem is that we're seeing performance problems when adding new elements to these vectors while preserving sort order. At the moment ...

Why does C++ allow an integer to be assigned to a string?

I encountered an interesting situation today in a program where I inadvertantly assigned an unsigned integer to a std::string. The VisualStudio C++ compiler did not give any warnings or errors about it, but I happened to notice the bug when I ran the project and it gave me junk characters for my string. This is kind of what the code lo...

What is a good book to learn STL?

Possible Duplicate: Good book for learning the C++ standard template library? I am having very less knowledge on templates and STL. So looking for a good for STL. ...

DLLs and STLs and static data (oh my!)

OK..... I've done all the reading on related questions, and a few MSDN articles, and about a day's worth of googling. What's the current "state of the art" answer to this question: I'm using VS 2008, C++ unmanaged code. I have a solution file with quite a few DLLs and quite a few EXEs. As long as I completely control the build enviro...

Is there a way to access the underlying container of STL container adaptors ?

Is there a standard way to access the underlying container of stack, queue, priority_queue ? I found a method called : _Get_container() in VS2008 implementation of stack and queue, but no one for priority_queue! I think it is not standard anyway. Also, I know it is a silly question! where can I find official documentation of the standa...

How do I initialize a static std::map?

I've made a message-only window class, and I'm trying to map HWNDs back to the objects with those handles. I'm trying to do that using a private static std::map<HWND, CMyClass*> belonging to the class, like this: MyClass.h: class CMyClass { ... private: HWND m_hWnd; HINSTANCE m_hInstance; LPCSTR m_szClas...

Comparing default-constructed iterators with operator==

Does the C++ Standard say I should be able to compare two default-constructed STL iterators for equality? Are default-constructed iterators equality-comparable? I want the following, using std::list for example: void foo(const std::list<int>::iterator iter) { if (iter == std::list<int>::iterator()) { // Something } } ...

How to keep items sorted based on dynamic attribute?

I'm using an STL std::multiset<> as a sorted list of pointers. The sort order is determined by a property of the items being pointed to, something along the lines of this simplified example: struct A { int x; }; bool CompareAPointers(const A* lhs, const A* rhs) { return lhs->x < rhs->x; } std::multiset<A*, CompareAPointers> sorted_...

Why isn't stl compare function a member?

Just idly curious why the compare function for stl::sort can't be a static member? I have a small little helper class foo that is declared and defined in a header, but now I have to create a foo.cpp file for the implementation of cmp() so it isn't multiply defined. I also have to think of a suitably decorated name so fooCmp() doesn...

const pointers in STL containers

Hello fellow C++ programmers. I have, what I hope to be, a quick question about STL containers: std::list<std::string> l; This statement compiles fine when used in some C++ sourcefile (with the appropriate includes). But std::list<const std::string> m; or std::list<std::string * const> n; fails to compile when using gcc (gcc ver...

Building a vector from components contained in another vector type

I have a code that looks something like this: struct First { int f1; int f2; }; struct Second { First s1; int s2; }; std::vector < Second > secondVec; Second sec; sec.s1 = First(); secondVec.push_back(sec); secondVec.push_back(sec); std::vector < First > firstVec; firstVec.reserve(secondVec.size()); for (std::vect...

How to implement a queued map?

The problem: I want to be able to FIFO queue outgoing messages. For update/deletion reasons, I also want to be able to access every message in the queue based upon an object ID. I've currently implemented a solution where data is pushed into a deque, and an iterator to that data is kept. The iterator, keyed by an object ID, is then plac...

Container with two indexes (or a compound index)

I have a class like this class MyClass { int Identifier; int Context; int Data; } and I plan to store it in a STL container like vector<MyClass> myVector; but I will need to access it either by the extenal Index (using myVector[index]); and the combination of Identifier and Context which in this case I would perform a...

C++ STL map typedef errors

I'm having a really nasty problem with some code that I've written. I found someone else that had the same problem on stackoverflow and I tried the solutions but none worked for me. I typedef several common STL types that I'm using and none of the others have any problem except when I try to typedef a map. I get a "some_file.h:83: erro...

When would you use an std::auto_ptr instead of boost::shared_ptr?

We've pretty much moved over to using boost::shared_ptr in all of our code, however we still have some isolated cases where we use std::auto_ptr, including singleton classes: template < typename TYPE > class SharedSingleton { public: static TYPE& Instance() { if (_ptrInstance.get() == NULL) _ptrInstance.rese...

How to use a different STL with g++

Hello, I want to use a different STL with g++ instead of its default libstdc++. What is the easiest way to do this? I found -nostdinc++ flag which inhibits g++ from looking for its STL headers but this is only a compile time thing. It will still make g++ link against its own STL. So I need to find a way to inhibit the linking. Thanks...

Criteria for selecting the right STL container for the job?

Do you just base your STL container selections on the following attributes? Searching/Updating Insertion and Deletion If not, what else do you base your selections upon? Is there any reference out there that lists how each container performs across all these different attributes? ...

Does link line order for supc++ really matter?

Hello, This is a follow up to an earlier question - http://stackoverflow.com/questions/1227615/how-to-use-a-different-stl-with-g I can now get my code to build while using a different STL. However, I still need to link -lsupc++ (along with said different STL) I see anecodal references that -lsupc++ should be the last library on the ...

How do I initialize a const std::pair ?

Let's say that I've got a : #include <utility> using namespace std; typedef pair<int, int> my_pair; how do I initialize a const my_pair ? ...