stl

Wrapping STL container without code duplication

I have a simple struct struct A { int t; int s; }; Those were contained in std::list<A>. The problem is that I group A depending on s. I can avoid duplication of s if I'd wrap A in another struct and keep s in a separated container. This has a lot of benefits (e.g. I don't need to do stupid and potentially dangerous things like AC...

Thread safety of std::map for read-only operations.

I have a std::map that I use to map values (field ID's) to a human readable string. This map is initialised once when my program starts before any other threads are started, and after that it is never modified again. Right now, I give every thread its own copy of this (rather large) map but this is obviously inefficient use of memory and...

simulate virtual constructor in c++

Hi, In my application I have to derive some classes from a base one, the problem is that I want to enforce the derived classed to have 3 particular constructor implementation. As c++ don't have virtual pure constructor, it seemed quite desperate (I had to check manually each class implementation to ensure that the particular ctors are i...

Use a regular iterator to iterate backwards, or struggle with reverse_iterator?

I recently learned about the right way to work with reverse iterators in C++ (specifically when you need to erase one). (See this question and this one.) This is how you're supposed to do it: typedef std::vector<int> IV; for (IV::reverse_iterator rit = iv.rbegin(), rend = iv.rend(); rit != rend; ++rit) { // Use 'rit' if a rever...

C++ : When do I need a shared memory allocator for std::vector?

First_Layer I have a win32 dll written in VC++6 service pack 6. Let's call this dll as FirstLayer. I do not have access to FirstLayer's source code but I need to call it from managed code. The problem is that FirstLayer makes heavy use of std::vector and std::string as function arguments and there is no way of marshaling these types int...

maximum value of int

is there any code to find the maximum value of integer (which is acccording to the compiler) in c/c++ like Integer.MaxValue function in java ...

C++ binary file I/O to/from containers (other than char *) using STL algorithms

I'm attempting a simple test of binary file I/O using the STL copy algorithm to copy data to/from containers and a binary file. See below: 1 #include <iostream> 2 #include <iterator> 3 #include <fstream> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 typedef std::ostream_iterator<double> oi_t; 10 typed...

Assigning a "const char*" to std::string is allowed, but assigning to std::wstring doesn't compile. Why?

I assumed that std::wstring and std::string both provide more or less the same interface. So I tried to enable unicode capabilities for our application # ifdef APP_USE_UNICODE typedef std::wstring AppStringType; # else typedef std::string AppStringType; # endif However that gives me a lot of compile errors when -DAPP_USE_UNI...

What is the C++ equivalent of C# Collection<T> and how do you use it?

I have the need to store a list/collection/array of dynamically created objects of a certain base type in C++ (and I'm new to C++). In C# I'd use a generic collection, what do I use in C++? I know I can use an array: SomeBase* _anArrayOfBase = new SomeBase[max]; But I don't get anything 'for free' with this - in other words, I can't...

To iterate or to use a counter, that is the question

Whenever someone starts using the STL and they have a vector, you usually see: vector<int> vec ; //... code ... for( vector<int>::iterator iter = vec.begin() ; iter != vec.end() ; ++iter ) { // do stuff } I just find that whole vector<int>::iterator syntax sickitating. I know you can typedef vector<int>::iterator VecI...

What _can_ I use as std::map keys?

Extends. I have: struct Coord { int row, col ; bool operator<( const Coord other.row other.col ; } } ; I'm trying to create a map<Coord, Node*>, where you can look up a Node* by Coord. The problem is, it has bugs. Lookups into the map<Coord, Node*> by Coord are returning the wrong ones. I'm having difficulty figuring out ...

Is it allowed to inherit from a class in the std namespace (namely std::wstring)?

The class std::wstring is missing some operations for "normal" c strings (and literals). I would like to add these missing operations in my own custom class: #include <string> class CustomWString : public std::wstring { public: CustomWString(const char*); const char* c_str(void); }; The code above c...

Using STL containers for multiple keys

I came across one requirement where the record is stored as Name : Employee_Id : Address where Name and Employee_Id are supposed to be keys that is, search function is to be provided on both Name and Employee Id. I can think of using a map to store this structure std::map< std:pair<std::string,std::string> , std::string > // ...

not2 stl negator

Hi, Studying STL I have tried to negate a functor with not2 but encontered problems. Here is the example: #include <iostream> #include <vector> #include <functional> #include <algorithm> using namespace std; struct mystruct : binary_function<int,int,bool> { bool operator() (int i,int j) { return i<j; } }; template <class T> class gen...

C++ "Scrolling" through items in an stl::map

I've made a method to scroll/wrap around a map of items, so that if the end is reached, the method returns the first item and vice-versa. Is there more succinct way of doing this? MyMap::const_iterator it = myMap.find(myKey); if (it == myMap.end()) return 0; if (forward) { it++; if (it == myMap.end()) { it = myM...

Need sample problems for hands-on

I have been working with C++ for a few years now and have got good theoretical knowledge on the matter (I think). However I've been missing involvement in good projects, sort of projects that really gets one going on the technologies. So I intend to work on my own to get some good grip on C++ and related technologies. 'Have started with ...

STL allocators and operator new[]

Are there STL implementations that use operator new[] as an allocator? I was just wondering because on my compiler, making Foo::operator new[] private did not prevent me from creating a vector<Foo>... is that behavior guaranteed by anything? ...

struct vs class as STL functor when using not2

Hi. Studing STL I have written a a simple program to test functors and modifiers. My question is about the difference aon using CLASS or STRUCT to write a functor and try to operate on it with function adaptors. As far as I understand in C++ the difference beetween CLASS and STRUCT is that in the last case the members are public by defau...

How do you use a C++ iterator?

I have a vector like so: vector<MyType*> _types; And I want to iterate over the vector and call a function on each of MyTypes in the vector, but I'm getting invalid return errors from the compiler. It appears the pos iterator isn't a pointer to MyType, it's something else. What am I not understanding? Edit: Some code.. for (pos =...

STL iterator - why is the code analysis tool complaining?

I'm checking the results from the static code analysis tool Klocwork. It complains about the following code: 293 for( my_vector_typedef::iterator it( start_pos ); it != end_pos ; ++it ){ 294 delete *it; 295 } With the following message: Object 'it._M_current' is used after it was freed. Object 'it._M_current' was used at line 293 ...