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

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.

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.

C++ | Implementing a Stack using STL deque and Adapter design [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 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.

What is `using`, and what is the colon after the constructor in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Could you help me to understand the code below? I have an interface and need to write a code. But it first time when I work with OOP. I would be grateful if you answer some questions about this code.
template <class T, class Compare = std::less<T>>
class List_of_objects {
public:
using IndexChange =
std::function<void(const T& element, size_t new_element_index)>;
explicit Heap(
Compare compare = Compare(),
IndexChange index_change = IndexChange());
// Other methods not important in my question
private:
IndexChange index_change_;
Compare compare_;
}
// Realization of methods
template<class T, class Compare>
List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}
I have learned what std::function<> do from this site but I don't understand why we are writing using before 'IndexChange'
Also I have no idea what does compare_(compare) means, and what mean single colon in realization of List_of_objects()
I think that it is not difficult questions but I meet a deadline, I can't find answers in Stroustrup and in i-net:(
Thank you for any help!
using IndexChange = std::function<void(const T& element, size_t new_element_index)>;
permits you not to write the definition another time. This is a shortcut.
Then
IndexChange index_change_;
is the same as defining
std::function<void(const T& element, size_t new_element_index)> index_change_;
See : What is the logic behind the "using" keyword in C++?
Concerning compare_, it is the attribute where its type is defined by the template argument Compare in your definition, and by default it is std::less.
So calling compare_() will call an instance of std::less or another class if you instantiate List_of_objects with another template argument. As for example : List_of_objects<int, MyComparator> then the class MyComparator will be used instead of std::less.
See : Default template arguments for function templates
The part with the semicolons will initialize the members of your class.
See : In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?
using IndexChange =
std::function<void(const T& element, size_t new_element_index)>;
You can understand this as an abbreviation for a type. Instead of having to write std::function<..... you can simply use IndexChange, as it is done here:
explicit Heap(
Compare compare = Compare(),
IndexChange index_change = IndexChange());
This is a constructor for List_of_objects:
List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}
the part after the single colon initializes members with the parameters passed to the constructor.

What are templates in C++ 2 [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 9 years ago.
Improve this question
Hello everybody could anyone explain to me the difference between class temlplate member functio return type of type T (parameterized type) and an object of the claas return type
template<class T>
class array
{
public:
array(T tx): tObj(tx){}
T getObj()const{return tObj;}
array GETTOBJ()const;
private:
T tObj;
}
I have confusion: what is the difference between the return value of the functions getObj() and the GETTOBJ()?????
You've got:
an array
which holds tObj
The tObj's real value is given through the arrays' constructor, and the array remembers it.
The getObj method returns the tObj, the thing that array remembered.
The GETOBJ method returns an array. It's code is not shown, but the difference is already here: it returns some array, not the thing the array remembers.
It's a difference like "returns a candy from a box" and "return the box".
Btw. I think that your sig is missing type parameter. I mean:
not: array GETTOBJ()const;
but: array<T> GETTOBJ()const;
because array is a template, and array without type parameters is meaningless.
In the folowing example "class T" is not defined so, then when initializing this class, you can add as parameters what ever you what. For example:
array <int> _array_var(); //OR
array <char> _array_var(); // OR WHAT TYPE YOU WHANT, EVEN SOME STRUCTURES OR CLASSES
in your example "T tOBJ()" will return something of the type you initialized. For example:
if you
array <int> _array_var(); // THAN T=int.... SO T tOBJ will return int type, equivalent to
int tOBJ();
and so on... class T = typename T
for more info check:
http://en.wikipedia.org/wiki/Template_(C%2B%2B)
ow yeah... and GETTOBJ returnes type of the class... always...
the can be equal GETTOBJ with tOBJ in case you define:
array <array> _array_var();

Regarding Templates in c++ [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
this might be a silly question but it's very important to me , tomorrow is my final exam in Object-Oriented C++ , anyways my teacher said something very important (That I can't remember) about when I am supposed to specify variables or data types when I am making an object, it's something like < int , int , int , double , int > .. everything on the left side must be the same type or the opposite I can't remember it, please if anyone got the idea about what I am talking about PLEASE help me <3 am desperate
Editing:
ugh I'm very sorry but I am really scared from this exam and this thing is important :( anyways when I am making an object from a class that has templates it goes like this Class <.int, int, double.> obj1 , right ? these data type in the middle, there's a rule about them that says if the first type is integer then everything on it's right or left must be the same type, or something like that, that's what I can't remember
A class template declaration consists of a name and the template parameters. The template parameters come first, and are written within angle brackets, like this:
template <typename Foo, typename Bar>
class Gizmo
{
};
At this point, Foo and Bar aren't real types -- they are placeholders for real types you will provide when you instantiate the Gizmo.
Instantiating a class template starts with the class name, followed by <, and then a comma-seperated list of template parameters, followed finally by a > and your variable name:
Gizmo<int, double> myGizmo;
these data type in the middle, there's a rule about them that says if
the first type is integer then everything on it's right or left must
be the same type, or something like that, that's what I can't remember
No, there is no such rule. Any of the template parameters above could be any type. The following are all legitimate:
Gizmo<int, double> g1;
Gizmo<int, std::string> g2;
Gizmo<std::string, int> g3;
Gizmo<double, char> g4;
What you might be thinking of is default parameters. With class templates you can specify some defaults for the template parameters:
template <typename Foo=int, typename Bar=double>
class Gizmo
{
};
However, if you do specify a default template parameter, then all subsequent template parameters must also have defaults. So this is OK:
template <typename Foo, typename Bar=int>
class Gizmo
{
};
...but this is not:
template <typename Foo=int, typename Bar>
class Gizmo
{
};
because there is a default for Foo, but not for Bar.