stl

std::list fixed size

How can I create std::list with a fixed element count? ...

List iterator error

Hello, I make stack based in std:list. Now i try to make functions for showing all elements from this stack: I try to declare iterator: void show_elements() { list<T>::iterator it; } But i get error: error: dependent-name ‘std::list::iterator’ is parsed as a non-type, but instantiation yields a type What's wrong? Thank you. ...

Parsing with Boost::Spirit (V2.4) into container.

I just started to dig into Boost::Spirit, latest version by now -- V2.4. The essense of my problem is following: I would like to parse strings like "1a2" or "3b4". So the rule I use is: (double_ >> lit('b') >> double_) | (double_ >> lit('a') >> double_); The attribute of the rule must be "vector <double>". And I'm reading it into ...

Logical error in Function template.

My professor has given me this assignment. Implement a generic function called Max, which takes 3 arguments of generic type and returns maximum out of these 3. Implement a specialized function for char* types. Here's my code : #include <iostream> #include <string> using namespace std; template<typename T> T Max(T first,T ...

std::find vs. deriving template from vector

I use vectors a lot in my programming, and generally to traverse a vector for an existing value I use std::find as in: std::vector<int> foo; std::vector<int>::iterator pos( std::find( foo.begin(), foo.end(), bar ); This is a real bore. So I went to deriving a template from std::vector to provide a find method: template<class T> class...

Designing and coding a non-fragmentizing static memory pool

I have heard the term before and I would like to know how to design and code one. Should I use the STL allocator if available? How can it be done on devices with no OS? What are the tradeoffs between using it and using the regular compiler implemented malloc/new? ...

Can I define a map whose key is a structure?

and how can I do it in C++? ...

Vector handling bullets in DirectX

I have a vector to hold objects of a bullet class. Is this the correct way to add bullets to the vector structure? std::vector<Bullet> bullets; Bullet newbullet(thisPlayer.x+PLAYERSPRITEWIDTH,(thisPlayer.y-(PLAYERSPRITEHEIGHT/2))); bullets.push_back(newbullet); I don't think the bullets get added this way. ...

Detailed difference between functor's call and function call ?

The key reason this works is that for_each () doesn’t actually assume its third argument to be a function. It simply assumes that its third argument is something that can be called with an appropriate argument. A suitably defined object serves as well as – and often better than – a function. For example, it is easier to ...

recursive stl map

I'm trying to make a tree of maps (or just have values of a map point to another map), but I'm not too sure how to approach this. I found a discussion about this: http://bytes.com/topic/c/answers/131310-how-build-recursive-map but I'm a little confused on what's going on there. For example, my key is a char, and my value is the next ma...

How to get the exact position of an element in a set?

I have a std::set<std::string> and I want to know the exact position of the element in the set after the insertion. I tried with std::distance but without any luck: #include <iostream> #include <string> #include <set> #include <iterator> using namespace std; int main (int argc, char const *argv[]) { string array[] = { "zero", "o...

c++ STL map copy setting boolean value to true.

Hi All, I'm having a problem with stl map. Initially I fill the map with data like so. //loop pair< int, int > xy (x,y); currentMap.insert( make_pair(xy), value); //map< pair<int, int>, bool> prevMap.insert( make_pair(xy), value); // End Loop Then I delete an element according to some rules like so. currentMap.erase( make_pair(xy) )...

Error trying to make an wrapper of the STL map container

I'm trying to make a wrapper to the STL map container, in order to add a const method to return the value given the key. In map, operator[] isn't const, and find() requires dereferencing to get the value (map.find()->second). I'm basing some of my "research" off of http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-...

What makes this bucket sort function slow?

The function is defined as void bucketsort(Array& A){ size_t numBuckets=A.size(); iarray<List> buckets(numBuckets); //put in buckets for(size_t i=0;i!=A.size();i++){ buckets[int(numBuckets*A[i])].push_back(A[i]); } ////get back from buckets //for(size_t i=0,head=0;i!=numBuckets;i++){ //size_t bucket_size=buckets[...

Avoiding Memory Leaks w/ Exceptions in C++

I allocate objects on the heap and under some circumstances I combine them into a new object (my Foo class mainly contains 2-3 STL containers). (An alternative would be to use copies, but I guess that would be less efficient.) These operations may fail, thus exceptions may be thrown. Unless I release the dynamic memory whenever I throw ...

C++ To call member function in for_each for items in the member container.

If I have a class (that mimic some of STL's container) like this: class Elem { public: void prepare(); // do something on *this // ... }; class Selector { public: typedef vector<Elem *> container_type; typedef container_type::iterator iterator; iterator begin() { return cont_.begin(); } iterator end() { return cont_.end(...

Why can't I use reference types as the value type of a container type?

For example, std::vector<int&> vec_int; this seems to be invalid in c++. Why is this invalid? ...

Why is there no "Iterable" interface in the STL?

The C++ STL does not seem to use purely abstract base classes (aka interfaces) very often. I know that most things can be achieved with the STL algorithms or clever template metaprogramming. But still, for some use cases (for example, in an API, if I do not want to be specific about the type of container I get, just about the elements ...

does exists a stl data structures that "holds" a current element index/pointer ?

i would like to improve my c++ code style so i decided that i definitively have to deep into stl...at first, as i need it in a real case, i woudl like to know if it is available some kind of container that hold a current index inside... for example i mean some container class i can navigate with next()/prev() but i can also ask for g...

Fast and flexible iterator for abstract class

In order to traverse grids with data in a fast and flexible way I set up an abstract, templated GridDataStructure class. The data should be accessed by STL iterators. When someone uses the class, he should not worry about which kind of STL iterator is appropriate for a specific subclass. A solution to this problem seems to be http://st...