When vectors are allocated, do they use memory on the heap or the stack?

vector<Type> vect; will allocate the vector, i.e. the header info, on the stack, but the elements on the free store (“heap”). vector<Type> *vect = new vector<Type>; allocates everything on the free store. vector<Type*> vect; will allocate the vector on the stack and a bunch of pointers on the free store, but where these point is … Read more

In STL maps, is it better to use map::insert than []?

When you write map[key] = value; there’s no way to tell if you replaced the value for key, or if you created a new key with value. map::insert() will only create: using std::cout; using std::endl; typedef std::map<int, std::string> MyMap; MyMap map; // … std::pair<MyMap::iterator, bool> res = map.insert(MyMap::value_type(key,value)); if ( ! res.second ) { cout … Read more

Is std::vector copying the objects with a push_back?

Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector. If you want to store pointers to objects in your vector, create a std::vector<whatever*> instead of std::vector<whatever>. However, you need to make sure that the objects referenced by the pointers remain valid while the vector holds a reference to them (smart … Read more

Why are Standard iterator ranges [begin, end) instead of [begin, end]?

The best argument easily is the one made by Dijkstra himself: You want the size of the range to be a simple difference end − begin; including the lower bound is more “natural” when sequences degenerate to empty ones, and also because the alternative (excluding the lower bound) would require the existence of a “one-before-the-beginning” sentinel value. … Read more

Why is std::map implemented as a red-black tree?

Probably the two most common self balancing tree algorithms are Red-Black trees and AVL trees. To balance the tree after an insertion/update both algorithms use the notion of rotations where the nodes of the tree are rotated to perform the re-balancing. While in both algorithms the insert/delete operations are O(log n), in the case of … Read more

Why is it wrong to use std::auto_ptr with standard containers?

The C++ Standard says that an STL element must be “copy-constructible” and “assignable.” In other words, an element must be able to be assigned or copied and the two elements are logically independent. std::auto_ptr does not fulfill this requirement. Take for example this code: class X { }; std::vector<std::auto_ptr<X> > vecX; vecX.push_back(new X); std::auto_ptr<X> pX … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)