stl

Problem using templated predicate in STL algorithms

I have the following piece of code, that gives an error on the first usage form of pred2. I was hoping if someone could explain why this particular usage is incorrect, as I think pred3 usage is similar. #include <algorithm> bool pred1(const int&) { return true; } template<typename T> bool pred2(const T&) { return true; } struct pred3...

Caching the end iterator - Good idea or Bad Idea?

Generally speaking is it a good idea to cache an end iterator (specifically STL containers) for efficiency and speed purposes? such as in the following bit of code: std::vector<int> vint; const std::vector<int>::const_iterator end = vint.end(); std::vector<int>::iterator it = vint.begin(); while (it != end) { .... ++it; } Under...

Does STL and vector provide sort option?

Hi, Does STL and Vector provide the default sorting option?. ...

How to achieve better efficiency re-inserting into sets in C++

I need to modify an object that has already been inserted into a set. This isn't trivial because the iterator in the pair returned from an insertion of a single object is a const iterator and does not allow modifications. So, my plan was that if an insert failed I could copy that object into a temporary variable, erase it from the set, m...

Is there a C++ equivalent to Java's Collection interface for STL container classes?

I would like to pass arbitrary container as an argument of function and iterate over it (no erasing nor pushing elements). Unfortunately it looks like there is no standard way of doing this. First solution which comes to my mind is an interface (let's call it CollectionInterface) implemented by classes that will wrap STL containers. so...

C++ iterator in for loop pitfalls?

I see somewhere it metions: for ( itr = files.begin(); itr < files.end(); ++itr ) // WRONG for ( itr = files.begin(); itr != files.end(); ++itr ) // ok Why is the first expression wrong? I always used the first expression, and didn't have any problems. ...

looking for a MemoryStream in C++

In the wonderful world of C# i can create a memory stream without specifying its size, write into it and then just take the underlying buffer. How can i do the same in c++? basicly i need to do: memory_stream ms(GROW_AS_MUCH_AS_YOU_LIKE); ms << someLargeObjects << someSmallObjects << someObjectsWhosSizeIDontKnow; unsigned char* buff...

Is it safe to pass a vector as an array?

Say I have a function: void someFunc(int *x,int count); which is out of my control, so I can't write it to accept iterators. Is it safe to call it like so (regardless of the specific STL implementation): vector<int> v; /* ... */ someFunc(&v[0],v.size()); Obviously, one counter example is vector<bool>. How about any other type? (as...

How can I return a copy of a vector containing elements not in a set?

Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items vector contains all known items and the bad_items vector contains a list of bad items. These two data structures are populated entirely independent of one another. What's the proper way to write a method that will retu...

iterator_traits: code from Effective C++, Item 47 and TCPPSL, Section 7.5.1

This is going to be rather long, so I apologize preemptively. Near the end of Item 47 in Scott Meyers's "Effective C++" (Third Edition) he shows the following code: template<typename IterT, typename DistT> void advance(IterT& iter, DistT d) { doAdvance( iter, d, typename std::iterator_traits<IterT>::...

Usage of this pointer

I have a question relating to the usage of "this". Suppose I have two classes A & B; their rough outline is as follows: class A { public: ... void AddB( B* b ) { // inserts B into the vector v } private: std::vector<B*> v; }; class B { public: ... void foo( void ) { ... // Adds itself to th...

c++ vector as a parameter Help

I wrote a function to take in a vector, a int position and a int value void add(vector<int>& pro, int pos, int val){ pro[pos] += val; while(pro[pos] > 9){ int carry = pro[pos]/10; pro[pos] %= 10; pos++; pro[pos] += carry; }//while }//function add lets say i have vector<int> list1,list2,pro...

Multimap containing pairs?

Is it possible for a multimap to contain within it pairs? IE, rather then being defined as multimap<char,int> for instance, it would be defined as multimap<pair, pair>? How would this multimap then be sorted? Also, how would one access the individual contents of each pair? ...

Why C++ STL does not provide hashtable and union data structures?

At various places, I've read that STL does not provide hashtable and union data structures. How could these be implemented using other existing STL data structures? ...

Using a class type as map key_value

Could a variable of Data be used as a map key? struct Data { Data(int X, int Y) {x=X; y=Y;} int x; int y; } int main() { std::map<Data, int> map_; map_.insert(std::make_pair(Data(1,2), 0)); //error inserting } ...

change value inside multimap

I have a multimap<key1,pair<key2, value2>> I want to change value2 in this multimap. typedef std::pair<int, int> comp_buf_pair; //pair<comp_t, dij> typedef std::pair<int, comp_buf_pair> node_buf_pair; typedef std::multimap<int, comp_buf_pair> buf_map; //key=PE, value = pair<comp_t, dij> typedef buf_map::iterator It_buf; buf_map b...

OMPTL in Visual Studio?

I was trying to use the OMPTL in Visual Studio. As far as I understand it, I only need to set the /openmp option, in order for the OMPTL to use the multi-threaded implementation of some stl functions. When I don't use /openmp everything is fine and OMPTL maps the functions to their normal stl counter parts, without multi-threading. With...

C++ case declaration?

I am trying to adapt a digital electronics problem to a C++ STL based program. Originally I have 4 inputs C1, C2, C3, C4. This means I have a total of 16 combinations: 0000 0001 . . . 1111 I have a multimap defined by typedef std::pair<int, int> au_pair; //vertices typedef std::pair<int, int> acq_pair; //ch qlty typedef std::multim...

multimap accumulate values

I have a multimap defined by typedef std::pair<int, int> comp_buf_pair; //pair<comp_t, dij> typedef std::pair<int, comp_buf_pair> node_buf_pair; typedef std::multimap<int, comp_buf_pair> buf_map; //key=PE, value = pair<comp_t, dij> typedef buf_map::iterator It_buf; int summ (int x, int y) {return x+y;} int total_buf_size = 0; std::c...

Deleting user-defined vectors - C++

Hi, I have 2 classes, say A & B. Class B has a destructor of its own. Within class A, I have a vector of pointers to objects of class B. The vector is as follows: vector<B*> vect; In the destructor for class A, how do I retrieve memory? If I cycle through the vector, do I retrieve each object and use delete on every retrieved object?...