Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

You have 10 seconds to implement std::vector in plain C

Name: Anonymous 2016-07-20 3:56

Must support:

* Adding elements
* Regrowing

Name: Anonymous 2016-07-20 17:54

>>4
It's implemented in C++ (it pretty much has to be, because it depends on templates and operator overloading). You could get similar functionality in plain C, but you'd have to use a different syntax.

For example, there's really no way to use something like std::vector<int> vec; in plain C, since C doesn't support namespaces or templates. You could implement a struct and appropriate functions to create an automatically resizing array-like data structure, which is really what I think >>1 is asking for. Making it type-generic like std::vector without resorting to making a new version of the struct and associated functions for each data type would be tricky and probably require messing around with pointers and casting, though that would actually be more elegant than what C++ templates actually do (they basically are macros that create multiple type-specific versions of functions and classes at compile time).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List