stl

How to fill std::vector with data from another vector that meets some criteria

Hi, I have a vector of points, and I need to get those which are at a distance less than a value from a given point. I could do it with a simple loop, but is there a better way to do it? Thanks in advance ...

Data access synchronization between multiple threads.

Hi, I'm trying to implement a multi threaded, recursive file search logic in Visual C++. The logic is as follows: Threads 1,2 will start at a directory location and match the files present in the directory with the search criteria. If they find a child directory, they will add it to a work Queue. Once a thread finishes with the files in...

Removing duplicates in an array while preserving the order in C++

Possible Duplicate: How to make elements of vector unique? (remove non adjacent duplicates) Is there any standard algorithm which is provided as part of STL algorithms which can remove duplicates from an array while preserving the order. For example, if I have an array like int a[] = {2,1,3,1,4,2}; after the removal of duplica...

How do I implement the equivalent of nested Perl hashes in C++?

I want to change some Perl code into C++. I need to know how to implement nested Perl hashes in C++. I thought of STL as a good choice and used maps. With the help of maps I can create only a simple hash but I do not know how to create a nested hash structure. My Perl hash is like this: %foo = ( "bar1" => { Default => 0, ...

Problem with leading zero's in a vector array of doubles

Hi, I'm tring to calculate the standard deviation of a vector of doubles (called A). Now I have a function called StDev that will do this. However, the first few elements of vector A are zero and I need to remove these. To do this I create a sub-array and then pass this to my StDev function as follows: std::vector<double> Array(f...

base class 'class std::vector<...>' has a non-virtual destructor

One of my C++ classes derives from std::vector so that it can act as a container that also perform custom actions on its content. Unfortunately, the compiler complains about the destructor not to be virtual, which I cannot change, since its in the standard library. Am I doing the whole thing wrong (thou shall not derive from STL) or is ...

Using STL in a closed-source library

Hi, Is it safe to use one standard compliant STL in a library, and another in a project that uses that library? For example: //library.h #include <string> //let's say here it uses minGW STL void Foo(std::string& str_mingw); //library.cpp void Foo(std::string& str_mingw) { /*do something*/ } //application.cpp #include "library.h...

std::inserter with set - insert to begin() or end()?

I have some code that looks like this: std::set<int> s1, s2, out; // ... s1 and s2 are populated ... std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(out, out.end())); I've read inserts can be done in amortized constant time if the value being inserted to the...

Why does istream_iterator<unsigned char, unsigned char> throw std::bad_cast?

What is going on? #include <iostream> #include <iterator> #include <sstream> int main() { std::basic_stringbuf<unsigned char> buf; std::basic_istream<unsigned char> stream(&buf); // the next line throws std::bad_cast on g++ 4.4 std::istream_iterator<unsigned char, unsigned char> it(stream); } I've tried stream.write(s...

Interleaving/deinterleaving 3 vectors in C++ STL

I'm trying to combine three signal waveforms into a single, interleaved waveform. I need to know the best way to do it in C++ STL. Better solutions would use as much C++ STL style as possible, avoid redundant code, etc. Is there some STL "tuple" type class that would do this for me? I need contiguous storage at all times for backward com...

bitset for more than 32 bits?

I need to use bit flags with more than 32 bits (33 to be exact right now). I tried and find std::bitset doesn't handle more than 32 bits (ulong). Do I have to use vector or there's a way to make bitset to work? I am limited to c++98 in this project so I can't use boost. Thanks. Edit: I'd like to do something like this: const uint64 ...

How reliable are basic_stringbuf::in_avail() and basic_stringbuf::str()?

Hi, I need to get all the contents from a stream, without actually extracting them (just like stringstream::str()). I've tried basic_stringbuf::str(), but it behaves incorrectly when the stream is empty. To avoid that case, I had a go at basic_stringbuf::in_avail(), but that hasn't worked out very well either. In the following test ca...

STL: set_union, includes, mismatch, find_if but is there no includes_any?

From the title you'd almost assuredly think use set_union to create a list and then check if it's empty. However, the objects I'm comparing are "expensive" to copy. I've looked at includes but that only works if all the items of one list are found in another. I've also looked at mismatch but rejected it for obvious reasons. I can and...

How to get unique pairs of values from a stl set

I have a stl set of integers and I would like to iterate through all unique pairs of integer values, where by uniqueness I consider val1,val2 and val2,val1 to be the same and I should only see that combination once. I have written this in python where I use the index of a list (clusters): for i in range(len(clusters) - 1): for j in...

Deadlocked when allocating an std::string

Hi, I have an application with several threads running. I have 2 threads that seem deadlocked when trying to allocate an std::string. Inspecting the backtrace of both threads suggest that at some point one has tried to allocate an std::string, and got a bad_alloc exception. In its catch block, another string is created in an attempt to ...

visual studio 2010 express STL list compiler error

I am porting some code from linux to windows and am coming up with some strange error. I have the following class: (header) RegionRectangle.h #ifndef __RECTANGLE_H__ #define __RECTANGLE_H__ #include <iostream> using namespace std; class Rectangle { public: Rectangle(int x = 0,int y = 0,int width = 0,int height = 0, int threshold=...

Flattening iterator

Is there any existing iterator implementation (perhaps in boost) which implement some sort of flattening iterator? For example: unordered_set<vector<int> > s; s.insert(vector<int>()); s.insert({1,2,3,4,5}); s.insert({6,7,8}); s.insert({9,10,11,12}); flattening_iterator<unordered_set<vector<int> >::iterator> it( ... ), end( ... ); for...

Segmentation fault in std::map::insert(...)

hello, i've used search but i didn't find answer satisfying me... so.. this is chunk of code: //VoteContainer.h typedef uint32_t order_id_t; typedef int driver_id_t; class Vote { public: enum DriverVoteResponse {YES, NO, TIMEOUT}; struct DriverResponse { driver_id_t dri...

tr1::unique_ptr and SelectObject()

Hi, I have some original code that manages exception safety like this: void foo() { HDC hdc = //get an HDC HBITMAP hbitmap = //get an HBITMAP HGDIOBJ hbitmapOld = SelectObject(hdc, hbitmap); try { //do something that may throw an exception } catch (...) { SelectObject(hdc, hbitmapOld); thro...

STL container with more than 1 sorting method in c++

Hello, i am looking for a container, to contain objects like Employee (with info: name, salary, phone ....) that will be possible to once sort it by name (a..z) and other time sort it by salary for example. what is the best way to do it ? i thought about map, but then i define only 1 key to go by would appreciate every idea (not too adva...