stl

STL list exception

Hello, I was trying to use an STL list in C++ and I arrived into a strange exception that I'm not able to understand. The list is defined as list<ASTNode*> m_stats; and ASTNode* is a class. When I try to add elements by calling ASTNode *node = new ASTNode(); m_stats.push_back(node); it throws the following exception: Program receive...

Transforming multiple iterator elements

My problem is more complex than this, so I've narrowed it down to a very simple example that would show me enough to know how to handle the rest. Say I have an input iterator. I want make a new input iterator derived from it, where each element is the combination of multiple sequential elements of the original input with the following p...

Variables as a parameters for templates in C++

Hi there! I'm trying to start learning C++ and I have a problem. I'm trying to create a function template, template<multimap<string, double> arr> void calculate(string key) { } and use it like this: multimap<string, double> arr; vector<string> keys; // ... for_each(keys.begin(), keys.end(), calculate<arr>); But i doesnt'complile: ...

C++ deque's iterator invalidated after push_front()

Hello all! Just now, I'm reading Josuttis' STL book. As far as I know -- c++ vector is a c-array that can be reallocated. So, I understand, why after push_back() all iterators and references can become invalid. But my question is about std::deque. As I know it is array of large blocks (c-array of c-arrays). So push_front() inserts elem...

C++ How to find the biggest key in a std::map?

At the moment my solution is to iterate through the map to solve this. I see there is a upper_bound method which can make this loop faster, but is there a quicker or more succinct way? ...

C++: std::string in a multi-threaded program

Given that: 1) The C++03 standard does not address the existence of threads in any way 2) The C++03 standard leaves it up to implementations to decide whether std::string should use Copy-on-Write semantics in its copy-constructor 3) Copy-on-Write semantics often lead to unpredictable behavior in a multi-threaded program I come to the...

What's the difference between std::string and std::basic_string? And why are both needed?

What's the difference between std::string and std::basic_string? And why are both needed? ...

What is the most painless approach to insert an element in the middle of the std::vector

I want to be able to insert an element in the middle (or another location) in the vector without overwriting existing element. Say my vector has 3 6 9 10 and I want to insert 7 right after 6. How should it be done without causing issues? It's very infrequent operation so efficiency is not a problem here. Also, at this point, I cannot ...

Advance iterator for the std::vector std::advance VS operator + ?

I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator it=vec.begin(); /..../ std::advance(it, location); instead of it= it + 5; what is the Preferred/Recommended way ? ...

STL or Qt containers?

What are the pros and cons of using Qt containers (QMap, QVector, etc.) over their STL equivalent? I can see one reason to prefer Qt: Qt containers can be passed along to other parts of Qt. For example, they can be used to populate a QVariant and then a QSettings (with some limitation though, only QList and QMap/QHash whose keys are s...

Adding elements to an STL Map in a constructors Initialization List?

I was wondering if this was possible, and if so how I'd go about doing so. If it's not possible I'll just have to add the elements during the constructor body. Ideally I would like the map immutable after construction. What I'm trying to achieve is adding two pairs to the map that are created from constructor parameters. ...

intersect two maps using only keys as a comparator

Hi all, I have two maps and I would like to get map which is intersection of two using only key as a comparator and in the same time do simple math operation on values of common elements such as +/- for example : map<int, double> m1,m2; m1[1] = 1.1; m1[2] = 2.2 m2[2] = 0.1; m2[4] = 3.3; after intersection i will get m3 which will h...

sort using boost::bind

bool pred(int k, int l, int num1, int num2) { return (num1 < num2); } int main() { vector <int> nums; for (int i=50; i > 0; --i) { nums.push_back(i); } std::sort (nums.begin(), nums.end(), boost::bind(&pred, 5, 45)); } I am a boost newbie. I was learning to use boost::bind and I wanted to sort a vector of intege...

Crash in msvcp90d.dll when retrieving an iterator from a boost::tokenizer

When I retrieve the begin() iterator of a boost::tokenizer, I get a crash in msvcp90d.dll, that says "ITERATOR LIST CORRUPTED", which looks suspiciously like issues I have run into before with the _HAS_ITERATOR_DEBUGGING compiler flag, however I have verified that my program is being compiled with this flag turned off. Here is the prog...

std::map initialization crashing on iPhone device, but not in simulator.

I've tried turning on the Call C++ Default Ctors/Dtors in Objective-C flag but I'm still getting an EXC_BAD_ACCESS error when I first try to access my map: (*[TextureBatcher getSharedTextureBatcher].getMap)[texID].vertexCount=0; (*[TextureBatcher getSharedTextureBatcher].getMap)[texID].indexCount=0; getMap just returns a reference to ...

Erase-remove idiom: what happens when remove return past-the-end-iterator?

I got this question when I was reading erase-remove idiom (item 32) from Scott Meyers "Effective STL” book. vector<int> v; ... v.erase(remove(v.begin(), v.end(), 99), v.end()); remove basically returns the "new logical end” and elements of the original range that start at the "new logical end" of the range and continue until the real...

C++ STL for_each should take pointer to member function taking one argument

Hi I have to pass the address of a member fn taking one argument to the std::for_each. how do i do this? class A{ void load() { vector<int> vt(10,20); std::for_each(vt.begin(), vt.end(), &A::print); //It didnt work when i tried mem_fun1(&A::print) } void print(int a) { cout<<a; } }; Thanks ...

C++ array with value semantics and no allocator shenanigans?

I'm looking for a C++ container that's a cross between boost::array, boost::scoped_array and std::vector. I want an array that's dynamically allocated via new[] (no custom allocators), contained in a type that has a meaningful copy-constructor. boost::array is fixed-size, and although I don't need to resize anything, I don't know the s...

c++: what exactly does &rand do?

This is an excerpt of some c++ code, that i'll have to explain in detail in some days: std::vector<int> vct(8, 5); std::generate(vct.begin(), vct.end(), &rand); std::copy(vct.rbegin(), vct.rend(), std::ostream_iterator<int>(std::cout, "\n")); i think i understand everything about it, except that tiny mystical &rand. what exactly...

STL Sorting with Abstract Classes

I'm having a problem sorting my derived classes with the STL sort function. Example - The header: vector<AbstractBaseClass *> *myVector; In the ImpL: sort(myVector->begin(), myVector->end(), compareBy); The comparator: bool MyClass::compareBy(AbstractBaseClass& a, AbstractBaseClass& b) { return (a->someMethod() < b->someMeth...