stl

openGL and STL?

hi, I am using openGL and am currently passing it a vertex array. The problem is that I have to create many vertices, and add them in between one another (for order). This means that using a regular array is pretty annoying/inefficient. I want to use a data structure from STL so that I can efficiently (and easily) put new vertices at a...

C++ Member Function Pointers and STL Algorithm Problem

Okay I have a bit of a problem which i'm struggling to solve. Basically I have an abstract functor class that overloads operator() and derived objects that implement it. I have a function (part of another class) that tries to take an Array of these functor classes and tries to pass a pointer to a member function to the std algorithm fo...

Member variables and STL algorithms

#include <vector> #include <functional> #include <algorithm> using namespace std; struct Foo { int i; double d; Foo(int i, double d) : i(i), d(d) {} int getI() const { return i; } }; int main() { vector<Foo> v; v.push_back(Foo(1, 2.0)); v.push_back(Foo(5, 3.0)); vector<int> is; ...

How to make elements of vector unique? (remove non adjacent duplicates)

I have a vector containing few non-adjacent duplicates. As a simple example, consider: 2 1 6 1 4 6 2 1 1 I am trying to make this vector unique by removing the non-adjacent duplicates and maintaining the order of elements. Result would be: 2 1 6 4 The solutions I tried are: Inserting into a std::set but the problem with this a...

What is the reason for a segmentation fault with this C++ code using lists?

I have some complicated C++ code but the problem narrows down to doing a push_back on a list of structures: list<cache_page> cachedPages; void f() { cache_page cpage(a,b); cachedPages.push_back(cpage); } I have commented all the data members of the struct cache_page and still the error persists. If I comment the push_back lin...

Why STL implementation is so unreadable? How C++ could have been improved here?

For instance why does most members in STL implementation have M or _ or __ prefix? Why there is so much boilerplate code ? What features C++ is lacking that would allow make vector (for instance) implementation clear and more concise? ...

Where can I find source of unhook function used in STL implementation provided with g++.

It is used in /usr/include/c++/4.3/stl_list.h on my system (current Ubuntu). ...

std::vector reserve() and push_back() is faster than resize() and array index, why?

I was doing a quick performance test on a block of code void ConvertToFloat( const std::vector< short >& audioBlock, std::vector< float >& out ) { const float rcpShortMax = 1.0f / (float)SHRT_MAX; out.resize( audioBlock.size() ); for( size_t i = 0; i < audioBlock.size(); i++ ) { out[i] = (float...

Using std::bind2nd with references

I have a simple class like this: class A { public: void f(const int& n) { std::cout<<"A::f()" << n <<"\n"; } }; and I am trying to use it like this: std::vector<A> vec; A a; vec.push_back(a); std::for_each(vec.begin(), vec.end(), std::bind2nd(std::mem_fun_ref(&A::f), 9)); But when I compile the code I get the follo...

Transfer ownership within STL containers?

Is it possible to transfer ownership of a vector contents from one vector to another? vector<T> v1; // fill v1 vector<T> v2 = OvertakeContents(v1); // now v1 would be empty and v2 would have all the contents of v1 It is possible for lists with splice function. This should be possible in constant time for whole vector as well. If it...

Standard Library Containers with additional optional template parameters?

Having read the claim multiple times in articles - I want to add this question to Stackoverflow, and ask the community - is the following code portable? template<template<typename T, typename Alloc> class C> void f() { /* some code goes here ... */ } int main() { f<std::vector>(); } Is the implementation that supplies std::vector...

Matrix Circular Shift

Does anyone know an efficient way to right circular-shift a matrix? Btw, the matrix is binary but a method to solve a non-binary matrix is also fine. Right now, I'm thinking of implementing a circular array for the rows of my matrix and updating each row whenever a shift operation is required. Another method, I was considering was impl...

C++ STL - iterate through everything in a sequence

I have a sequence, e.g std::vector< Foo > someVariable; and I want a loop which iterates through everything in it. I could do this: for (int i=0;i<someVariable.size();i++) { blah(someVariable[i].x,someVariable[i].y); woop(someVariable[i].z); } or I could do this: for (std::vector< Foo >::iterator i=someVariable.begin(); i...

How can I make my char buffer more performant?

I have to read a lot of data into: vector<char> A 3rd party library reads this data in many turns. In each turn it calls my callback function whose signature is like this: CallbackFun ( int CBMsgFileItemID, unsigned long CBtag, void* CBuserInfo, int CBdataSize, void* CBdataBuffe...

Should I expose iterators and adaptor methods or a whole container in C++?

Consider the piece of code: class Foo { // ... std::vector<Bar> bars; }; Should I expose the whole container, or should I expose typedef'd iterator class and write adaptor methods (begin(), end(), size(), and whatever I need)? If the answer is it depends, how should one make a decision? ...

How to require certain concepts in C++ code?

How do I require and check that an argument is a certain concept in C++? For example, the random_shuffle function in the algorithm header requires that its arguments are RandomAccessIterators: template<typename _RandomAccessIterator> inline void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) {...

C++ std::transform() and toupper() ..why does this fail?

Hi, I have 2 std::string. I just want to, given the input string: capitalize every letter assign the capitalized letter to the output string. How come this works: std::string s="hello"; std::string out; std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper); but this doesn't (results in a program crash)?...

How to efficiently copy istringstream?

Or ostringstream? istringstream a("asd"); istringstream b = a; // This does not work. I guess memcpy won't work either. ...

Can I use std::stack as object pool container?

I need to create a pool of objects to eliminate dynamic allocations. Is it efficient to use std::stack to contain pointers of allocated objects? I suspect every time I push the released object back to stack a new stack element will be dynamically allocated. Am I right? Should I use std::vector to make sure nothing new is allocated? Tha...

best lib for vector array in c++

I have to do calculation on array of 1,2,3...9 dimensional vectors, and the number of those vectors varies significantly (say from 100 to up to couple of millions). Of course, it would be great if the data container can be easily decomposed to enable parallel algorithms. I came across blitz++(almost impossible to compile for me), but a...