找到 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.