“通用编程”的含义是什么,在c ++? [英] What is the meaning of "generic programming" in c++?

查看:116
本文介绍了“通用编程”的含义是什么,在c ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ c

$ b

另外,我试图找出容器,迭代器,而且不同类型的意思。

解决方案

不是编写原样编译的源代码,而是编写编译器在编译过程中转换为源代码的源代码的模板。通用编程的最简单的例子是容器类,如包含其他对象集合的数组,列表或映射。但还有更多的泛型编程。在C ++的上下文中(并称为元编程)意味着编写在编译时评估的程序。



<通用编程的一个基本例子是容器的模板:在类似C ++的静态类型语言中,你必须声明保存整数,浮点数和其他类型的单独容器,或者处理指向 void ,因此丢失所有类型信息。作为通用编程的C ++方法的模板通过允许您定义类,在定义类时未指定一个或多个参数时,利用此约束。当你实例化模板以后你告诉编译器应该使用哪个类型从模板创建类。示例:

 模板< typename T& 
class MyContainer
{
//处理任意类型的容器T
};

void main()
{
//使MyContainer只接受int。
MyContainer< int> intContainer;
}

模板是通用,因为编译器翻译模板转换为实际代码。请注意,在不实例化您的模板的情况下,将不会为其生成任何代码。另一方面,如果你声明一个 MyContainer< int> ,一个 MyContainer< float> c $ c> MyContainer< String> ,编译器将创建三个版本的代码,每个代码都有不同的类型。



迭代器是一种设计模式

/ strong>,被Gamma等人在设计模式的开创性着作中推广。它是一个模式来迭代容器类的内容。与使用 for -loop不同,迭代器是一个指向容器成员的类的实例,它提供了一个统一的接口来遍历容器以及访问成员。看看这个例子:

  // Instanciate模板QList类型int 
QList< int> myList;

//将一些int放入myList中

//指向
//的第一个成员的Copyconstruct迭代器。
QList< int> :: iterator i = myList.begin();

//遍历列表
while(i!= myList.end()){
std :: cout< * i < std :: endl;
i ++;
}

在这个C ++示例中,我正在实例化一个模板 QList类型 int 。 QList存储对象列表的容器类。在这个例子中,我们将使用它来存储整数。



然后,我创建一个迭代器 i 遍历列表。 myList.begin()返回一个迭代器,指向列表的第一个元素。我们可以将迭代器与另一个迭代器 myList.end()比较,该指向列表的最后一个元素。如果两个迭代器是相同的,我们知道我们已经通过了最后一个元素。在循环中,我们通过访问 * i 打印元素,然后转到下一个具有 i ++ 的元素。



请注意,在此示例中 * ++ 是重载运算符,并由iterator类重新实现。在没有操作符重载的编程语言中,可以有 ielement() i.next()做同样的任务。重要的是看到 i 不是一个指针,而是一个只是模仿指针行为的整个类。



迭代器的好处是什么?它们提供了一种统一的方式来访问容器类的成员,完全不考虑容器类是如何在内部实现的。无论你想要遍历列表,映射还是树,迭代器类(应该)总是以相同的方式工作。


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.

解决方案

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 void and 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:

template<typename T>
class MyContainer
{
    // Container that deals with an arbitrary type T
};

void main() 
{
    // Make MyContainer take just ints.
    MyContainer<int> intContainer;
}

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>, a MyContainer<float>, and a MyContainer<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:

// Instanciate template QList with type int
QList<int> myList;

// put some ints into myList

// Copyconstruct iterator that points to the
// first member of the list.
QList<int>::iterator i = myList.begin();

// Iterate through the list 
while (i != myList.end()) {
  std::cout << *i << std::endl;
  i++;
}

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 i to 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 iterator myList.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 *i and go to the next element with i++.

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 like i.element() or i.next() that do the same task. It's important to see that i is 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.

这篇关于“通用编程”的含义是什么,在c ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆