stl

Easier way to do callbacks for vectors (or maybe something else in the STL)? C++

I'm making a simple crime sim game. Throughout it I keep doing the same thing over and over: // vector<Drug*> drugSack; for (unsigned int i = 0; i < this->drugSack.size(); i++) this->sell(drugSack[i]); Just one example. I hate having all these for loops all over the place omg QQ, anyway to do something like: drugSack->D...

Sorting std::list using std::set

I'm adding two different elements to both std::list and std::set and I want the std::list to be sorted with the same order as of std::set. one way I tried is when the element is added to std::set, find that element then get the index of that element using std::distance(begin, found) and then insert the element to that index in std::list....

stl vector assign vs insert

I understand the semantics of the 2 operations , assign- erases before replacing with supplied values. insert - inserts values at specified location(allocates new memory if necessary). Apart from this is there any reason to preffer one over the other? Or to put it another way, is there any reason to use assign instead of insert. ...

Why does the interface for auto_ptr specify two copy-constructor-like constructors

I was going through the auto_ptr documentation on this link auto_ptr There is something which i could not fully understand why is it done. In the interface section there are two declarations for its copy constructor 1) auto_ptr(auto_ptr<X>&) throw (); 2) template <class Y> auto_ptr(auto_ptr<Y>&) throw(); What purpose i...

what is auto_ptr_ref, what it achieves and how it achieves it

auto_ptr_ref documentation here says this This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions. Can somebody explain how auto_ptr_ref helps in achieving this. I just want to understand the auto_ptr class and its internals ...

Use of unique on STL vector with a structure

In a programming task, I'm trying to ensure a particular vector contains only unique items. With primitive types, the operation is as simple as: vector<int> lala; lala.push_back(1); lala.push_back(99); lala.push_back(3); lala.push_back(99); sort(lala.begin(), lala.end()); // lala: 1, 3, 99, 99 lala.erase(unique(lala.begin(), lala.end()...

Is there a readable implementation of the STL?

I'm on Linux; looking at the STL headers; they're really really complicated. Is there, somewhere, a smaller version of STL that has the core features of the STL, but is actually readable? Thanks! ...

gcc reverse_iterator comparison operators missing?

I am having a problem using const reverse iterators on non-const containers with gcc. Well, only certain versions of gcc. #include <vector> #include <iostream> using namespace std; int main() { const char v0[4] = "abc"; vector<char> v(v0, v0 + 3); // This block works fine vector<char>::const_iterator i; for (i = ...

Fast cross platform algorithm for reading/writing file in C++

I want to pose a seemingly simple question that i can't find the answer nowhere. Is there a FAST modern algorithm for file input and/or output that can be compiled with all standard compliant C++ compilers and works for all operating systems without the requirement of external libraries ? i have found that the fastest way is to use mem...

Using a STL map of function pointers

Hello, I developed a scripting engine that has many builtin functions, so far to call any function code just went into a if .. else if .. else if wall checking the name but I would like to develop a more efficient solution. Shoul I use a hashmap with strings as keys and pointers as values. How could I do it by using an STL map? EDIT: ...

Performance issue with graph incremental construction

I am working on a software where i have to create a graph (using boost::adjacency_list). The incremental insertion of vertices takes an extremely long time. Until now, i hadn't worked on the issue, because the use of STLport made this problem go away. I have now migrated my work to Visual Studio 2008, but can't take the time to go on us...

STL priority_queue copies comparator class

I'm trying to create a priority queue with a custom comparator: std::priority_queue<int, std::vector<int>, MyComparator> pq; My problem is that MyComparator has a method that stores additional state. Because MyComparator is copied to the priority queue (as far as I can tell), there's no way for me to call this method on the MyComparat...

typedef and containers of const pointers

The following line of code compiles just fine and behaves: list<const int *> int_pointers; // (1) The following two lines do not: typedef int * IntPtr; list<const IntPtr> int_pointers; // (2) I get the exact same compile errors for list<int * const> int_pointers; // (3) I'm well aware that the last line is not legal since the...

Is it safe to use C++ STL containers on multiple threads if no insertions and only .find()?

In C++, is it safe to use an std::map or std::vector concurrently in different threads if you are NOT inserting, just doing .find() operations on it? ...

Iterating the std::list through a const_iterator

is it possible to iterate through until the end of list in main() function using the const_iterator? I tried using iter->end() but i can't figure it out. #include <list> #include <string> using std::list; using std::string; class list_return { public: list <string>::const_iterator get_list() { _list.push_back("1"); _list.push_back("2")...

Can I pass a parameter to an std::vector sort function?

Consider the class: MyClass { int varA; int varB; }; I have a vector of pointers to MyClass objects: std::vector<MyClass*> Vec; I want to sort the vector according to varA or varB using the same sort function, i.e. : bool SortFunction(const MyClass* obj1, const MyClass* obj2, const short type) { if( type == VARA_ID ) ...

C++ compilation for iPhone (STL issue?)

Hello, I am trying to compile some C++ code as a static library to use on the iPhone. If I compile things for the simulator (i386 architecture), everything compiles just peachy, but when I switch the architecture to arm, I get all these include errors, seemingly within the iPhone SDK STL headers. Any idea what's going on? First of the e...

How to find the memory used by any object

class Help { public: Help(); ~Help(); typedef std::set<string> Terms; typedef std::map<string, std::pair<int,Terms> > TermMap; typedef std::multimap<int, string, greater<int> > TermsMap; private: TermMap terms; TermsMap termsMap; }; How can we find the memory used (in bytes...

Why can't we declare a std::vector<AbstractClass> ?

Having spent quite some time developping in C#, I noticed that if you declare an abstract class for the purpose of using it as an interface you cannot instantiate a vector of this abstract class to store instances of the children classes. #pragma once #include <iostream> #include <vector> using namespace std; class IFunnyInterface { p...

How can I store a pair of numbers in C++?

I'm trying to learn C++ and right now I'm writing a program that needs to output a list of pairs of integers. What is the best way to handle this? I don't have the boost library available on our linux computers at school, so I don't believe I can use boost::tuple. Any suggestions? ...