news 2026/4/11 0:24:10

C++入门指南:从零开始,做一名优秀的程序员

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++入门指南:从零开始,做一名优秀的程序员

“Hello,world”

2.1. C++和C语言的关系

C++兼容C语言的绝大多数语法,所以C语言的"Hello World"在C++中也能运行。但是C++有自己的输入输出方式,更现代化、更方便。

代码语言:javascript

AI代码解释

// C语言的Hello World #include <stdio.h> int main() { printf("Hello World\n"); return 0; }

代码语言:javascript

AI代码解释

// C++的Hello World #include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }

这里有几个关键点需要解释:

  • #include <iostream>:C++的输入输出流库,相当于C语言的#include <stdio.h>
  • using namespace std;:使用标准命名空间,让coutendl等可以直接使用
  • cout:C++的输出流,相当于C语言的printf
  • <<:流插入运算符,用于将数据插入到输出流中
  • endl:换行并刷新缓冲区,相当于C语言的\n
2. 2 为什么C++的输入输出更友好?

C语言的printfscanf需要手动指定格式,比如printf("%d", a),而C++的cout可以自动识别变量类型:

代码语言:javascript

AI代码解释

int a = 10; double b = 3.14; cout << a << " " << b << endl; // 输出:10 3.14

这就像C++自带了一个"智能助手",能自动判断你输出的是什么类型,不需要你手动指定格式。

三、命名空间:解决名字冲突的"小助手"

3.1. 为什么需要命名空间?

在C语言中,如果两个文件定义了同名的函数或变量,编译器就会报错,就像两个同学都叫"小明",老师很难区分谁是谁。

代码语言:javascript

AI代码解释

// 文件1 int rand = 10; // 文件2 int rand = 20; // 编译错误:重复定义

C++引入了命名空间(namespace),就像给每个人分配了一个"家庭",在同一个家庭内可以有同名的人,但不同家庭之间不会冲突。

3.2.命名空间怎么用

代码语言:javascript

AI代码解释

// 定义命名空间 namespace bit { int rand = 10; int Add(int left, int right) { return left + right; } } int main() { // 访问bit命名空间中的rand cout << bit::rand << endl; // 输出:10 // 访问bit命名空间中的Add函数 cout << bit::Add(1, 2) << endl; // 输出:3 return 0; }

命名空间的三种方式:

指定命名空间访问(推荐):

代码语言:javascript

AI代码解释

cout << bit::rand << endl;

using将命名空间中某个成员展开:

代码语言:javascript

AI代码解释

using bit::rand; cout << rand << endl;

展开命名空间中全部成员(不推荐,可能引起冲突):

代码语言:javascript

AI代码解释

using namespace bit; cout << rand<<endl;

四、C++输入输出:比C语言更智能

1. 为什么C++的输入输出更好?

C语言的输入输出需要手动指定格式,比如scanf("%d", &a),而C++的cin可以自动识别变量类型:

代码语言:javascript

AI代码解释

int a; doubie b; cout << a << b; //能够自动识别 a是 int 型,b是 double型

这就像

2. 为什么需要using namespace std;

C++标准库中的所有函数和对象都放在std命名空间中,比如coutcinendl等。using namespace std;就是把std命名空间中的所有内容"导入"到当前作用域,这样我们就可以直接使用coutcin等,而不需要写std::cout

提醒:在实际项目中,我们不建议使用using namespace std;,因为它可能引起名字冲突。但在学习阶段,为了方便,可以暂时使用。

www.dongchedi.com/article/7594907272338391577
www.dongchedi.com/article/7594906883010921022
www.dongchedi.com/article/7594905551013265982
www.dongchedi.com/article/7594904583437648409
www.dongchedi.com/article/7594903316430275134
www.dongchedi.com/article/7594903056937206296
www.dongchedi.com/article/7594901837732512318
www.dongchedi.com/article/7594901098721427993
www.dongchedi.com/article/7594900396469322265
www.dongchedi.com/article/7594899476994163224
www.dongchedi.com/article/7594900087831085593
www.dongchedi.com/article/7594898822019744280
www.dongchedi.com/article/7594899523533996569
www.dongchedi.com/article/7594899750689145406
www.dongchedi.com/article/7594897483562828350
www.dongchedi.com/article/7594898207591711257
www.dongchedi.com/article/7594896653753565720
www.dongchedi.com/article/7594898207591809561
www.dongchedi.com/article/7594896961158349374
www.dongchedi.com/article/7594897178171327001
www.dongchedi.com/article/7594897178171523609
www.dongchedi.com/article/7594895915098718745
www.dongchedi.com/article/7594894897057317400
www.dongchedi.com/article/7594894386924700222
www.dongchedi.com/article/7594895162640433726
www.dongchedi.com/article/7594893333349466649
www.dongchedi.com/article/7594893449250783806
www.dongchedi.com/article/7594894572120343065
www.dongchedi.com/article/7594870233123340825
www.dongchedi.com/article/7594914712018600510
www.dongchedi.com/article/7594913283375907352
www.dongchedi.com/article/7594914016129106456
www.dongchedi.com/article/7594914424213766718
www.dongchedi.com/article/7594913083894891033
www.dongchedi.com/article/7594913119710069310
www.dongchedi.com/article/7594912459706696254
www.dongchedi.com/article/7594911633613390360
www.dongchedi.com/article/7594909036307595800
www.dongchedi.com/article/7594910057444786750
www.dongchedi.com/article/7594909893274927641
www.dongchedi.com/article/7594909974816588350
www.dongchedi.com/article/7594909035217404441
www.dongchedi.com/article/7594908551181615678
www.dongchedi.com/article/7594906883010855486
www.dongchedi.com/article/7594907513641058878
www.dongchedi.com/article/7594905549272285720
www.dongchedi.com/article/7594906861996884505
www.dongchedi.com/article/7594906134906421785
www.dongchedi.com/article/7594905869373096472
www.dongchedi.com/article/7594904114086560281
www.dongchedi.com/article/7594903301414650392
www.dongchedi.com/article/7594901235942228542
www.dongchedi.com/article/7594901805579452953
www.dongchedi.com/article/7594900315275657752
www.dongchedi.com/article/7594899458794799641
www.dongchedi.com/article/7594900266315645465
www.dongchedi.com/article/7594900151853236798
www.dongchedi.com/article/7594898846111580697
www.dongchedi.com/article/7594899021756776984
www.dongchedi.com/article/7594897166766965272
www.dongchedi.com/article/7594897310665245246
www.dongchedi.com/article/7594897192310407705
www.dongchedi.com/article/7594897631617794585
www.dongchedi.com/article/7594896580080435737
www.dongchedi.com/article/7594895704896684568
www.dongchedi.com/article/7594897374330503705
www.dongchedi.com/article/7594895618720449048

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/9 20:12:58

DLSS版本管理工具:从零开始掌握DLSS Swapper

DLSS版本管理工具&#xff1a;从零开始掌握DLSS Swapper 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper DLSS版本管理工具DLSS Swapper让玩家能够轻松切换游戏中的DLSS动态链接库&#xff0c;实现版本升级或回退。这款…

作者头像 李华
网站建设 2026/4/4 4:25:20

Holistic Tracking实战案例:智能医疗康复评估系统

Holistic Tracking实战案例&#xff1a;智能医疗康复评估系统 1. 引言 1.1 业务场景描述 在现代医疗康复领域&#xff0c;精准、客观的动作评估是制定个性化治疗方案的关键。传统的康复评估依赖于医生肉眼观察和经验判断&#xff0c;存在主观性强、量化不足、难以持续跟踪等…

作者头像 李华
网站建设 2026/4/3 21:20:44

8大网盘直链下载工具:彻底告别限速困扰的完整方案

8大网盘直链下载工具&#xff1a;彻底告别限速困扰的完整方案 【免费下载链接】Online-disk-direct-link-download-assistant 可以获取网盘文件真实下载地址。基于【网盘直链下载助手】修改&#xff08;改自6.1.4版本&#xff09; &#xff0c;自用&#xff0c;去推广&#xff…

作者头像 李华
网站建设 2026/4/1 18:42:42

游戏画质优化新纪元:DLSS Swapper完全使用指南

游戏画质优化新纪元&#xff1a;DLSS Swapper完全使用指南 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper 还在为游戏画面不够清晰、帧率不稳定而烦恼吗&#xff1f;DLSS Swapper作为一款专业的游戏画质优化工具&#…

作者头像 李华
网站建设 2026/4/6 6:15:05

GARbro视觉小说资源管理器完全使用手册

GARbro视觉小说资源管理器完全使用手册 【免费下载链接】GARbro Visual Novels resource browser 项目地址: https://gitcode.com/gh_mirrors/ga/GARbro GARbro是一款专为视觉小说游戏设计的强大资源浏览工具&#xff0c;能够让你轻松访问和管理游戏中的图像、音频等各类…

作者头像 李华
网站建设 2026/4/8 9:18:32

如何快速获取网盘直链:八大云盘高速下载完整指南

如何快速获取网盘直链&#xff1a;八大云盘高速下载完整指南 【免费下载链接】Online-disk-direct-link-download-assistant 可以获取网盘文件真实下载地址。基于【网盘直链下载助手】修改&#xff08;改自6.1.4版本&#xff09; &#xff0c;自用&#xff0c;去推广&#xff0…

作者头像 李华