C++之C++入门篇重点代码
·
C++入门重点代码
《C++之C++入门》那篇文章详细叙述了C++的学习目标以及 C++ 入门的基础且重要的十大知识点。如果小伙伴们仔细阅读过那篇文章,我相信你们现在已经完全理解并掌握了C++入门阶段的基础知识啦~ 本篇文章是对《C++之C++入门》那篇文章的重点知识代码补充,让我们一起开启C++代码之旅吧(ง •_•)ง
1.命名空间
局部域 >全局域 >展开命名空间域 or 指定命名空间域
#include<stdio.h>
int a = 1;
namespace xh
{
int a = 3;
}
//using namespace xh;//展开就相当于暴露在全局
int main()
{
int a = 2;
printf("%d\n", a); //局部
printf("%d\n", ::a); //全局
printf("%d\n", xh::a); //指定命名空间
return 0;
}
运行结果:
2.cout自动识别类型
#include<iostream>
using namespace std;
int main()
{
int x = 10;
float y = 11.1;
std::cout << x << " " << y << std::endl;
return 0;
}
运行结果:
3.缺省参数
缺省参数分为全缺省和半缺省两类。
#include<iostream>
using namespace std;
void Func(int a, int b = 1,int c = 2)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
Func(1);
Func(1, 2, 3);
//Func();//错误的,因为a不能没有参数 return 0;
}
运行结果:
4.函数重载
#include<iostream>
using namespace std;
void f()
{
cout << "f()" << endl;
}
void f(int a = 0)
{
cout << "f(int a)" << endl;
}
int main()
{
f();//错误的,重载函数的调用不明确
return 0;
}
运行结果:
5.引用
5.1 案例一
#include<iostream>
using namespace std;
int main()
{
int a = 0;
int& b = a;
int& c = b;
int& d = c;
cout << &a << endl;
cout << &b << endl;
cout << &c << endl;
cout << &d << endl;
b++;
d++;
return 0;
}
运行结果:
5.2 案例二
#include<iostream>
using namespace std;
void Swap(int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
void Swap(int*& a, int*& b)
{
int* tmp = a;
a = b;
b = tmp;
}
int main()
{
int x = 1, y = 2;
Swap(x, y);
cout << x << " " << y << endl;
int* px = &x, * py = &y;
cout << px << " "<< py << endl;
Swap(px, py);
cout << px << " " << py << endl;
return 0;
}
运行结果:
5.2.1 引用使用场景(错误样例,引用做返回值)
#include <iostream>
using namespace std;
int& Count()//引用做返回值(当去掉static时)
{
int n = 0;
n++;
return n;
}
int main()
{
int ret = Count(); //ret是不确定的值,Count函数结束,栈帧销毁,没有清理栈帧,则ret的结果侥幸是正确的
cout << ret << endl; //Count函数结束,栈帧销毁,清理栈帧,则ret的结果是随机值
return 0;
} //不在栈帧里面在静态区,堆区不危险,在栈桢里面危险,再加一层引用更危险。
运行结果:
5.2.2 引用使用场景(正确样例,顺序表读写功能)
#include <iostream>
using namespace std;
#include <assert.h>
struct SeqList
{
int a[100];
size_t size;
};
int& SLAL(SeqList* ps, int pos)
{
assert(pos < 100 && pos >= 0);
return ps->a[pos];
}
int main()
{
SeqList s;
SLAL(&s, 0) = 1;
cout << SLAL(&s, 0) << endl;
SLAL(&s, 0) += 5;
cout << SLAL(&s, 0) << endl;
return 0;
}
运行结果:
5.2.3 引用使用场景(高级用法,c++用法)
#include <iostream>
using namespace std;
#include <assert.h>
struct SeqList
{
int a[100];
size_t size;
int& at(int pos)
{
assert(pos < 100 && pos >= 0);
return a[pos];
}
int& operator[](int pos)
{
assert(pos < 100 && pos >= 0);
return a[pos];
}
};
int main()
{
SeqList s;
s.at(0) = 1;
s.at(0) += 5;
cout << s.at(0) << endl;
s[1] = 10;
s[1] += 5;
cout << s[1] << endl;
return 0;
}
运行结果:
5.3 常引用
#include <iostream>
int& func()
{
static int x = 0;
return x;
}
int main()
{
int& ret = func(); //语句成立,权限平移( ret 就是 x)
const int& ret1 = func(); //语句成立,权限缩小
return 0;
}
运行结果:
5.3.1 类型提升
//运算符两边的类型不同会产生截断或者提升(小的值向大的值提升,即变成大的值的类型)
#include<iostream>
int main()
{
int i = 1;
double j = 1.1;
if (j > i) //i被提升为double类型
{
std::cout << "xxxxx" << std::endl;
}
return 0;
}
运行结果:
6. auto用法
6.1 typeid打印类型
#include<iostream>
int main()
{
int a = 2;
auto b = a;
std::cout << typeid(a).name() << std::endl;
std::cout << typeid(b).name() << std::endl;
return 0;
}
运行结果:
6.2 auto用途(范围for)
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
{
cout << arr[i];
}
cout << endl;
for (int* p = arr; p < arr + sizeof(arr) / sizeof(arr[0]); p++)
{
cout << *p;
}
cout << endl;
for (auto a : arr)
{
cout << a;
}
cout << endl;
for (auto& e : arr)
{
e *= 2;
cout << e;
}
cout << endl;
return 0;
}
运行结果:
7.内联函数
//宏函数可用内联函数替换
#include<iostream>
using namespace std;
inline int Add(int x, int y)
{
return (x + y)* 10;
}
#define Add1(x,y)((x) + (y))*10
int main()
{
int ret = Add(2, 3);//可以成为内联
int ret1 = Add1(2, 3);
cout << ret << endl;
cout << ret1 << endl;
for (int i = 0; i < 10000; i++)
{
cout << Add(i, i + 1) << endl;//不能成为内联,因为占用空间太大
}
return 0;
}
运行结果:
8.空指针nullptr
#include <iostream>
using namespace std;
void f(int)
{
cout << "f(int)" << endl;
}
void f(int*)
{
cout << "f(int*)" << endl;
}
int main()
{
f(0);
f(NULL); //null是0,nullptr是指针
f(nullptr);
return 0;
}
运行结果:
写在最后:到这里,咱们的 C++ 代码之旅就暂告一段落啦~ 希望这些 重点代码能像一块块基石,帮助小伙伴们更扎实地掌握 C++ 入门阶段的知识。通过这篇文章再结合《C++之C++入门》那篇文章,相信只要小伙伴们认真学习,肯定能吃透C++的这些基础且重要的内容。
从命名空间到空指针 nullptr,每一个知识点都在为我们打开 C++ 世界的一扇扇门。相信经过这段代码的实践与探索,小伙伴们对 C++ 的理解又加深了不少。未来,期待我们所有人能带着这些知识,在 C++ 的学习道路上继续大步前进,去探索更广阔、更精彩的编程天地~
更多推荐


所有评论(0)