news 2026/1/19 23:28:36

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/1/18 2:29:50

ARM处理器模式切换深度剖析

ARM处理器模式切换&#xff1a;从异常响应到系统安全的底层逻辑你有没有遇到过这样的场景&#xff1f;一个嵌入式系统在运行中突然卡死&#xff0c;调试器显示程序“莫名其妙”跳进了Data Abort处理函数&#xff1b;或者你在写Bootloader时&#xff0c;发现SVC指令根本没触发预…

作者头像 李华
网站建设 2026/1/19 3:09:31

STLink驱动安装:STM32开发必备手把手教程

STLink驱动安装&#xff1a;STM32开发必备手把手教程 从一块Nucleo板说起——为什么我连不上STM32&#xff1f; 你有没有过这样的经历&#xff1f; 满怀期待地打开新买的Nucleo开发板&#xff0c;USB线一插&#xff0c;准备烧录第一个“Hello World”程序&#xff0c;结果ST…

作者头像 李华
网站建设 2026/1/14 3:06:24

元宇宙开发利器:用Holistic Tracking镜像快速实现数字人驱动

元宇宙开发利器&#xff1a;用Holistic Tracking镜像快速实现数字人驱动 1. 引言&#xff1a;数字人驱动的技术演进与核心挑战 随着元宇宙概念的持续升温&#xff0c;虚拟数字人作为人机交互的核心载体&#xff0c;正在从游戏、直播向教育、客服、电商等多领域渗透。而实现自…

作者头像 李华
网站建设 2026/1/14 3:05:56

DLSS Swapper终极指南:解锁游戏性能新高度

DLSS Swapper终极指南&#xff1a;解锁游戏性能新高度 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper 还在为游戏卡顿而烦恼&#xff1f;面对眼花缭乱的DLSS版本不知如何选择&#xff1f;别担心&#xff0c;DLSS Swapp…

作者头像 李华
网站建设 2026/1/14 3:05:47

DLSS Swapper终极升级指南:一键替换DLSS版本完整教程

DLSS Swapper终极升级指南&#xff1a;一键替换DLSS版本完整教程 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper 想要轻松升级游戏DLSS版本&#xff0c;获得更出色的图像质量和性能表现吗&#xff1f;DLSS Swapper作为…

作者头像 李华
网站建设 2026/1/14 3:05:35

DLSS Swapper深度指南:解锁游戏画质与性能的终极秘籍

DLSS Swapper深度指南&#xff1a;解锁游戏画质与性能的终极秘籍 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper 还在为游戏画面不够清晰流畅而烦恼吗&#xff1f;DLSS Swapper正是你需要的利器&#xff01;这款开源工…

作者头像 李华