What is the meaning of generic programming in c++?
Also, I am trying to figure out what container, iterator, and different types of them mean.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generic programming means that you are not writing source code that is compiled as-is but that you write “templates” of source codes that the compiler in the process of compilation transforms into source codes. The simplest example for generic programming are container classes like arrays, lists or maps that contain a collection of other objects. But there’s much more to generic programming. In the context of C++ (and called meta programming) it means to write programs that are evaluated at compile time.
A basic example of generic programming are templates of containers: In a statically typed language like C++ you would have to declare separate containers that hold integers, floats, and other types or deal with pointers to
voidand therefore losing all type information. Templates which are the C++ way of generic programming leverage this constraint by letting you define classes where one or more parameters are unspecified at the time you define the class. When you instance the template later you tell the compiler which type it should use to create the class out of the template. Example:Templates are generic because the compiler translates the template into actual code. Note that in the case you don’t instantiate your template no code will be generated for it at all. On the other hand, if you declare a
MyContainer<int>, aMyContainer<float>, and aMyContainer<String>the compiler will create three versions of your code each of them having a different type. There will be some optimizations involved but basically your template code will be instantianted to three new types.Iterators are a design pattern that were popularized in the seminal book “Design Patterns” by Gamma et al. It’s a pattern to iterate over the content of a container class. Unlike using a
for-loop an iterator is an instance of a class that points to a member of the container and gives you an unified interface to traverse the container as well as accessing the members. Take look at this example:In this C++ example I’m instantating a template QList with type
int. QList a container class that stores a list of objects. In this example we will use it to store integers.Then I create an iterator
ito traverse through the list.myList.begin()returns an iterator that points to the first element of the list. We can compare the iterator with another iteratormyList.end()that points after the last element of the list. If both iterators are the same we know that we have passed the last elment. In the loop we’re printing the element by accessing it with*iand go to the next element withi++.Note that in this example
*and++are overloaded operators and reimplemented by the iterator class. In a programming language without operator overloading there could be methods likei.element()ori.next()that do the same task. It’s important to see thatiis not a pointer but a whole class that just mimics the behaviour of a pointer.What’s the benefit of iterators? They provide a unified way to access the members of a container class, completely indepented on how the container class is implemented internally. No matter if your want to traverse a list, map or tree, the iterator classes (should) always work the same way.