C++基础知识(一)
文章目录
1、前言
2、编译器环境
3、第一个C++程序
🐾🐾C++在C的基础下进行了升级,我们来见证第一个C++程序。C++代码如下:
#include <iostream>//输入输出流头文件
using namespace std;//C++标准程序库中的所有标识符都被定义于一个名为std的命名空间 namespace 中
int main(){
//C++常用的输入输出函数
cout << "please input a string:" << endl;
char string[100];
cin >> string;
cout << string << endl;
//C常用的输入输出函数
printf("please input a string:\n");
scanf("%s",string);
printf("%s\n",string);
//调用DOS系统的暂停命令 pause 来暂停程序执行,按任意一个键后将继续执行。
system("pause");
return 0;
}
4、数据类型
🐾🐾枚举类型(enumeration)是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
enum 枚举名(){
标识符1(=整型常数),
标识符2(=整型常数),
...
标识符n(=整型常数)
} 枚举变量;
🐾🐾第一个枚举成员的默认值为整型的0,后续枚举成员的值依次加1,而且可以将枚举量赋值给整型变量,但反过来就不行。C++代码如下:
/* 数据类型 枚举型 */
int main(){
enum sex{boy,girl};
enum weekday{Mon,Tue,Wed,Thu,Fri,Sat,Sun}weekday1,weekday2;
int a = boy;//将枚举变量赋值给整型变量。
int b = girl;
weekday1 = Tue;//weekday1,weekday2为weekday类型。
weekday2 = weekday1;
int i = weekday2;
int j = Wed;
cout << a << endl;
cout << b << endl;
cout << i << endl;
cout << j << endl;
system("pause");
return 0;
}
🆚🆚运行结果:
0
1
1
2
📌📌实例:随机从一周中抽取两天休息时间,输出有多少个不同的组合。C++代码如下:
/* 枚举类型实例 */
/* 随机从一周中抽取两天休息 */
#include <iostream>//输入输出流头文件
#include <iomanip>//输入输出流格式控制头文件,就像C里面的格式化输出一样
using namespace std;//C++标准程序库中的所有标识符都被定义于一个名为std的命名空间namespace中
int main(){
enum restday_set{Mon,Tue,Wed,Thu,Fri,Sat,Sun};
restday_set restday;//枚举类型变量
int count=0;
for(int i=Mon;i<=Sun;i++){//Mon的值默认为0,Sun的值为6。
for(int j=i+1;j<=Sun;j++){
if(i!=j){
count++;
cout << count;
for(int c=1;c<=2;c++){
switch(c){
case 1:restday=(restday_set)i;break;
case 2:restday=(restday_set)j;break;
}
switch(restday)
{
case 0: cout << setw(5) << "Mon"; break;//setw用来设置输出的域宽。
case 1: cout << setw(5) << "Tue"; break;
case 2: cout << setw(5) << "Wed"; break;
case 3: cout << setw(5) << "Thu"; break;
case 4: cout << setw(5) << "Fri"; break;
case 5: cout << setw(5) << "Sat"; break;
case 6: cout << setw(5) << "Sun"; break;
}
}
cout << endl;
}
}
}
cout << "There are " << count << " options for your rest days." << endl;
system("pause");
return 0;
}
🆚🆚运行结果:
1 Mon Tue
2 Mon Wed
....
20 Fri Sun
21 Sat Sun
There are 21 options for your rest days.
5、存储类
🐾🐾使用extern关键字来声明并引用全局变量,全局变量对所有的程序文件都是可见的,通常用于当有两个或多个文件共享相同的全局变量或函数的时候;使用static关键字修饰局部变量可以在函数调用之间保持局部变量的值,当修饰全局变量时,会使变量的作用域限制在声明它的文件内。helloworld.cpp代码如下:
/* 存储类 */
#include <iostream>//输入输出流头文件
#include "helloworld.h"
using namespace std;//C++标准程序库中的所有标识符都被定义于一个名为std的命名空间namespace中
/* 全局函数func2,作用域在所有的程序文件。*/
extern void func2(void);
/* 静态全局变量a,作用域限制在声明它的程序文件内。 */
static int a = 6;
/* 普通全局变量count,声明时如果未初始化,系统会自动初始化为0。 */
int count;
void func()
{
/* 静态局部变量b,作用域为局部作用域,首次初始化后调用函数不再进行初始化。 */
static int b = 9;
b++;
cout << "a=" << a <<" "<< "b=" << b <<endl;
}
int main(){
while (a--)
{
func();
count++;
}
func2();
system("pause");
return 0;
}
🐾🐾helloworld.h代码如下:
#include <iostream>
using namespace std;
/* 全局变量count */
extern int count;
void func2(void){
cout << "The number of runs is " << count << endl;
}
🆚🆚运行结果:
a=5 b=10
a=4 b=11
a=3 b=12
a=2 b=13
a=1 b=14
a=0 b=15
The number of runs is 6
🐾🐾mutable存储类只能用于类的数据成员,不能用于普通变量。具有mutable性质的类的数据成员打破了类对象的const限定,允许修改类中mutable关键字修饰的数据成员,即便类的其它成员仍然是const只读属性。
/* mutable存储类 */
#include <iostream>
using namespace std;
class A
{
public:
//mutable关键字修饰的变量,即使在const函数中也将永远处于可变的状态。
void test() const;
mutable bool flag;
int id;//id成员变量不能被修改
};
//const关键字修饰的函数是为了能够保护类中的成员变量。
void A::test() const{
flag = true;
//定义id = 1;会报错In member function 'void A::test() const',成员变量不能被修改。
cout << flag << endl;
cout << id << endl;
}
int main(){
A a;
a.test();
system("pause");
return 0;
}
🆚🆚运行结果:
1
0
🐾🐾使用thread_local关键字声明的变量仅可在它在其上创建的线程上访问。 变量在创建线程时创建,并在销毁线程时销毁。 每个线程都有其自己的变量副本。C++代码如下:
#include <iostream>
#include <thread>
using namespace std;
/* 对于全局变量,thread_local变量在每个线程里是分别自加互不干扰的。 */
thread_local int n = 1;
void func1(){
n++;
/* this_thread::get_id()方法可以得到当前线程的线程ID,用来验证主线程和子线程是不同的两个线程 */
cout << "thread number is " << this_thread::get_id() << " n=" << n << endl;
//printf("thread number is %d, n=%d\n",this_thread::get_id(),n);
//cout如果存在输出格式问题,就用printf()验证。
}
void func3(){
/* 作为局部变量,thread_local变量会自动static。 */
/* thread_local虽然改变了变量的存储周期,但是并没有改变变量的使用周期或者作用域 */
thread_local int m = 0;
cout << "thread number is " << this_thread::get_id() << " m=" << m << endl;
//printf("thread number is %d, m=%d\n",this_thread::get_id(),m);
m++;
}
/* 运行2次func3() */
void func2(){
func3();
func3();
}
int main(){
n++;
func1(); //主线程t,n=3
thread t2(func1);//子线程t2,n=2
thread t3(func1);//子线程t3,n=2
/* 主线程等待该子线程执行完成后再继续执行。 */
/* 子线程结束后由主线程负责回收子线程资源。 */
t2.join();
t3.join();
func2();//主线程t,n=0,n=1
thread t4(func2);//子线程t4,n=0,n=1
thread t5(func2);//子线程t5,n=0,n=1
t4.join();
t5.join();
//system("pause");
return 0;
}
🆚🆚运行结果:
thread number is 140737348192064 n=3
[New Thread 0x7ffff7a53700 (LWP 2977)]
thread number is 140737348187904 n=2
[New Thread 0x7ffff7252700 (LWP 2978)]
[Thread 0x7ffff7a53700 (LWP 2977) exited]
thread number is 140737339795200 n=2
thread number is 140737348192064 m=0
thread number is 140737348192064 m=1
[Thread 0x7ffff7252700 (LWP 2978) exited]
[New Thread 0x7ffff7252700 (LWP 2979)]
thread number is 140737339795200 m=0
thread number is 140737339795200 m=1
[New Thread 0x7ffff7a53700 (LWP 2980)]
[Thread 0x7ffff7252700 (LWP 2979) exited]
thread number is 140737348187904 m=0
thread number is 140737348187904 m=1
[Thread 0x7ffff7a53700 (LWP 2980) exited]
[Inferior 1 (process 2976) exited normally]
——>以上内容是关于C++的基础知识(一),希望对初学者或再次学习者有所帮助,基础打扎实,不怕风吹雨打! 如果以上内容有错误或者内容不全,望大家提出!我也会继续写好每一篇博文!
👍👍👍
待续未完
——文优
🙊🙊🙊
欢迎观看和提问!!!
👏👏👏