C++ auto-expanding vector class template

Discussion of chess software programming and technical issues.

Moderator: Ras

matthewlai
Posts: 793
Joined: Sun Aug 03, 2014 4:48 am
Location: London, UK

Re: C++ auto-expanding vector class template

Post by matthewlai »

sje wrote:First, instances are NOT the same size:
I stand corrected. std::vector does also keep a separate allocation size, for better performance. Probably a good idea to do it in yours as well, so that you can implement a "size()" function, which is pretty important for a vector class, and not very friendly if it doesn't reflect the number of elements actually in the vector.
Not very friendly, is it?
You are doing something vectors are not designed to do - accessing it past the end.

If you do something your Vector class is not designed to do, it won't be very friendly either.

Code: Select all

Vector<int> v;
v[-1] = 0;
It's also missing copy constructor and assignment operator, so if you try to copy your vector, it won't be very friendly either.

Code: Select all

Vector<int> v1;
Vector<int> v2;

v1[5] = 3;

v2 = v1;

v2[5] = 9;

std::cout << v1[5] << std::endl; // HUH?
It's also much slower than vector's allocation, doing a bunch of unnecessarily operations most of the time, but I'm sure you know that already.

Also, what if T is not copyable? For example, if it's a file stream or thread handle? Though to be fair, that can't be solved before C++11.
Disclosure: I work for DeepMind on the AlphaZero project, but everything I say here is personal opinion and does not reflect the views of DeepMind / Alphabet.
jdart
Posts: 4427
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: C++ auto-expanding vector class template

Post by jdart »

Yeah, I actually also have my own templatized vector class (https://github.com/jdart1/arasan-chess/ ... /arasvec.h), as well as a set class, but that is for historical reasons. 20+ years ago, C++ compilers were a lot buggier and STL was not implemented well by some vendors. Now I think there really is no reason not to use STL vector.

--Jon