Integer RAII index container for non-copy type

Is there a standard container that has the same common API as it does vector<T>, but which fills new locations with a direct default construction?


Background:

I have a type that forbids copying, but has a default constructor, and I really want to do this:

vector<NoCopy> bag(some_size);

// use bag[i]'s

return; // bag & contents get correctly cleaned up. 

However, this does not work, because it vector<T>(int)is implemented in terms of constructing an object by default, and then copies it to each new location.


Edit: Not C ++ 0xB (aka C ++ 11)

+4
source share
3 answers

One option is to upgrade to the standard implementation of the C ++ 11 standard library.

++ 11 vector(size_type) N . - .

Visual ++ 2010 ++ 11; , Visual ++ 11 Developer Preview . , libstd++; , lib++ .

+5

Boost.Container, Boost 1.48, boost::container::vector, . ++ 03, ++ 11.

, ++ 03, boost::container::vector : vector<bool> vector of bool s. , .

+2

Call it a workaround, but when I need NoCopy type containers, I usually use boost :: ptr_vector or std :: vector <shared_ptr>.

Obviously, this is a little expensive, but, fortunately, for me it is not a problem for me.

The good thing about boost :: ptr_vector is that it does auto dereferencing on some accesses. Check documents .

+2
source

All Articles