stl

What is better, a STL list or a STL Map for 20 entries, considering order of insertion is as important as the search speed.

I have the following scenario.The implementation is required for a real time application. 1)I need to store at max 20 entries in a container(STL Map, STL List etc). 2)If a new entry comes and 20 entries are already present i have to overwrite the oldest entry with the new entry. Considering point 2, i feel if the container is full (Max...

Using custom std::set comparator

I am trying to change the default order of the items in a set of integers to be lexicographic instead of numeric, and I can't get the following to compile with g++: file.cpp: bool lex_compare(const int64_t &a, const int64_t &b) { stringstream s1,s2; s1 << a; s2 << b; return s1.str() < s2.str(); } void foo() { set<...

C++ STL containers

Different STL containers like vector, stack, set, queue, etc support different access methods on them. If you are coding for example in Notepad++ or vim, you have to continuously refer to the documentation to see what all methods are available, atleast I have to. Is there some good way of remembering which container supports which meth...

How to change a particular element of a C++ STL vector

vector<int> l; for(int i=1;i<=10;i++){ l.push_back(i); } Now, for example, how do I change the 5th element of the vector to -1? I tried l.assign(4, -1); It is not behaving as expected. None of the other vector methods seem to fit. I have used vector as I need random access functionality in my code (using l.at(i)). ...

will a call to std::vector.clear() also set the std::vector.reserve() to zero?

If I use .reserve(items) on a vector, the vector will allocate enough memory for my guess of the number of items that I'll need. If I later on use .clear(), will that just clear the vector or save my earlier defined reserve? thanks. ...

How do I force my std::map to deallocate memory used?

I'm using a std::map, and I can't seem to free the memory back to the OS. It looks like, int main(){ aMap m; while(keepGoing){ while(fillUpMap){ //populate m } doWhatIwantWithMap(m); m.clear();//doesnt free memory back to OS //flush some buffered values into map for next iteration flushIntoMap(m); ...

How to restrict the range of elements of C++ STL vector?

vector<int> l; for(int i=0;i<10;i++){ l.push_back(i); } I want the vector to only be able to store numbers from a specified range (or set). How can that be done, in general? In particular, I want to restrict the vector to beonly be able to store single digits. So, if I do a l[9]++ (in this case l[9] is 9), it should give me an...

question about STL thread-safe and STL debugging

I have two questions about STL 1) why STL is not thread-safe? Is there any structure that is thread-safe? 2) How to debug STL using GDB? In GDB, how can I print a vector? ...

m.find(...) == m.end() - which is used, iterator or const_iterator

std::map find/end both provides const_iterator and iterator, e.g. iterator end (); const_iterator end () const Out of curiosity,if I have a std::map , which will be called/compared here, an iterator or a const_iterator ? : if(m.find(key) != m.end()) { ... } And should I care ? ...

Using a map with set_intersection

Not used set_intersection before, but I believe it will work with maps. I wrote the following example code but it doesn't give me what I'd expect: #include <map> #include <string> #include <iostream> #include <algorithm> using namespace std; struct Money { double amount; string currency; bool operator< ( const Money& rhs ...

How to detect first or last element iterating over a container?

How to do the following in more stylish/short way? for(i=container.begin(); i!=container.end(); ++i) { if (i!=container.begin()) { cout << ", "; } cout << *i; j=i; if (++j==container.end()) { cout << "!" << endl; } } Solutions like foreach are acceptable (actions on first and last elements need...

std::list or std::multimap

Hey, I right now have a list of a struct that I made, I sort this list everytime I add a new object, using the std::list sort method. I want to know what would be faster, using a std::multimap for this or std::list, since I'm iterating the whole list every frame (I am making a game). I would like to hear your opinion, for what should I ...

Getting meaningful error messages from fstream's in C++

What is the best way to get meaningful file access error messages, in a portable way from std::fstreams ? The primitiveness of badbits and failbits is getting to be bit annoying. I have written my own exception hierarchies against win32 and POSIX before, and that was far more flexible than the way the STL does it. I am getting "basic::i...

Problem with std::map and std::pair

Hi everyone. I have a small program I want to execute to test something #include <map> #include <iostream> using namespace std; struct _pos{ float xi; float xf; bool operator<(_pos& other){ return this->xi < other.xi; } }; struct _val{ float f; }; int main() { map<_po...

Does std::multiset guarantee insertion order?

I have a std::multiset which stores elements of class A. I have provided my own implementation of operator< for this class. My question is if I insert two equivalent objects into this multiset is their order guaranteed? For example, first I insert a object a1 into the set and then I insert an equivalent object a2 into this set. Can I exp...

Compilation problems with vector<auto_ptr<> >

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector<auto_ptr<A> > as; for (int i = 0; i < 10; i++) { auto_ptr<A> a(new A(i)); as.push_back(a); } for (vector<auto_ptr<A> >::itera...

Boost::Interprocess Container Container Resizing No Default Constructor

Hi, After combing through the Boost::Interprocess documentation and Google searches, I think I've found the reason/workaround to my issue. Everything I've found, as I understand it, seems to be hinting at this, but doesn't come out and say "do this because...". But if anyone can verify this I would appreciate it. I'm writing a series...

How to use mmap to point to STL type?

I have a mmap typecast to a char pointer char *ptr; ptr = (char *)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); This was my earlier code. But now I want to use a map instead of char * as the requirements changed. Now, my map is declared as map < int, string > i_s_map; How do I change my mmap call to point to the map?...

Suggest changes to make the time a function of input size

Vector v; int i=0; while(i!=999) { cin>>i; v.push_back(i); } Time taken by this piece of code could vary when the number of inputs vary. Since vector would take amortized time for new allocation. Even for same size the program at different times could take different time. Suggest changes (e.g. use list instead of vector), which...

Finding the left-most and right-most points of a list. std::find_if the right way to go?

Hi, I have a list of Point objects, (each one with x,y properties) and would like to find the left-most and right-most points. I've been trying to do it with find_if, but i'm not sure its the way to go, because i can't seem to pass a comparator instance. Is find_if the way to go? Seems not. So, is there an algorithm in <algorithm> to ac...