stl

How do I erase a reverse_iterator from an stl data structure?

For some reason the following code fails. You can't simply erase a reverse_iterator by using its base() method. #include <set> #include <iostream> int main() { std::set<int> setOfInts; setOfInts.insert(1); setOfInts.insert(2); setOfInts.insert(3); std::set<int>::reverse_iterator rev_iter = setOfInts.rbegin(); ...

simple C++ templates suited for STL Containers

I need a template like this, which work perfectly template <typename container> void mySuperTempalte (const container myCont) { //do something here } then i want to specialize the above template for std::string so i came up with template <typename container> void mySuperTempalte (const container<std::string> myCont) { //check...

Converting binary data to printable hex

In this thread some one commented that the following code should only be used in 'toy' projects. Unfortunately he hasn't come back to say why it's not of production quality so I was hoping some one in the community may be able to either assure me the code is ok (because I quite like it) or identify what is wrong. template< class T1, cla...

MFC and STL

Would you mix MFC with STL? Why? ...

Help to correct source code, with template.

I tried to compile the example posted (http://stackoverflow.com/questions/412165/c-service-providers) and failed to VS8 VC9. I have little experience with template. Any suggestions? Tanks. These are the errors : dictionarystl.cpp(40) : error C2663: 'std::_Tree<_Traits>::find' : 2 overloads have no legal conversion for 'this' pointer dic...

Best way to extract a subvector from a vector?

Suppose I have a std::vector (let's call it myVec) of size N. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For example, myVec [100000] through myVec [100999] in a vector of size 150000. If this cannot be done efficiently with a vector, is there another STL da...

std::list with std::map properties?

Basically, I want to have a std::list but with std::map properties such as find(), do I really need to loop through every single list entry to find what I need? ...

Inspecting standard container (std::map) contents with gdb

Supposing to have something like this: #include <map> int main(){ std::map<int,int> m; m[1] = 2; m[2] = 4; return 0; } I would like to be able to inspect the contents of the map running the program from gdb. If I try using the subscript operator I get: (gdb) p m[1] Attempt to take address of value not located in mem...

Debugging Best Practices for C++ STL/Boost with gdb

Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone who has used gdb with STL knows this. For example, see sample runs of some debugging sessions in code here. I am trying to reduce the pain by collecting tips. Can you please comment on the tips I have collected below (particularly which ones you have been...

What happens to an STL iterator after erasing it in VS, UNIX/Linux?

Please consider the following scenario: map(T,S*) //Forward decleration map(T, S*) T2pS = GetMap(); for(map(T, S*)::iterator it = T2pS.begin(); it != T2pS.end(); ++it) { if(it->second != NULL) { delete it->second; it->second = NULL; } T2pS.erase(it); //In VS2005, after the erase, we will crash on ...

Is there any way to check if an iterator is valid?

For two threads manipulating a container map for example, what the correct way to test whether an iterator still valid (for performance reason) ? Or would be of only indirect way that this can be done. The sample code for this : #define _SECURE_SCL 1 //http://msdn2.microsoft.com/en-us/library/aa985973.aspx #define _SECURE_SCL_THROWS 1 ...

Best way to safely printf to a string?

Does anyone know a good safe way to redirect the output of a printf-style function to a string? The obvious ways result in buffer overflows. Something like: string s; output.beginRedirect( s ); // redirect output to s ... output.print( "%s%d", foo, bar ); output.endRedirect(); I think the problem is the same as asking, "how many c...

STL __merge_without_buffer algorithm?

Where can I get a decent high-level description of the algorithm used in __merge_without_buffer() in the C++ STL? I'm trying to reimplement this code in the D programming language, with some enhancements. I can't seem to grok what it's doing at the algorithmic level from just reading the STL source code because there are too many low-l...

"Proper" way to store binary data with C++/STL

In general, what is the best way of storing binary data in C++? The options, as far as I can tell, pretty much boil down to using strings or vector<char>s. (I'll omit the possibility of char*s and malloc()s since I'm referring specifically to C++). Usually I just use a string, however I'm not sure if there are overheads I'm missing, or ...

Can I continue to use an iterator after an item has been deleted from std::multimap<>?

Can I continue to use an multimap iterator even after a call to multimap::erase()? For example: Blah::iterator iter; for ( iter = mm.begin(); iter != mm.end(); iter ++ ) { if ( iter->second == something ) { mm.erase( iter ); } } Should this be expected to run correctly, or is the iterator invalidated f...

vector or map, which one to use?

I've heard many people saying that if the number of elements expected in the container is relatively small it is better to use std::vector instead of std::map eventhough I use the container for only lookup and not for iterating. What is the real reason behind this ? Obviously the lookup performance of map can not be worse than that of th...

Best way to copy a vector to a list in STL ?

Is iterating through the vector using an iterator and copying to a list the most optimal method of copying. Any recommendations? ...

Iterating over vector and calling functions

I have a class that has a vector of another class objects as a member. In many functions of this class I have to do same operation on all the objects in the vector: class Small { public: void foo(); void bar(int x); // and many more functions }; class Big { public: void foo() { for (size_t i = 0; i < V...

How can I shift elements inside STL container

I want to shift elements inside container on any positions to the left or right. The shifting elements are not contiguous. e.g I have a vector {1,2,3,4,5,6,7,8} and I want to shift {4,5,7} to the left on 2 positions, the expected result will be {1,4,5,2,7,3,6,8} Is there an elegant way to solve it ? ...

Using std::for_each on polymorphic method in c++

When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have classes inheriting from A and implementing foo(), and we hold a vector of pointers to A, is there any way to call a polymorphic call on foo(), rather then explicitl...