stl

debugging C++ code with templates and STL with gdb

Hello, What do gdb users here think about its capabilities in regards to debugging code with templates and STL? Do you use any tricks to make the debugging any simpler? Perhaps some Python scripts? Or are you satisfied the way it is at present in gdb (ver 6.x, haven't tried 7.x yet)? Thanks. ...

why would std::vector max_size() function return -1?

I have a std::vector<unsigned char> m_vData; m_vData.max_size() always returns -1. why would that happen? ...

vector.resize function corrupting memory when size is too large.

what's happening is i'm reading encryption packets, and I encounter a corrupted packet that gives back a very large random number for the length. size_t nLengthRemaining = packet.nLength - (packet.m_pSource->GetPosition() - packet.nDataOffset); seckey.SecretValues.m_data.resize(nLengthRemaining); In this code m_data is a std::vector<...

Account memory usage with custom allocator

I'm using a custom allocator to account for memory usage in several containers. Currently I use a static variable to account for the memory usage. How could I separate this account across several containers without having to rewrite the allocator to use different static variables? static size_t allocated = 0; template <class T> ...

Are there good examples of good C++ I/O usage.

I am heavily involved in doing I/O in C++ (currently using it for printing headers, tables, some data alignments), and wonder about it proper/great usage in either open source projects or general examples/nippets I use things such: cout.setf(ios::right,ios::jyustified); cout<<std::setw() std::copy (vector.begin(), vector.end(), std::o...

When do I have to use initializer lists for initializing C++ class members?

let's say I have std::map< std::string, std::string > m_someMap as a private member variable of class A Two questions: (and the only reason I'm asking is because I came across code like that) What's the purpose of this line: A::A() : m_someMap() Now I know that this is intialization, but do you have to do this like that? I'm c...

STL algorithms and const_iterators

Hello, Today I wrote a small predicate to find matching symbols in a container. But I'm faced to a problem: I want to use this predicate in a std::find_if call inside a const-method of a class, searching in a container that is a member of this class. But I just noticed that neither std::find nor std::find_if are able to operate on con...

Does std::sort change the relative order of equal elements?

Does the standard guarantee that order of equal elements will not change (eh, forgot the term for that) by using std::sort or do I need to consider an alternative solution to achieve this goal? ...

how to solve this problem with a cast of iterator ?

The compiler (VC8) error is: error C2680: 'std::_Tree<_Traits>::iterator' : invalid target type for dynamic_cast the source code to simulate the error : [EDIT]source fixed now #include <map> #include <string> struct tag_data { int in; int on; std::string sn; }; class myclass { private: typedef std::map<std::string, tag...

Set Visual Studio (conditional) breakpoint on local variable value

I'm trying to debug a method which among other things, adds items to a list which is local to the method. However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this. Thanks. ...

iterator validity ,after erase() call in std::set

Do erase call in std::set invalidate iterator ? As i have done below 5th from last line..? if yes what is better way to erase all elements from set class classA { public: classA(){}; ~classA(){}; }; struct structB { }; typedef std::set <classA*, structB> SETTYPE; typedef std::map <int, SETTYPE>MAPTYPE; int __cdecl wmain (...

C++ stack allocated object, explicit destructor call

Hi, I came across a strange use of the destructor while working on an existing library. The destructor of a stack allocated stl vector was being called explicitly, when its the case that that object may need to be used again. These vector objects are a slightly customised version of the stl vector class that have a specialized clear met...

Calling a function on STL map element without inserting it first

Is the following code OK? class A { public: A(); void foo(); }; map<int,A> m; m[0].foo(); Or do I have to do the following: map<int,A> m; m[0] = A(); m[0].foo(); And also, can I do this: map<int,A> m; A a = m[5]; And how about access by reference: void foo(A & a); map<int,A> m; foo(m[5]); ...

STL containers on the stack and the heap

If std::vector and friends are self resizing, does that mean if I declare a vector like so: std::vector<string> myvec; Then it'll resize using more stack, whereas: std::vector<string> *myvec = new std::vector<string>(); Would resize using more heap? ...

Difference between hash_map and unordered_map?

I recently discovered that the implementation of the hash map in c++ will be called unordered_map. When I looked up why they weren't just using hash_map, I discovered that apparently there are compatiblity issues with the implementation of hash_map that unordered_map resolves(http://en.wikipedia.org/wiki/Unordered_map_%28C%2B%2B%29). The...

Why don't STL containers have virtual destructors?

Does anyone know why the STL containers don't have virtual destructors? As far as I can tell, the only benefits are: it reduces the size of an instance by one pointer (to the virtual method table) and it makes destruction and construction a tiny bit faster. The downside is that it's unsafe to subclass the containers in the usua...

C++ : struggle with generic const pointer

I've run into some annoying issues with const-correctness in some templated code, that ultimately boils down to the following observation: for some reason, given an STL-ish Container type T, const typename T::pointer does not actually seem to yeild a constant pointer type, even if T::pointer is equivalent to T::value_type*. The followin...

C++ boost::thread and automatically locking containers

Is there a way to automatically lock an STL container on access, without having to lock and release around it? ...

C++ standard date/time class

Does C++ stl have a standard time class? Or do I have to convert to c-string before writing to a stream. Example, I want to output the current date/time to a string stream: time_t tm(); ostringstream sout; sout << tm << ends; In this case I get the current date/time written out as a number without any formatting. I can use c- runtime...

Changing std::endl to put out CR+LF instead of LF

I'm writing a program on a Linux platform that is generating text files that will be viewed on, inevitably, a Windows platform. Right now, passing std::endl into a ostream generates the CR character only for newlines. Naturally, these text files look wrong in MS Notepad. 1) Is there a way to change std::endl such that it uses CR+LF f...