stl

stl vector and c++: how to .resize without a default constructor?

how could I tell STL, specifically for the method resize() in vector, to initialize objects with a constructor other than default, and with which parameters? I mean: class something { int a; something (int value); } std::vector<something> many_things; many_things.resize (20); more generally, how could I force STL to use my ...

Why is type_info declared outside namespace std?

Hi, I'm using VS2005 and the MS implementation of STL. However, the class type_info in is declared outside of "namespace std". This creates some problems for third party libs that excepts to find a std::type_info. Why is this so, and is there any workaround? Here is a sample from the beginning of typeinfo: class type_info { ... }; _S...

What questions should an expert in STL be expected to answer, in an interview

I was looking at a job posting recently and one of the requirements was that a person be a 9/10 in their knowledge of STL. When I judge my skills, to me a 10 is someone that writes advanced books on the subject, such as Jon Skeet (C#), John Resig (JavaScript) or Martin Odersky (Scala). So, a 9/10 is basically a 10, so I am not certain ...

How do STL containers copy objects?

I know STL containers like vector copies the object when it is added. push_back method looks like: void push_back ( const T& x ); I am surprised to see that it takes the item as reference. I wrote a sample program to see how it works. struct Foo { Foo() { std::cout << "Inside Foo constructor" << std::endl; } Foo...

Fastest way to write large STL vector to file using STL

I have a large vector (10^9 elements) of chars, and I was wondering what is the fastest way to write such vector to a file. So far I've been using next code: vector<char> vs; // ... Fill vector with data ofstream outfile("nanocube.txt", ios::out | ios::binary); ostream_iterator<char> oi(outfile, '\0'); copy(vs.begin(), vs.end(), oi); ...

Hash/key creation function for latitude longitude?

Hello, I have blocks of data associated with latitude/longitude values. I'd like to create a lookup key/hash value from the latitude/longitude value so it can be used as a lookup into a map or something similar. I'm using negative values for West and South... therefore 5W, 10S is represented as -5, -10 in the program. I'd like to...

STL: How to check that an element is in a std::set ?

How do you check that an element is in a set? Is there a simpler equivalent of the following code: myset.find(x) != myset.end() ...

check type of element in stl container - c++

how can i get the type of the elements that are held by a STL container? ...

When should you use an STL other than the one that comes with your compiler?

I was curious about STL implementations outside of what's packaged with gcc or Visual Studio, so a quick Google search turned up a few results, such as: Apache stdcxx uSTL rdeSTL Under what circumstances should one use an alternative standard template library? For instance, Apache's page has a list including items such as "full conf...

How stl vector gives random access

Hello Gurus, Yesterday evening I was using std::vector for my work, and this question popped into my mind: how does vector gives random access? I tried to look into code but was unsuccessful. Can anyone give some pointers? Thanks, Arun ...

Which sorting algorithm is used by STL's list::sort()?

I have a list of random integers. I'm wondering which algorithm is used by the list::sort() method. E.g. in the following code: list<int> mylist; // ..insert a million values mylist.sort(); EDIT: See also this more specific question. ...

Which sorting algorithm is used by Microsoft's STL::list::sort()?

Note: I accidentally posted this question without specifying which STL implementation I was using, and I felt it can't really be updated since it would render most of its answers obsolete. So, the correct question goes - which sorting algorithm is used in the below code, assuming I'm using the STL library of Microsoft Visual C++?: list...

What is the right approach when using STL container for median calculation?

Let's say I need to retrieve the median from a sequence of 1000000 random numeric values. If using anything but STL::list, I have no (built-in) way to sort sequence for median calculation. If using STL::list, I can't randomly access values to retrieve middle (median) of sorted sequence. Is it better to implement sorting myself and go...

C++ STL unordered_map problems and doubts

Hello, after some years in Java and C# now I'm back to C++. Of course my programming style is influenced by those languages and I tend to feel the need of a special component that I used massively: the HASH MAP. In STL there is the hash_map, that GCC says it's deprecated and I should use unordered_map. So I turned to it. I confess I'm n...

C++ STL: Custom sorting one vector based on contents of another

This is probably best stated as an example. I have two vectors/lists: People = {Anne, Bob, Charlie, Douglas} Ages = {23, 28, 25, 21} I want to sort the People based on their ages using something like sort(People.begin(), People.end(), CustomComparator), but I don't know how to write the CustomComparator to look at Ages rather than...

Which allocator are available in STLPORT, and how to use them

We're using STLPORT and we'd like to change stlport's default allocator: instead of vector<int>, we'd like to try vector<int, otherallocator> Which alternative allocator are available in stlport, and what are their features? How do I use them? ...

Insert into an STL queue using std::copy

Hi, I'd like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) ); But this fails to compile, complaining that 'begin' is not a member of 'std::queue'. Note: I tried it with std::inserter...

Why doesn't my custom iterator work with the STL copy?

I wrote an OutputIterator for an answer to another question. Here it is: #include <queue> using namespace std; template< typename T, typename U > class queue_inserter { queue<T, U> &qu; public: queue_inserter(queue<T,U> &q) : qu(q) { } queue_inserter<T,U> operator ++ (int) { return *this; } queue_inserter<T,U> operat...

Where do I get sample code in C++ creating iterator for my own container?

I have been searching for sample code creating iterator for my own container, but I haven't really found a good example. I know this been asked before (http://stackoverflow.com/questions/148540/c-creating-my-own-iterators) but didn't see any satisfactory answer with examples. I am looking for simple sample code to start how to design ...

How can i find a value in a map using binders only

Searching in the second value of a map i use somthing like the following: typedef std::map<int, int> CMyList; static CMyList myList; template<class t> struct second_equal { typename typedef t::mapped_type mapped_type; typename typedef t::value_type value_type; second_equal(mapped_type f) : v(f) {}; bool operator()(co...