stl

What is the lifetime and validity of C++ iterators ?

I'm planning to implement a list of Things in C++ where elements might be removed out of order. I don't expect that i'll need any kind of random access (i just need to sweep the list periodically), and the order of items isn't important either. So I thought of std::list<Thing*> with this->position = insert(lst.end(), thing) should do th...

Breaking in std::for_each loop.

While using std::for_each algorithm how do I break when a certain condition is satisfied? ...

What is the naming convention when typdef complex STL maps?

1) What is the convention used in practice when typedef'ing something like typedef std::map<SomeClass*, SomeOtherClass> [SomeStandardName>] typedef std::map<SomeClass*, std<SomeOtherClass> > <[omeStandardName] 2) Where do you usually put typedef: header files globally, local to the class? 3) Do you typedef iterators or const map<...

How to remove constness of const_iterator?

As an extension to this question Are const_iterators faster?, I have another question on const_iterators. How to remove constness of a const_iterator? Though iterators are generalised form of pointers but still const_iterator and iterators are two different things. Hence, I believe, I also cannot use const_cast<> to covert from const_i...

Should I prefer iterators over const_iterators?

Someone here recently brought up the article from Scott Meyers that says: Prefer iterators over const_iterators (pdf link). Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are? Here is mine: One of the main points of the article is that you cannot erase or insert on a const_iterat...

using std::find_if with std::string

I'm being stupid here but I can't get the function signature for the predicate going to find_if when iterating over a string: bool func( char ); std::string str; std::find_if( str.begin(), str.end(), func ) ) In this instance google has not been my friend :( Is anyone here? ...

stl algorithm in class

Is there a way to use stl algorithms like find() and find_if() in a container of objects? Ex.:With find() find the element whit name "abc" in a vector of class Alfhabetic. ...

Choosing a STL container

I am unable to decide which STL container to use in the following case: (1). I want to preserve the order of insertion of the elements (2). The elements in the container have to be unique. Is there any readymade container available for this? I don't want to use a vector and then perform a std::find before doing a push_back every time...

Problem linking when using stl in VS2005

I just added STL usage to some code, and I'm getting this link error: error LNK2019: unresolved external symbol "public: __thiscall std::_Lockit::~_Lockit(void)" I must be missing something in the link, I've done this before - and googling has not helped so far. hmm...... Here's the code snippet: #pragma once #include "Observer.h" #i...

How to use BOOST_FOREACH with two std::maps?

I have code that looks essentially like this: std::map<int, int> map1, map2; BOOST_FOREACH(int i, map1) { // do steps 1-5 here... } BOOST_FOREACH(int i, map2) { // do steps 1-5 (identical to above) here... } Is there any way to concatenate the maps to eliminate the duplicate code in the second loop? Or a way to extend BOOST_FO...

Copy map values to vector in STL

Working my way through Effective STL at the moment. Item 5 suggests that it's usually preferable to use range member functions to their single element counterparts. I currently wish to copy all the values in a map (i.e. - I don't need the keys) to a vector. What is the cleanest way to do this? ...

How to inherit from std::ostream?

I've been googling around and I just can't find a simple answer to this. And it should be simple, as the STL generally is. I want to define MyOStream which inherits publicly from std::ostream. Let's say I want to call foo() each time something is written into my stream. class MyOStream : public ostream { public: ... private: void ...

Structure to hold value by ranged key

I need a structure to hold a value based on a key that has a range. My implementation is C++, so any STL or Boost would be excellent. I have as range-key, which are doubles, and value [0,2) -> value1 [2,5) -> value2 [5,10) -> value3 etc Such that a search of 1.23 should return value1, and so on. Right now I am using a vector contai...

MS Extensions To STL

Hi all, If this is a "Google Is Your Friend" question, I apologize in advance. I've searched but perhaps I've been using the wrong terms for searching. Can anyone point me to any sort of introductory document on the STL extensions which MS provided with Visual Studio 2003? I believe the libraries were licensed from Dinkumware and I'v...

How to create an iterator over elements that match a derived type in C++?

I'd like an iterator in C++ that can only iterate over elements of a specific type. In the following example, I want to iterate only on elements that are SubType instances. vector<Type*> the_vector; the_vector.push_back(new Type(1)); the_vector.push_back(new SubType(2)); //SubType derives from Type the_vector.push_back(new Type(3)); t...

Function for manipulating container of Base/Derived objects

consider the following algorithm with arrays: class MyType; { // some stuff } class MySubType:MyType { // some stuff } void foo(MyType** arr, int len) { for (int i = 0;i<len;i++) // do something on arr[i]-> } void bar() { MySubType* arr[10]; // initialize all MySubType*'s in arr foo(&arr, 10); } Noth...

In C++ how can I use a template function as the 3rd parameter in std::for_each?

I am trying to use std::for_each to output the contents of vectors, which may contain different types. So I wrote a generic output function like so: template<typename T> void output(const T& val) { cout << val << endl; } which I would like to use with: std::for_each(vec_out.begin(), vec_out.end(), output); but the compiler comp...

What's faster, iterating an STL vector with vector::iterator or with at()?

In terms of performance, what would work faster? Is there a difference? Is it platform dependent? //1. Using vector<string>::iterator: vector<string> vs = GetVector(); for(vector<string>::iterator it = vs.begin(); it != vs.end(); ++it) { *it = "Am I faster?"; } //2. Using size_t index: for(size_t i = 0; i < vs.size(); ++i) { //...

Performance: vector of classes or a class containing vectors

I have a class containing a number of double values. This is stored in a vector where the indices for the classes are important (they are referenced from elsewhere). The class looks something like this: Vector of classes class A { double count; double val; double sumA; double sumB; vector<double> sumVectorC; vector<double>...

Why this sample works?

> typedef pair<double, double> dd; const double epsilon = 1e-6; struct sort_by_polar_angle { dd center; // Constuctor of any type // Just find and store the center template<typename T> sort_by_polar_angle(T b, T e) { int count = 0; center = dd(0,0); while(b != e) { ...