stl

display map every time it is updated sorted by value

basically, I have the map<std::string, int> so if i have foo 5 bar 10 jack 3 in the map, I want to display it (notice the reverse order) bar 10 foo 5 jack 3 And every time it is updated, I want iterate through all the elements, cout them, sorted by value. What is the good way to implement that? should I provide a comparat...

AV while iterating through hash_map?

_transaction is a private member variable of my class, declared as: public: typedef stdext::hash_map<wchar_t*, MyClass*, ltstr> transaction_hash_map; private: transaction_hash_map _transactions; During cleanup I am trying to iterate through this list and free up any objects still unfreed. However I am getting an AV on the for...

Memory corrupt in adding string to vector<string> loop

This is on Visual Studio 2008 on a dual-core, 32 bit Vista machine. In the debug code this runs fine, but in Release mode this bombs: void getFromDB(vector<string>& dates) { ... sql::Resultset res = stmt->executeQuery("SELECT FROM ..."); while (res->next()) { string date = res->getString("date"); dates.push_bac...

Setting the internal buffer used by a standard stream (pubsetbuf)

I'm writing a subroutine that needs to write data to an existing buffer, and I would like to use the stringstream class to facilitate the formatting of the data. Initially, I used the following code to copy the contents of the stream into the buffer, but would like to avoid this solution as it copies too much data. #include <sstream> #...

How do I Search/Find and Replace in an STL string

Is there a way to replace all occurrences of a substring with another string in std::string? For instance: void SomeFunction(std::string& str) { str = str.replace("hello", "world"); //< I'm looking for something nice like this } ...

Reversing strings in a vector using for_each and bind

Hi! I was wandering how it's possible to reverese strings that are contained in a vector using a single for_each command just in one "simple" line. Yea, I know it is easy with a custom functor, but I can't accept, that it can't be done using bind (at least I couldn't do it). #include <vector> #include <string> #include <algorithm> std...

std::vector and its iterator as single template typename

In order to get an "easier-to-remember" interface to the index-generating function std::distance(a,b), I came up with the idea of a better distinction of it's arguments (when used against the base of a vector: vec.begin() ) by calling a templated function with the vector and its iterator, like: std::vector<MyType> vect; std::vector<My...

Renaming first and second of a map iterator

Is there any way to rename the first and second accessor functions of a map iterator. I understand they have these names because of the underlying pair which represents the key and value, but I'd like the iterators to be a little more readable. I think this might be possible using an iterator adaptor, but I'm not sure how to implement ...

Nested std::maps

Supposed I have a type I'll call NamedNestedMap std::map<std::string, std::map<std::string, NamedNestedMap> > In this case each second (value) of the pair is the same kind or type as the parent object. What I can't figure out is how to declare it. This will allows a recursive algorithm to walk down thru the "tree" of maps. The Va...

convert a pointer to a reverse vector iterator in STL

I have sort(arr, arr+n, pred); How do I sort in reverse order? ...

What is the optimization level ( g++ ) you use while compairing two different algorithms written in C++ ?

I have two algorithms written in C++. As far as I know, it is conventional to compile with -O0 -NDEBUG (g++) while comparing the performance of two algorithms(asymptomatically they are same). But I think the optimization level is unfair to one of them, because it uses STL in every case. The program which uses plain array outperforms the...

Removing from STL std::queue without destructing the removed object?

All the documentation I can find on the STL containers (both queue and list) say that for any of the remove functions the removed object's destructor is called. This means that I can't use std::queue any time I want a queue that's simply a list of objects needing some operation performed on them. I want to be able to add objects to the...

stl::find_if with user search

I was wondering if there was a way to use the stl::find_if to search for a user inputted value I don't know to do that without using any bad conventions(globals) or adding loads of extended code. For example, if a user inputs a int x for 10, then I want to search an vector of ints iterator = find_if(begin,end,pred) //but how does pre...

How to use hash_map with char* and do string compare?

I was using std::hash_map<char*,T> and somehow managed to make it work but have now discovered that the default compare function, euqal_to<char*> does a pointer compare rather than a string compare. I've fixed this by making my own compare type (using C's strcmp and it's about 5 LOC) but I'd be slightly shocked if there wasn't one alread...

priority_queue<> comparison for pointers?

Hello, So I'm using the STL priority_queue<> with pointers... I don't want to use value types because it will be incredibly wasteful to create a bunch of new objects just for use in the priority queue. So... I'm trying to do this: class Int { public: Int(int val) : m_val(val) {} int getVal() { return m_val; } private: int ...

Delete all items from a c++ std::vector

I'm trying to delete everything from a std::vector by using the following code vector.erase( vector.begin(), vector.end() ); but it doesn't work, help! Update: Doesn't clear destruct the elements held by the vector? I don't want that, as I'm still using the objects, I just want to empty the container ...

C++ using list with count() function

Hi, I have a list L which needs to count how many 1s it has in it. list<int> L; L.push_back(14); L.push_back(5); L.push_back(22); L.push_back(1); L.push_back(1); L.push_back(-7); the function that i have been given is : assert ( count(...,...,...) == 2); i need to know what would replace the ...'s. i have tried L.begin(), L.end(...

C++ writing a modify_if function

Hi, I have a hw question which confuses me on what i have to do. The question is below: The idea is to design a generic function called Modify_If that will take an input x (passed by reference), and two functors f1 and f2. The function Modify_If will use functor f1 to determine whether x obeys a certain condition. If it does, Modify_if...

C++ templated functors

Hi, I was wondering if anyone can help me with functors. I dont really understand what functors are and how they work I have tried googling it but i still dont get it. how do functors work and how do they work with templates ...

Ternary operator on auto_ptr content not working

I initialize an auto_ptr to NULL and later in the game I need to know if it has NULL or not to return it or a new copy. I've tried this auto_ptr<RequestContext> ret = (mReqContext.get() != 0) ? mReqContext : new RequestContext(); And several other similar stuff casting and so, but g++ tries to invoke auto_ptrs nonexistent operator? (...