首页 技术 正文
技术 2022年11月21日
0 收藏 571 点赞 4,922 浏览 1604 个字

1.头文件:

C++头文件不是以.h结尾,C语言中的标准库文件如math,h,stdio.h在C++中被命名为cmath,cstdio

2.命名空间:

为防止名字冲突(出现同名),C++引入名字空间(namespace)。通过::运算符限定某个名字属于哪个名字空间。

namespace name
{
//变量,函数,类等
}
//电子::“小明”

通常有三种方法使用名字空间X的名字name:

/*
using namespace X;//引入整个名字空间
using X::name;//使用单个名字
X::name;//程序中加上名字空间前缀,如X::
*/
#include <cstdio>
namespace first
{
int a;
void f(){/*...*/}
int g(){/*...*/}
}namespace second
{
double a;
double f(){/*...*/}
char g;
}int main ()
{
first::a = ;
second::a = 6.453;
first::a = first::g()+second::f();
second::a = first::g()+6.453; printf("%d\n",first::a);
printf("%lf\n",second::a); return ;
}
 #include<iostream>
//命名空间的using声明
//using namespace::name;
using std::cin;
int main()
{
int i;
cin >> i;
cout << i;//错误,没有对应的using声明
std::cout << i;
return ;
}

3.C++的输入和输出

#include <iostream> //标准输入输出头文件
#include <cmath>
using namespace std; //引入整个名字空间std中的所有名字
//cout cin都属于名字空间std;
//cout输出用<<运算符 cin紧跟>>运算符
int main() {
double a;
cout << "从键盘输入一个数" << endl;
cin >> a;
a = sin(a);
cout << a;
return ;
}

4.程序块{}内部作用域可定义域外部作用域同名的变量,在该块里就隐藏了外部变量

#include <iostream>
using namespace std;int main ()
{
double a; cout << "Type a number: ";
cin >> a; {
int a = ; // "int a"隐藏了外部作用域的“double a"
a = a * + ;
cout << "Local number: " << a << endl;
} cout << "You typed: " << a << endl; //main作用域的“double a"
return ;
}

5.struct的加强

struct Student
{
char name[];
int age;};//C语言:在定义变量结构体变量时一定要在前面加上struct关键字
struct Student stu={"wang",};
//C++:可以直接用结构体名来定义变量
Student stu ={"wang",};

6..访问和内部作用域变量同名的全局变量,要用全局作用域限定 ::

#include <iostream>
using namespace std;double a = ;int main (){
double a = ; cout << "Local a: " << a << endl;
cout << "Global a: " <<::a << endl; //::是全局作用域限定 return ;
}

7.  内联函数

对于不包含循环的简单函数,建议用inline关键字声明 为”inline内联函数”, 编译器将内联函数调用用其代码展开,称为“内联展开”,避免函数调用开销, 提高程序执行效率

内联函数没有普通函数调用时的额外开销(如压榨、跳转、返回)

inline int myMax(int a, int b)
{
return (a>b?a:b);}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845