stl

Store Templated Object in Container

Is it possible to store a templated class like template <typename rtn, typename arg> class BufferAccessor { public: int ThreadID; virtual rtn do_work(arg) = 0; }; BufferAccessor<void,int> access1; BufferAccessor<int,void> access2; in the same container like a vector or list edit: The purpose for this is I am trying to ...

Can I create a map with a dynamic constructed comparer?

I want to create std::map in STL, but the comparer depends some dynamic value which is available only at runtime.. How can I make this? For example, I want something looks like std::map<int, int, Comp(value1, value2)>. value1 and value2 are not the compared number here, they are some kind of configuration numbers. ...

lower_bound in set (C++)

Hi I've got a set and I want to find the largest number not greater than x in it. (something like lower_bound(x) ) how should i do it? Is there any predefined functions? set<int> myset; myset.insert(blahblahblah); int y; //I want y to be greatest number in myset not greater than x ...

STL Set: insert two million ordered numbers in the most efficient manner

For the following mass-insert, because the inputs are ordered, are there any (slight) optimizations? set<int> primes; for ( int i = 2; i <= 2000000; i++ ) { primes.insert(i); } // then follows Sieve of Eratosthenes algorithm New improvement, twice as fast: set<int> primes; for ( int i = 2; i <= 2000000; i++ ) { primes.inser...

how would I sort a list and get the top K elements? (STL)

I have a vector of doubles. I want to sort it from highest to lowest, and get the indices of the top K elements. std::sort just sorts in place, and does not return the indices I believe. What would be a quick way to get the top K indices of largest elements? ...

In C++, removing an object from a list

I'm writing a program which is more or less like this : #include <list> list<MyClass> things; class MyClass { // some stuff void remove() { things.remove_if(THING_IS_ME); } }; What do I need to write instead of THING_IS_ME? In other words, I'm using a global STL list as a collection of things. At some point, an obj...

Are standard library warnings normal?

I'm learning my way around visual studio at the moment. If I've turned set /Wall -- I'll be greeted with a seemingly unhealthy amount of warning messages although my application will compile just fine. Is this normal? Changing the error level will stop the messages. It looks as if they are all related to the C++ STL or its header files ...

C++ STL:: what's the difference between inplace_merge and sort

As far as I can tell, inplace_merge does the exact same thing as sort, except it only works in certain circumstances (When the container is already in two sorted parts). In other words, is there an difference between these two: int first[] = {1,3,5,7}; int second[] = {2,4,6,8}; vector<int> v(8); vector<int>::iterator it; copy(first,fir...

STL queue push behavior

I come across a behavior in STL queue push which I don't quite understand. Basically, I have two struct structA{ string a; } structB{ char b[256]; } structA st1; structB st2; ...assign a 256 characters string to both st1 and st2... queue<structA> q1; queue<structB> q2; for(int i=0 ; i< 10000; i++){ q1.push(st1); } for(int i...

C++ STL vector reserve

I have tested on stl vector with code below: struct structA{ char charArray[256]; } structA a; ..assign 256 characters to a.charArray vector<structA> v1; v1.reserve(1000); for(int i=0; i<1000; i++){ v1.push_back(a); } I realized that for every 16 push_back, there is a spike in the v1.push_back. I suspect that there is a reall...

c++ std::vector Orphan Range error

A program dealing with graphs(from graph theory) representation and transformation.The adjacency list and matrix are implemented like dynamic arrays of vectors(don't ask why not vector of vector) for the following function program exits with memory error and compiler pointing to the orphan vector definition. int vertex,edges; vector<int...

C4503 warnings? How do i solve/get rid of them?

Hi, It's my first time trying out C++ STL. I'm trying to build a multidimensional associative array using map. For example: typedef struct DA { string read_mode; string data_type; void *pValue; void *pVarMemLoc; }DA; int main() { map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA; ...

Can make STL string::c_str() return NULL when it has no string?

My project has legacy library which consider NULL pointer as empty string. But when I get return data from std::wstring like this, std::wstring strData; const wchar* pStr = strData.c_str(); ASSERT(NULL == pStr); // ASSERT!! pStr is not NULL but pointer which wstring point. Can I make std::string return NULL when it has no string da...

Finding max_element of a vector where a member is used to decide if its the maximum.

Consider a class A having a member x and a std::vector< A >. Now its a common task to search for the maximal x among all elements inside the vector. Clearly I can only use std::max_element if there is an iterator on the x's. But I must write one by my own, or I just make a simple for loop. maxSoFar = -std::numeric_limits< double >::max(...

Constructing associative containers

I was convinced (until I just tried it a moment ago) that it was possible to instantiate an associative container with array style notation. For example, std::set< int > _set = { 2, 3, 5 }; This isn't the case but I am wondering if there is any other way of bulk initialising a container in the constructor like this? ...

find int inside struct with find_if for std::list with structs

Hi, How can I use find_if with a std::list if the list contains structs? My first pseudo code attempt at this looks like this: typename std::list<Event>::iterator found = find_if(cal.begin(), cal.last(), predicate); The problem here is that the predicate is not directly visible in the list but inside event.object.return_number(...

Using a const key for unordered_map

I've been switching my code over from std::map to std::unordered_map where appropriate. With std::map, I typically write the following just to make sure the key cannot be modified: std::map<const std::string, int> Frankly, I never checked if this const was of any value. This has always compiled and worked with g++. Now, with std::u...

How is *it++ valid for output iterators?

In example code, I often see code such as *it++ for output iterators. The expression *it++ makes a copy of it, increments it, and then returns the copy which is finally dereferenced. As I understand it, making a copy of an output iterator invalidates the source. But then the increment of it that is performed after creating the copy would...

Advantages of using arrays instead of std::vector?

I'm currently seeing a lot of questions which are tagged C++ and are about handling arrays. There even are questions which ask about methods/features for arrays which a std::vector would provide without any magic. So I'm wondering why so much developers are chosing arrays over std::vector in C++? ...

Unnecessary locking in STL? (Visual C++ Express)

I'm trying to build a Tetris AI algorithm that can scale over multiple cores. In my tests it turns out that using multiple threads is slower than using a single thread. After some research I found that my threads spend most of their time waiting for _Lockit _Lock(_LOCK_DEBUG). Here's a screenshot. As you can see, the lock is applied o...