|
|
My original template contains a push_back() and pop_back() function, which should of course be part of the specialized template. The question here is what would make sense? -> strcpy() every time a new item is added? Or something else? |
There is also the overloaded [] which I am not too sure how to write, here a little help would be appreciated. (Or generally what to do with it) |
[Misenna] My original template contains a push_back() and pop_back() function, which should of course be part of the specialized template. The question here is what would make sense? -> strcpy() every time a new item is added? Or something else? [Cubbi] Yes. I'd use std::copy or memcpy as an improvement, since the length of the string is already known and there's no need to waste time with strcpy (applies to the constructor too). |
[Cubbi]speaking of which.. here's how I'd write the core part of that SimpleVector in today's C++ |
|
|
|
char
and the last of them is '\0'
keskiverto Would you explain line 130? |
keskiverto The std::vector has size and capacity. Internally, the vector has an array that can hold capacity elements. Externally, the vector holds size elements. Every push and pop do change the size, but the capacity changes only when necessary. Your SimpleVector creates a new array every time you do push. Every time. However, pop does not change the internal array. |
|
|
|
|
|
|
keskiverto1. Why do you calculate newCapacity outside of the if clause? It is used only inside. |
keskiverto 2. Why line 22? You do it on line 28. |
keskiverto 3. http://www.cplusplus.com/reference/memory/unique_ptr/operator[]/ but how about: http://www.cplusplus.com/reference/algorithm/copy_n/
|
|
|
There is a slight difference to std::vector. If I do:
Q: How many initialized Foo objects do I have in the bar? There is no practical difference to SimpleVector with basic types, but if Foo('s default constructor) is mind-bogglingly complex and heavy ... |
Q: How many initialized Foo objects do I have in the bar? |
new
" differs from new
and new[]
.Misenna wrote: |
---|
While VS does not complain about using std::copy_n() the wandbox and cppshell websites both tell that: std::copy_n is not part of std namespace ... |
Misenna wrote: |
---|
I considered only the size, but never thought about capacity. |
keskiverto I don't suggest making SimpleVector into full std::vector. I just point out that there is much under the hood. |
Cubbi Misenna wrote: While VS does not complain about using std::copy_n() the wandbox and cppshell websites both tell that: std::copy_n is not part of std namespace ... did you forget to #include <algorithm>? https://wandbox.org/permlink/AT1IEEAcSDK2rgpw |
Cubbi: Misenna wrote: I considered only the size, but never thought about capacity. it seemed like it was intentional. Once you add capacity, your SimpleVector is not going to be so simple anymore (as keskiverto above points out too). In fact, classes that manage a dynamic array with just size are fairly common in real code, although such classes don't usually offer push_back/pop_back/resize. |
|
|
Is this now as it should be? |
Cubbi real std::vector doesn't shrink capacity in pop_back, and, more significantly, always calls the destructor of the last T Though making an auto-shrinking vector is perhaps a justifiable reason for writing your own. |
|
|
|
|
template <> class SimpleVector<char *>
, then go with it.
|
|
keskiverto If the book says template <> class SimpleVector<char *>, then go with it. Does the book say whether this specialization should represent one C-string or an array of C-strings? |
Main Section 16.4: "For example, the declaration of a specialized version of the SimpleVector class might start like this: class SimpleVector<char *> The compiler would know that this version of the SimpleVector class is intended for the char * data type. Anytime an object is defined of the type SimpleVector<char *>, the compiler will use this template to generate the code. |
"In this chapter, the section Specialized Templates within Section 16.4 describes how to design templates that are specialized for one particular data type. The section introduces a method for specializing a version of the SimpleVector class template so it will work with strings. Complete the specialization for both the SimpleVector and SearchableVector templates. Demonstrate them with a simple driver program." |
keskiverto Ultimately, the question is what the book/you want to learn with this exercise. |
|
|
|
|
a version of the SimpleVector so it will work with strings. |
keskiverto: The full specialization is again very much like writing a concrete Bar. Just a tiny bit of extra <> in syntax. There will be duplication, because you want to keep the specialization similar in spirit to the un-specialized template. |
|
|
|
|