C++ | Implementing a Stack using STL deque and Adapter design [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Getting ready for an exam and passed upon this question that I cannot confidently answer.
Say we need to implement a Stack using STL deque and an Adapter design, which of the following is the right approach: (There can be only one answer)
1. template <typename T> class stack: public deque<T> {};
2. template <typename T> class stack {private: deque<T>* dptr; };
3. template <typename T> class stack {protected: deque<T>* dptr; };
4. template <typename T> class stack {private: deque<T> {};}
5. template <typename T> class stack: private deque<T> {};
6. template <typename T> class stack {protected: deque<T> d; };
Any tips for the solution would be appreciated. Thanks.

You never want to publicly inherit from an STL container. They're simply not designed for it because for "pay as you go" reasons they have no virtual destructor. Other forms of inheritance are simply creating unnamed members which won't serve you any purpose in this case (they can be useful for using C APIs etc). So that rules out 1 and 5. So now you can have either a private or protected member. Choose protected because that leave options open for other classes inheriting from you. So that leaves 3 and 6. Now do you want by value or a pointer. This is an STL container that can happily manage its own resources when destructed, unlike a raw pointer. So 6 is the clear winner.

Related

Is there good practices for where to declare using in C++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is there good pratices to declare a using (for typedef like usage) ?
For example if I have a class with a std::array<std::array<T, W>, H> member and I want to ease both reading and writing with a using like this
template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;
Where should I put this ? Inside the class declaration or outside or even in a separate header file ?
Thanks
Your type alias template has exactly the same purpose than a template class, i.e. define a type:
template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;
int main() {
matrix<double, 10,3> m;
return 0;
}
So the good practice would be to handle it exactly as you would do with other type definitions:
Put it in a header if you intend to reuse this definition in many places (and it seems so for your example, since a matrix is something rather general);
Embed it in a class (probably in a header) if it's an implementation detail that has not a general purpose;
Put it in the compilation unit where you use it, if you use your matrix in a single source file.
For non-template type alias, it's the same principle as with the old typedef, so exactly the same as above, and in addition,
Put it in a function body, if it has the sole purpose of serving as a very local shortcut for a very long type name.

CPP:Is generic data type T a class when defining template [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
When defining a template, the format is:
template <class T> returnType templateName{...};
My question is: will the class keyword inside the above template declaration makes data type T a class? The question is explained below:
inside <class T>, T should be a name of data type, and it's preceded by "class". When learning class in cpp, I know that a class is defined:class ClassName{...};. So my understanding is that everything followed by a class keyword would be the name of a class. In the case of the template declaration, there is also a class keyword before T. Is this means the data types in CPP are also classes?
inside <class T>, T should be a name of data type, and it's preceded by "class". Is this means the data types in CPP are also classes?
The answer is "no".
When defining a template, be it a class template or a function template, one can use typename as well as class. typename is the more accurate description but class is also supported, most likely for historical reasons.
Hence,
template <typename T> struct Foo {};
is the same as
template <class T> struct Foo {};
You can create objects by using any type as template parameter. It could be one of the fundamental types or one of the user defined types (aka classes/structs).
Give the above class template, one can use:
struct Bar {}; // User defined type
Foo<Bar> f1; // Using user defined type to create the object f1
Foo<int> f2; // Using a fundamental type to create the object f2

understanding container template parameter of `std::stack` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What exactly does the second template parameter of std::stack represent? For example, in the following code, what does std::vector<int> imply?
std::stack<int, std::vector<int>> fourth(myvector);
std::vector<int>
specifies the container used by std::stack<int> to store the values internally.
The template signature of std::stack is
template<
class T,
class Container = std::deque<T>
> class stack;
Check the explanations on the Container template parameter here please.
You can specify container classes that fulfil the requirements of a Sequence Container there. The default container type is std::deque<T>, if you omit the template parameter specification.
NOTE:
To avoid any misconceptions from your side, about the wrapping done by std::stack<> around these container instances:
std::stack<int,std::vector<int> > fourth (myvector);
Matches the explicit stack( const Container& cont ); constructor signature, which
1) Copy-constructs the underlying container c with the contents of cont. This is also the default constructor (until C++11)
Thus, the myvector and the fourth instances are independent after the construction of fourth. Manipulating either of them does not affect the other.
The std::vector<int> instance used by fourth is managed internally.
Look at the template parameters for std::stack:
template< class T, class Container = std::deque<T>> class stack;
std::stack is a container adapter and it default wraps std::deque. This code changing it so that fourth wraps an std::vector instead.

Deciphering C++ code: function in struct with same name as the struct [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to pick up a programming. I got this code from ai-junkie.com. I don't understand the last lines of each struct
SNeuron(int NumInputs),
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron).
What is the purpose of this? Does anyone care to teach basic c++?
struct SNeuron {
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
struct SNeuronLayer {
//the number of neurons in this layer
int m_NumNeurons;
//the layer of neurons
vector<SNeuron> m_vecNeurons;
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron);
};
It's a constructor. And someone has applied a pointless comment ctor to it. It allows you to instantiate an instance of SNeuron using code like
SNeuron sn(5);
This helps program stability. In C, you'd have to populate the structure fields yourself having instantiated an instance of the structure. That can leave a structure in an ill-defined state. In C++, the instance can be fully created in one step.
Remember that in C++, a struct is exactly the same as a class: with the exception that in a struct all member functions and member data are public by default (whereas in a class they are private by default).
This is a C-tor for the struct SNeuron that accepts an int.
in C++, a struct is simply a class with all members public by definition. And like any class it can have a c-tor, d-ctor and other C++ elements.

c++ multiple function on template? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
New to c++ - kind of been cramming the last few days. Things are going pretty well! I do have one question though.
If I make a template:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
My question, is the template <> line specific to that ONE function underneath? I couldn't continue to use the T placeholder in further functions could I?
like:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
T testFunc2(T t1, T t2)
{
// This one does other things....;
}
Well when you declare
template <class T>
T testFunc(T t1, T t2)
This means that you are declaring a generic function "testFunc" which takes any class and works with it. To be honest, I don't really know how it can be useful. If you are defining your function in the global namespace then you can use as many template 's above your functions as you like. They are in now way connected.
You can make a generic class that takes another class as a template. That way you don't have to write "template " above every single method in your class. But your methods CAN take the type T as an argument.