stl

Why do I need std::get_temporary_buffer ?

For what purpose I should use std::get_temporary_buffer? Standard says the following: Obtains a pointer to storage sufficient to store up to n adjacent T objects. I thought that the buffer will be allocated on the stack, but that is not true. According to the C++ Standard this buffer is actually not temporary. What advantages does ...

Best C++ programming practices in professional game development environments?

I'm well educated in the C++ programming language regarding syntax, STL, etc. I haven't done any true projects with it yet other than some college courses. My goal is to start writing progams but attempt to keep it to industry best practices. One of my main goals is to work in the game industry. A door is opening which lead me to ask the...

Bulk reads using Berkeley DB, C++ STL interface

I'm using the C++ STL API to Berkeley DB 4.8, and I'm able to use bulk retrieval for a db_map or db_multimap const iterator created using begin(), but not one created from find() (or lower_bound() for multimaps). I appreciate for single item random access uses of find() would be a waste to use bulk retrieval, but I want to access many r...

STL, reducing an array, c++

For a hw assignment, we are to code a reduce routine that looks like: int reduce(long array[], int size) //Where array is the array to reduce, and size is the size of the array. Using STL. My initial thoughts were to create a set, put all items in the set with a comparison, but then I realized that the set I would create would never...

question about bitset in c++

i have tried to implement following code #include <iostream> #include <bitset> using namespace std; int main(){ //bitset<4>mybits; //cout<<mybits<<endl; int a[]={3,1,4,5,7,8}; int max=a[0]; int t=sizeof(a)/sizeof(a[0]); for (int i=1;i<t;i++){ if (a[i]>max){ max=a[i]; ...

How to call constructor of objects contained in a std::vector?

When I create a std::vector of objects, the constructor of these objects is not always called. #include <iostream> #include <vector> using namespace std; struct C { int id; static int n; C() { id = n++; } // not called // C() { id = 3; } // ok, called }; int C::n = 0; int main() { vector<C> vc; vc.resize...

How to solve a template issue to save different data types to file?

I came a across a new problem when using templates. This is me being a little creative from a book I recently read, expanding his ideas, and it has this code example. Say you have a templated Array2D class. And you have this method (Array2D::WriteFile): bool WriteFile( const char* p_filename ) { FILE* outfile = 0; int writt...

Architectural C++/STL question about iterator usage for O(1) list removal by external systems.

This is a pretty straightforward architectural question, however it's been niggling at me for ages. The whole point of using a list, for me anyway, is that it's O(1) insert/remove. The only way to have an O(1) removal is to have an iterator for erase(). The only way to get an iterator is to keep hold of it from the initial insert() or t...

STL erase-remove idiom vs custom delete operation and valgrind

This is an attempt to rewrite some old homework using STL algorithms instead of hand-written loops and whatnot. I have a class called Database which holds a Vector<Media *>, where Media * can be (among other things) a CD, or a Book. Database is the only class that handles dynamic memory, and when the program starts it reads a file form...

C++/STL: XOR of two set

Given two STL sets, I want to find out the XOR of them. Is there an easy, pre-existing way to do that? ...

Using pair as key in a map (C++ / STL)

Hello, I want to use a pair from STL as a key of a map. #include <iostream> #include <map> using namespace std; int main() { typedef pair<char*, int> Key; typedef map< Key , char*> Mapa; Key p1 ("Apple", 45); Key p2 ("Berry", 20); Mapa mapa; mapa.insert(p1, "Manzana"); mapa.insert(p2, "Arandano"); return 0; } But the compiler...

Templates, STL, C++

I wrote this routine to order items, keep only unique items, where it takes in an array of type T, and the size of the array. It returns the new size of the array after processing. template <class T> int reduce(T array[], int size) { T *begin = array; T *end = array + size; sort(begin, end); T *end_new = unique(begin,...

memmem() STL way?

Is there an STL algorithm which can be used to search a sequence of bytes inside a buffer like memmem() does? ...

Standard predicates for STL count_if

Hi, I'm using the STL function count_if to count all the positive values in a vector of doubles. For example my code is something like: vector<double> Array(1,1.0) Array.push_back(-1.0); Array.push_back(1.0); cout << count_if(Array.begin(), Array.end(), isPositive); where the function isPositive is defined as bool isPosit...

STL: Set of natural numbers from A to B

I want to add natural numbers from A to B in a set. Currently I am inserting each and every number from A to B, one by one in the set like this, set<int> s; for(int j=A; j<=B; j++) s.insert(j); But it takes O(n) time (here n = (B - A)+1). Is there any pre-defined way in STL to do it in O(1) time? Thanks ...

What is the preferred STL collection when that's all you need?

I just need a "bag of things". It doesn't need to be a set, a map or even have any particular order. I just need to be able to add things and iterate over it, nothing more. I don't expect it to be very large but it can't get really bad perf if it does. What container should I use? ...

How is the memory use in a queue ?

In my project I use the std::queue class. I would like to know what happen if I do the following. Get a a pointer of a element inside the queue (note: a pointer and not a iterator). I make modification in the queue like push and pop in the queue (pop element which is not the pointed by the previous pointer) Does my pointer still poin...

Pointers to elements of std::vector and std::list

Hi, I'm having a std::vector with elements of some class ClassA. Additionally I want to create an index using a std::map<key,ClassA*> which maps some key value to pointers to elements contained in the vector. Is there any guarantee that these pointers remain valid (and point to the same object) when elements are added at the end of the...

square-root and square of vector doubles in C++

Hi, I'd like to calculate the square and square-root of a vector of doubles. For example given: vector<double> Array1(10,2.0); vector<double> Array2(10,2.0); for(unsigned int i=0; i<Array1.size(); i++) Array1[i] = sqrt(Array1[i]); for(unsigned int i=0; i<Array2.size(); i++) Array2[i] = Array2[i] * Array2[i]; Is the...

C++ append one vector to another

I fully understand this question has been asked a lot, but I'm asking for a specific variation and my search-foo has given up, as I've only found algorithms that append one existing vector to another, but not one returned to from a function. I have this function that lists all files in a directory: vector<string> scanDir( const string&...