stl

Populate a static member container in c++

I've got a static class member which is some container, like (Foo.h) class Foo { ... private: static list<string> s_List; } I need to populate the list with a number of specific values. Actually it should be also const, but that might overcomplicate the problem further. All the class member functions are static, so initializing...

Howto elegantly extract a 2D rectangular region from a C++ vector

The problem is pretty basic. (I am puzzled why the search didn't find anything) I have a rectangular "picture" that stores it's pixel color line after line in a std::vector I want to copy a rectangular region out of that picture. How would I elegantly code this in c++? My first try: template <class T> std::vector<T> copyRectFr...

std::map<tstring<std::map<tstring, unsigned int>> assignment fail

basically i have (state, state code) pairs, that are subsets of country [USA] -> [VT] -> 32 so i'm using std::map<tstring<std::map<tstring, unsigned int>> but i'm having trouble with assignment of the state code for(std::map<tstring, std::map<tstring, unsigned int>>::const_iterator it = countrylist.begin(); it != countrylist.end(); ++i...

VC++ 2005 project option to include stl?

I'm working on a cross platform project that uses STL. The other compiler includes STL support by default, but in VS2005 I need to add the following before the class definitions that use STL items: #include <cstdlib> using namespace std; Is there a VS2005 option that would set this automatically? It's just a bit tedious to work around...

Initializing a vector before main() in C++

I want to be able to initialize a vector of a size 'SIZE' before main. Normally I would do static vector<int> myVector(4,100); int main() { // Here I have a vector of size 4 with all the entries equal to 100 } But the problem is that I would like to initialize the first item of the vector to be of a certain value, and the othe...

boost::filter_iterator -- how would I do that with the STL?

I am passed an Iterator and I have to pass it on to another function -- but filtered so that certain elements are skipped (it's a range of pointers, and I want to filter out the NULL pointers). I googled for "stl filter iterator" to see how to do this, and boost::filter_iterator came up. That looks nice and I could use it, but could ...

linked list memory management

How could I free up a linked list that contains dynamically allocated objects? I try to use list<class*> lists, but then I could not use the insert() function to insert object to the list. Does anyone know what is the cause? ...

Weird bug while inserting into C++ std::map

I'm trying to insert some value pairs into a std::map. In the first case, I receive a pointer to the map, dereference it and use the subscript operator to assign a value. i.e. (*foo)[index] = bar; Later, when I try to iterate over the collection, I'm returned key/value pairs which contain null for the value attribute in all cases ex...

Difference between erase and remove

I am bit confused about the difference between the usage of std::remove algorithm. Specifically I am not able to understand what is being removed when I use this algorithm. I wrote a small test code like this: std::vector<int> a; a.push_back(1); a.push_back(2); std::remove(a.begin(), a.end(), 1); int s = a.size(); std::vector<int>::...

remove_if equivalent for std::map

I was trying to erase a range of elements from map based on particular condition. How do I do it using STL algorithms? Initially I thought of using remove_if but it is not possible as remove_if does not work for associative container. Is there any "remove_if" equivalent algorithm which works for map ? As a simple option, I thought of ...

C++ char* vs std::string

When I use std::string and when char* to manage arrays of chars in C++? It seems you should use char* if performance(speed) is crucial and you're willing to accept some of a risky business because of the memory management. Are there other scenarios to consider? ...

How can I inspect an STL list in the CodeWarrior debugger?

Is there any easy way to view the data in an STL std::list<T> in the Metrowerks CodeWarrior debugger? I can view data near the beginning or end of the list by looking at expressions such as instances->__list_imp.__list_deleter.end_.compressed_pair_imp.second_.prev_->data_ I can expand the little '+' signs next to the struct members t...

Is there a STL way to validate that a (text) file is read-only first?

Also, is there a cross-platform way to change the attribute of a file from read-only to writeable? Thanks much, Bert ...

Why does std::ends cause string comparsison to fail?

I spent about 4 hours yesterday trying to fix this issue in my code. I simplified the problem to the example bellow. The idea is to store a string in a stringstream ending with std::ends, then retrieve it later and compare it to the original string. #include <sstream> #include <iostream> #include <string> int main( int argc, char** ...

how to make a map<CLSID, string>?

I want to create a container which can associate a CLSID structure to something else (for example, a string); for example, std::map. (the CLSID means standard Windows CLSID structure) However when I want to use its find() and insert (object[clsid] = string), the STL just failed and gives errors. Does anyone know how to solve this? Fo...

simple cout then cin allowing whitespace example?

Hello, Looking for a simple getline example which works correctly. I want to input something on the keyboard and assign it to a std::string, allowing for whitespace and tabs. The delimiter is the carriage return. TIA, Bert ...

How big is the performance gap between std::sort and std::stable_sort in practice?

Both should run in O(n log n), but in general sort is faster than stable_sort. How big is the performance gap in practice? Do you have some experience about that? I want to sort a very large number of structs that have a size of about 20 bytes. The stability of the result would be nice in my case, but it is not a must. At the moment the...

Is it possible to create a vector of pointers?

Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin() with it, ie: How would I turn this vector into a vector of pointers: class c { void virtual func(); }; class sc:public c { void func(){cout<<"using func...

Determining whether an object is in a std::set

I'm trying to determine whether an object is already contained within a std::set. According to msdn (and other sources) the set::find function is supposed to return end() if it doesn't find the element you asked for. However when I implement code like the following, set::find returns junk (0xbaadf00d) instead. set<Cell*> cellSet; Cell...

How to check whether STL iterator points at anything?

I want to do something like this: std::vector<int>::iterator it; // /cut/ search for something in vector and point iterator at it. if(!it) //check whether found do_something(); But there is no operator! for iterators. How can I check whether iterator points at anything? ...