stl

Compelling examples of custom C++ STL allocators?

What are some really good reasons to ditch the standard STL allocators for a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom allocators have always been a feature of the STL that I haven't had much need for. I was jus...

How do I store arrays in an STL list?

Using C++ and the STL, does anybody know how to store integer arrays as nodes in an STL list or vector? I have an unknown number of pairs of numbers that I need to store, and coming from other languages my first thought was to use some sort of list- or vector-like data structure... but I'm running into some trouble. I am 100% sure that I...

What is the STL?

I'm not a C++ programmer and have difficulty understanding the explanations given on websites. I don't understand containers or iterators and don't have plans to learn C++ in the near future. So in layman's terms: What is the STL and what can it do for me? How does it compare to something like the Python Standard library or glibc? ...

How to save a file to disk in C++?

I am making an application that simulates an ATM (its totally trivial). I was having a little trouble saving my transactions to the hard disk. The two main questions are: A) Should I save it as a DB or a text-file, B) How would I save to disk using either DB or txt format in STL C++ (I don't really want to use a third-party library but I...

how do I use STL algorithms with a vector of pointers

I have a vector of pointers that are not owned by the container. How do I use algorithms on the targets of the pointers. I tried to use boost's ptr_vector, but it tries to delete the pointers when it goes out of scope. Here is some code that needs to work: vector<int*> myValues; // ... myValues is populated bool consistent = count(my...

Best way to empty stringstream?

One of the possibilities is: somestringstream.str(""); But is it most optimal? Is there any way to preserve stringstream internal buffer, so that following operator<<() calls would not require to reserve memory again? ...

Using Hash Maps to represent an extremely large data source

I have a very large possible data set that I am trying to visualize at once. The set itself consists of hundreds of thousands of segments, each of which is mapped to an id. I have received a second data source that gives more real-time information for each segment, but the id's do not correspond to the id's I have. I have a 1:1 mappin...

Safe to store list::iterator for later use?

Suppose I have a list, in which no new nodes are added or deleted. However, the nodes may be shuffled around. Is it safe to save an iterator, pointing to a node in the list, and access it at some arbitrarily later time? Edit (followup question): The documentation for list::splice() says that it removes elements from the argument list....

C++ STL's String eqivalent for Binary Data

I am writing a C++ application and I was wondering what the C++ conventional way of storing a byte array in memory. Is there something like a string, except specifically made for binary data. Right now I am using a *unsigned char** array to store the data, but something more STL/C++ like would be better. ...

Reorder vector using a vector of indices

I'd like to reorder the items in a vector, using another vector to specify the order: char A[] = { 'a', 'b', 'c' }; size_t ORDER[] = { 1, 0, 2 }; vector<char> vA(A, A + sizeof(A) / sizeof(*A)); vector<size_t> vOrder(ORDER, ORDER + sizeof(ORDER) / sizeof(*ORDER)); reorder_naive(vA, vOrder); // A is now { 'b', 'a', 'c' } The fo...

Aggregating contributions from multiple donors

As I try to modernize my C++ skills, I keep encountering this situation where "the STL way" isn't obvious to me. I have an object that wants to gather contributions from multiple sources into a container (typically a std::vector). Each source is an object, and each of those objects provides a method get_contributions() that returns any...

Custom Iterator in C++

I have a class TContainer that is an aggregate of several stl collections pointers to TItems class. I need to create an Iterator to traverse the elements in all the collections in my TContainer class abstracting the client of the inner workings. What would be a good way to do this?. Should I crate a class that extends an iterator (if ...

how do I dynamically cast between vectors of pointers?

I have: class T {}; class S: public T {}; vector<T*> v; vector<S*> w; transform(v.begin(), v.end(), dynamic_cast_iterator<S*>(w.begin())); But, of course, dynamic_cast_iterator doesn't exist. ...

Failing to use stl containers in templated functions/classes

When I try and compile the following code... #include <vector> template <class T> void DoNothing() { std::vector<T>::iterator it; } int main(int argc, char**argv) { return 0; } g++ says: test.cpp:5: error: expected `;' before ‘it’ And I don't understand why this is a problem. If I replace it with std::vector<int>::it...

Which one to use c++ stl container or the MFC container?

For every stl container there is a MFC container available in visual c++.Which is better than the other one in what sense and what do you use? I always use STL container is that wrong? ...

How is STL iterator equality established?

I was wondering, how is equality (==) established for STL iterators? Is it a simple pointer comparison (and thus based on addresses) or something more fancy? If I have two iterators from two different list objects and I compare them, will the result always be false? What about if I compare a valid value with one that's out of range? I...

How do you determine the last valid element in a STL-Container

If i iterate over a STL container i sometimes need to know if the current item is the last one in the sequence. Is there a better way, then doing something like this? Can i somehow convert rbegin()? std::vector<int> myList; // .... std::vector<int>::iterator lastit = myList.end(); lastit--; for(std::vector<int>::iterator it = myList...

Mixing Qt with STL and Boost - are there any bridges to make it easy?

Are there any bridges to make mixing Qt with STL and Boost as seamless and easy as possible? This is a followup to Mixing Qt and Boost, where no specific answers how to accomplish this were given. ...

convert std::string to const BYTE* for RegSetValueEx()

I have a function that gets a std::string. That function calls RegSetValueEx the 5th parameter is the value of the registry value and expects a variable of type const BYTE*. So I have to convert the std::string to const BYTE* and also give the length of the resulting array as the 6th parameter. I have found a way to do it, but it feel...

c++ char array out of scope or not?

I have a method that requires a const char pointer as input (not null terminated). This is a requirement of a library (TinyXML) I'm using in my project. I get the input for this method from a string.c_str() method call. Does this char pointer need to be deleted? The string goes out of scope immediately after the call completes; so th...