找到 34423 篇文章,关于编程

在 C++ 中使用 shared_ptr 进行虚拟析构

Ayush Gupta
更新于 2020年3月12日 06:36:51

146 次浏览

在本教程中,我们将讨论一个程序来理解在 C++ 中使用 shared_ptr 进行虚拟析构。为了删除类的实例,我们将基类的析构函数定义为虚拟的。因此,它会按照创建它们的相反顺序删除继承的各种对象实例。示例 在线演示 #include #include using namespace std; class Base { public: Base(){ cout

C++ 中的虚拟基类

Ayush Gupta
更新于 2020年3月12日 06:34:40

14K+ 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的虚拟基类。虚拟类主要用于多重继承。为了避免同一类的多个实例被添加到同一类中,这后来会导致歧义,因此使用虚拟类。示例 在线演示 #include using namespace std; class A { public: int a; A(){ a = 10; } }; class B : public virtual A { }; class C : public virtual A { }; class D : public B, public C { }; int main(){ //创建类 D 对象 D object; cout

在 C++ 中使用类实现向量数量

Ayush Gupta
更新于 2020年3月12日 06:28:56

153 次浏览

在本教程中,我们将讨论一个程序来理解如何在 C++ 中使用类实现向量数量。向量数量既有大小又有方向。这里我们将使用类来实现它们,然后对它们执行基本运算。示例 在线演示 #include #include using namespace std; class Vector { private: int x, y, z; //向量的分量 public: Vector(int x, int y, int z){ this->x = x; this->y = y; this->z = z; } Vector operator+(Vector ... 阅读更多

C++ 中的类型推断 (auto 和 decltype)

Ayush Gupta
更新于 2020年3月12日 06:25:42

148 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的类型推断 (auto 和 decltype)。对于 auto 关键字,变量的类型是从其初始化程序的类型定义的。此外,使用 decltype,您可以从调用的元素中提取变量的类型。auto 类型示例 在线演示 #include using namespace std; int main(){ auto x = 4; auto y = 3.37; auto ptr = &x; cout

C++ 中的平凡类

Ayush Gupta
更新于 2020年3月12日 06:22:23

346 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的平凡类。当类/结构体在其内部包含显式默认值时,它被称为平凡类。此外,平凡类拥有自己的构造函数、赋值运算符和析构函数。示例 //使用默认构造函数 struct Trivial { int i; private: int j; }; //定义你自己的构造函数 //然后将其标记为默认 struct Trivial2 { int i; Trivial2(int a, int b){ i = a; } Trivial2() = default; };输出(没有输出,因为我们只是在这里定义类,并没有从中创建对象实例。)

C++ 中的 transform_inclusive_scan() 函数

Ayush Gupta
更新于 2020年3月12日 06:19:56

56 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的 transform_inclusive_scan() 函数。示例 在线演示 #include #include using namespace std; namespace point_input_iterator { template OutputItrator transform_inclusive_scan(InputItrator first, InputItrator last, OutputItrator d_first, BinaryOperation binary_op, UnaryOperation unary_op){ *d_first = unary_op(*first); first++; d_first++; for (auto it = first; it != last; it++) { //计算前缀和 *d_first = binary_op(unary_op(*it), *(d_first ... 阅读更多

C++ 中的 thread get_id() 函数

Ayush Gupta
更新于 2020年3月12日 06:15:40

124 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的 thread get_id() 函数。Thread get_id() 函数验证进程的当前状态,然后返回当前执行线程的 ID。此函数不接受任何参数。示例 #include #include #include using namespace std; //创建线程 void sleepThread(){ this_thread::sleep_for(chrono::seconds(1)); } int main(){ thread thread1(sleepThread); thread thread2(sleepThread); thread::id t1_id = thread1.get_id(); thread::id t2_id = thread2.get_id(); cout

C++ 中的包含性

Ayush Gupta
更新于 2020年3月12日 06:08:55

2K+ 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的包含性。某个类是否包含另一个类,这个参数被称为包含性。内部类被称为被包含类,而包含它的类被称为容器类。示例 在线演示 #include using namespace std; class first { public: first(){ cout

C++ STL 中的容器

sudhir sharma
更新于 2024年5月29日 13:11:59

1K+ 次浏览

C++ STL(标准模板库)是一组强大的 C++ 模板类,它提供通用的类和函数以及实现许多流行且常用的算法和数据结构(如向量、列表、队列和堆栈)的模板。它是一个容器类、算法和迭代器的库。它是一个通用的库,因此其组件是参数化的。使用 STL 的前提是需要了解模板类。模板是 C++ 编程语言的一项特性,它允许函数和类使用泛型类型进行操作。这允许函数或类在许多不同的... 阅读更多

C++ 编程中的构造函数

Ayush Gupta
更新于 2020年3月12日 06:02:08

524 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的构造函数。构造函数是类的成员函数,它初始化对象实例的创建。它们与父类同名,并且没有任何返回类型。默认构造函数示例 在线演示 #include using namespace std; class construct { public: int a, b; //默认构造函数 construct(){ a = 10; b = 20; } }; int main(){ construct c; cout

广告
© . All rights reserved.