Array vs vector

As I understand it the array type is defined, along with the rest of the types, via a class (but I know not which one). Vectors are also defined via a different class. One of the things that makes vectors different is th exact that the vector class includes useful methods and it is stored on the heap. Why can't the array type have methods you design? Better yet why didn't someone think of that before?

Tex
you look on wrapper pattern for that.
Which array are we talking about? The std::array template or the plain array? The std::array has methods.

Standard library algorithms operate generically on range of iterators, not on specific containers. Pointer to element in plain array qualifies as an iterator.
Why can't the array type have methods you design?
Because C and backward compatibility. Builtin arrays bahave the same way like they did in C.

THere is an array class which encapsulates builtin arrays, providing many useful member functions and removing most array problems.

http://en.cppreference.com/w/cpp/container/array
Being new to this I didn't realize there was a difference between std::array and a plain array. I thought there was just one array type, like int, double, etc.

So where is the plain array defined? Is it the same place as int, double? Is in in the math class? If not where.

Where is the std::array defined?

Since these are classes can you not create another class that inherits from the array class and then add your own methods to it? Same with the vector?

tex
The std::array is a template class and its header file is <array>.

Plain array is not a class. It is part of the core language syntax.


What additional methods should a std::vector have?
So where is the plain array defined?
It is built in language. There is no definition to it.

Where is the std::array defined?
int header <array>

Since these are classes can you not create another class that inherits from the array class and then add your own methods to it? Same with the vector?
Standard container classes are not intended to be inherited from. However you can create your own Decorator class which would have array/vector member (replacing inheritance with composition) and implement additional capabilities.
I don't know. Since I'm relearning all of this it occurs to me that conceptually you could create a derived class from the vector class and modify to your specifications.

This brings up another confusing thing for me. What is the difference between a class and and template class?

tex
Class defines a type.

Template is generic boilerplate that the compiler uses to create a class definition. Compile-time polymorphism.

For example, the is no type std::vector, but your code can use types std::vector<int>, std::vector<std::pair<int,std::string>> and std::vector<Person> thanks to templates.

Another example with function templates: std::max(). The library cannot possibly implement a concrete overload of that operation for all possible types that you will write in the future, but the template mechanism can.
thetexan wrote:
template class

It's more correct, and less confusing, to say the words in the opposite order. Class template, a template for creating classes.

The same could be said about template functions vs. function templates.
Topic archived. No new replies allowed.