文章标题

C++基础1:从C过渡到C++

C plus plus – C语言的超集/带Class的C语言
C++可以完全引用C/C不能直接调用C++

案例:输出Hello World

  • 源代码:HelloWorld.cpp
#include <iostream>
using namespace std;
int main(){
    cout<<"Hello world"<<endl; 
}
  • 编译:g++ HelloWorld.cpp -o HelloWorld
  • 执行:./HelloWorld
  • 结果:Hello world

HelloWorld.cpp,看C++与C的基本区别:
1. 文件后缀名.cpp
2. 头文件#include <iostream>
3. 命名空间 using namespace std;
4. 标准输出cout、输出运算符<<、换行endl
5. 编译工具g++

g++安装:yum install gcc-c++


1. 源文件后缀
  • C/C++后缀名区别
CC++
*.c*.cpp *.cc *.cxx

* 不同编译器C++后缀名区别

平台可用后缀名
Unix*.C, *.cc, *.cxx, *.c
GNU C++*.C, *.cc, *.cxx, *.cpp, *.c++
Borland C++*.cpp
Microsoft Visual C++*.cpp, *.cxx, *.cc

2.引用头文件

C++头文件使用C标准库,在C标准库文件名前加上字母c,并且省略后缀名.h,例如:

CC++
#include <stdio.h>#include <iosteam> /#include <cstdio>
#include <stdlib.h>#include <cstdlib>
#include <string.h>#include <cstring>
#include <math.h>#include <cmath>

有些C++编译器同时支持以上两种头文件,但有些不。请使用C++标准方式


3. 函数重载(overload)

实验: 以下C与C++的编译执行结果
* printf.c

#include <stdio.h>
void printf(){
    printf("Hello world");
}
int main(){
    printf();
}
  • printf.cpp
#include <cstdio>
void printf(){
    printf("Hello world");
}
int main(){
    printf();
}

函数重载:函数名相同只有参数(个数或者类型)不相同。

CC++
不支持重载支持重载

4. 命名空间

实验: 以下C的编译结果
* scope.c

#include <stdio.h>
void test(){
    printf("this is test\n");
}
void test(){
    printf("this is another test\n");
}
int main(){
    test();
}

作用域

CC++
文件、函数、复合语句命名空间、文件、类作用域、函数、复合语句

命名空间

CC++
不支持命名空间支持命名空间

* 命名空间的作用:避免全局变量、函数、类的命名冲突(因为名字相同而编译失败)。

  • 定义命名空间
namespace 空间名 {
}
  • 引用命名空间
using namespace 空间名;
  • 标准命名空间std
using namespace std;

C++命名空间处理方式

#include <cstdio>
namespace scope1 {
    void test(){
        printf("this is test\n");
    }
}
namespace scope2 {
    void test(){
        printf("this is another test\n");
    }
}
int main(){
    scope1::test();
    scope2::test();
}
  • 命名空间嵌套
namespace A {
  void test1();
  namespace B {
    void test2();
    namespace C {
      void test3();
    }
  }
}

在命名空间A外访问

A::test1();
A::B::test2();
A::B::C::test3();

在命名空间A内访问

test1();
B::test2();
B::C::test3();

在命名空间B内访问

test1();
test2();
C::test3();

在命名空间C内访问

test1();
test2();
test3();
namespace A {
  void test();
  namespace B {
    void test();
    namespace C {
      void test();
    }
  }
}

在命名空间A外访问

A::test();
A::B::test();
A::B::C::test();

在命名空间A内访问

test1();
B::test2();
B::C::test3();

在命名空间B内访问

test1();
test2();
C::test3();

在命名空间C内访问

test1();
test2();
test3();

命名空间可以嵌套,类定义是否嵌套?结构体/联合体定义是否可以嵌套?函数定义是否可以嵌套?复合语句(if-else for while do-while)是否可以嵌套?

class A{
    class B{
    };
}
void test1(){
    int test2(){
    }
}
  • 命名空间可以重复定义
namespace A{
  void test1();
  // ...
}

namespace A{
  void test2();
  // ...
}

全局命名空间/默认命名空间/匿名命名空间:所有没有定义命名空间的函数都在全局命名空间内。
比如:main()函数,使用

5. 类型
  • 新增基本类型booltrue/false
    • password.c
#include <stdio.h>
int main(){
      printf("input user name:");
      char name[BUFSIZ];
      scanf("%s",name);
      printf("input 3 number password:");
      int password1;
      scanf("%d",&password1);
      printf("input 3 number password again:");
      int password2;
      scanf("%d",&password2);
      printf("password check:%d\n", password1 == password2);
}
  • password.cpp
#include <iostream>
#include <cstdio>
using std::cout;
using std::cin;
using std::endl;
int main(){
      cout << "input user name:";
      char name[BUFSIZ];
      cin >> name;
      cout << "input 3 number password:";
      int password1;
      cin >> password1;
      cout << "input 3 number password again:";
      int password2;
      cin >> password2;
      cout << "password check:" << (password1 == password2) << endl;
}
  • 新增自定义类型class
    详细信息参见:类与对象章节

  • C++的新增关键and or not

if(a>b && c<d){

}
if(a>b and c<d){

}

在C语言中iso646.h定义了这些宏定义。


6. 思想
CC++
面向过程面向对象/基于对象、模板/泛型编程

何为面向过程?何为面向对象?
* 面向过程:强调如何处理,通常是自下而上。
* 面向对象:强调执行处理的对象,通常是自上而下。

面向过程与面向过程、厨师与老板

7. 动态内存
  • 基本类型的动态内存
    • dynamic_mem.c
#include <stdio.h>
#include <stdlib.h>
int main(){
      int* num = malloc(sizeof(int));
      *num = 100;
      printf("%d\n",*num);
      free(num); 
}
  • dynamic_mem.cpp
#include <iostream>
int main(){
      int* num = new int;
      *num = 100;
      std::cout << *num << std::endl;
      delete num; 
}

标量:单个值(基本类型(int、float、char)/复合类型(数组、指针、引用))/向量:多个值(类、结构体、联合体等)

new delete都是关键字,用于申请/释放堆内存。

动态内存区别

CC++
malloc()/free()new/delete

C++仍然可以使用malloc()/free(),但是不建议这么做。

问题:
* malloc()申请内存,是否可以使用delete销毁内存?
* new申请内存,是否可以使用free()销毁内存?

注意:deletefree()动态内存后,指针指向NULL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值