Review cpp day01

一、第一个C++程序

01first.cpp

#include <iostream>
int main(void){
	std::cout <<"hello world!"<<std::endl;
	return 0;
}

1、两种编译程序方法:

  • gcc 01first.cpp -lstd c++
    ./a.out

  • g++ 01first.cpp//推荐

2、文件扩展名

  • 1).cpp
  • 2).cxx
  • 3).cc
  • 4).c

3、头文件

  • 包含了标准C++中所有和I/O相关的类型、对象和函数
    #include <iostream>

注: 在C++中标准C的头文件依然可以使用

#include <stdio.h>==#include <cstdio>
#include <stdlib.h>==#include <cstdlib>
#include <string.h>==#include <cstring>
……
#include <iostream>
//#include <stdio.h> C语言的风格
#include <cstdio> //C++的风格
int main(void){
	std::cout <<"hello world!"<<std::endl;
	printf("Hello world!\n");
	return 0;
}

4、标准的输入和输出

  • 1)cin对象表示标准输入//类似scanf
    eg:
	int num;
	scanf("%d", &num);//C中
	cin >> num;//C++中
	//其中">>"称为提取操作符

	int i;
	double d;
	scanf("%d%lf", &i, &d);//C
	cin >> i >>d;//C++
  • 2)cout对象表示标准输出//类似printf
    eg:
	int num = 23;
	printf("num = %d\n", num0);//C
	cout << "num = " << num;//C++
	//其中"<<"称为插入操作符
	//注:endl == "\n"
	
	int i = 100;
	double d = 3.14;
	printf("%d,%lf\n", i, d);
	cout << i << ',' << d << endl;

5、"std::"标准名字空间

- C++标准库中的所有函数、变量、类型都是在std空间中

二、名字空间(namespace)******

1、名字空间的作用

  • 1)避免名字冲突
  • 2)划分逻辑单元

2、定义名字空间

namespace 名字空间{
	名字空间的成员1;
	名字空间的成员2;
	名字空间的成员3;
	……
	名字空间的成员n;

}

注: 名字空间成员可以是变量、函数、类型、名字空间
eg:

	namespace ns1{
		int num = 100;
		void func(//……)
		{//……}
		struct student{};
		namespace ns2{};
	}

3、名字空间成员的使用

  • 1)通过作用域限定操作“::”
    空间成员::要访问的成员
    eg:
	int main(void){
		//空间成员不能直接访问
		cout << num << endl;//error

		//通过"::"访问空间里面的成员
		cout << ns1::num << endl;//ok
	}

02namespace.cpp

#include <iostream>
namespace ns1{
	void func(void){
		std::cout << "ns1的func函数" << std::endl;
	}
}
int main(void){
	//func();发生错误
	ns1::func();
	return 0;	
}
  • 2)名字空间指令
    using namespace 名字空间名;
    在该条指令以后的代码中,指定名字空间中的成员都可见,
    可以直接访问,省略"空间名::"
    eg:
	int main(void){
		using namespace ns1;//名字空间指令
		cout << num << end1;//ok
	}

注: 这种方法要谨慎使用,小心不同名字空间中有相同的成员名,容易引发歧义。

常用场景:using namespace std;
以后在使用标准名字空间按里面的成员省略"std::"

#include <iostream>
using namespace std;//标准名字空间指令
int main(void){
	cout <<"hello world!"<<endl;
	return 0;
}
  • 3)名字空间声明
    using 名字空间名::名字空间成员;
    将名字空间中特定的一个成员引入当前作用域,在该作用域访问这个成员
    就如同访问局部的成员一样,可以直接访问,省略"空间名::"
    eg:
	using namespace std;
	int main(void){
		using ns1::num;//名字空间声明
		cout << num << endl;//ok
	}
  • 4)无名名字空间
    不属于任何名字空间的标识符,将被编译器之后放入无名名字空间中,无名名字空间里面的成员正常可以直接访问,但是当无名名字空间的成员和局部的标识符名字一样时,局部优先,这时如果还希望访问无名名字空间的成员,可以使用作用域限制操作符:::要访问的成员

综合例子:

#include <iostream>
using namespace std;
namespace ns1{
	num = 100;
	void func(void){
		cout << "This is ns1's function." << endl;
	}
}

namespace ns2{
	num = 200;
	void func(void){
		cout << "This is ns2's function." << endl;
	}
}

//普通全局变量,将被放入无名的名字空间中
int num = 300;

void main(void){
	cout << num << endl;//300
	using ns1::num;//名字空间声明
	cout << num << endl;//100,体现局部优先原则

	cout << ns2::num << endl;//200

	cout << ::num <<endl;//300,******
	return 0;
}
  • 5)名字空间嵌套//了解
    eg:
	namespace ns1{
		int num = 100;
		namespace ns2{
			int num = 200;
			namespace ns3{
				int num = 300;
			}
		}
	}
	cout << ns1::num << endl;//100
	cout << ns1::ns2::num << endl;//200
	cout << ns1::ns2::num3::num << endl;//300

三、C++的结构体、联合体、枚举

1、C++的结构体

  • 1)定义结构体变量可以省略struct关键字
    eg :
	struct Student{……};
	struct Student s;
	typedef struct Student STU;
	STU s2;
	//------------------------------
	struct Student{……};
	Student s;
  • 2)在C++结构体可以直接定义函数,称为成员函数(也称方法),在成员函数中可以直接访问其他成员。
    eg:
#include <iostream>
using namespace std;
struct Student{
	//成员变量
	char name[200];
	int age;
	//成员函数
	void who(void){
		cout << name << "," << age << endl;
	}
};

int main(void){
	/*struct*/Student s = {"张飞", 28};
	cout << s.name << endl;//张飞
	cout << s.age << endl;//28
	s.who();//张飞,28
	return 0;
}

2、联合体//了解

  • 1)定义联合体变量可以省略union关键字
  • 2)支持匿名联合
    注: 联合体内测成员共享内存
#include <iostream>
#include <cstdio>
using namespace std;

int main(void){
	union{//匿名联合
		//内联成员共享内存
		unsigned int un;
		unsigned char us[4];
	};
	un = 0x12345678;
	for(int i=0; i<4; i++){
		printf("&us[%d]=%p, 0x%x\n", i, &us[i], us[i]);
	}
	return 0;
}

这个程序可以测试系统的大小端

3、枚举

  • 1)定义枚举变量时enum关键字可以省略
  • 2)C++中枚举类型不能当做整型数直接来用
    eg:
    0–>RUN
    1–>SLEEP
    2–>STOP
    enum STATE{RUN, SLEEP, STOP};
    /enum/ STATE s;
    s = 1;//C:ok C++:error
    s = SLEEP;//C:ok C++:ok

07enum.cpp

#include <iostream>
using namespace std;

int main(void){
	enum Color{RED, YELLOW, BLUE};
	cout << RED << "," << YELLOW << "," << BLUE<< endl;//0,1,2
	/*enum*/ Color c;
	c = BLUE;
	cout << c << endl;//2
	return 0;
}

五、字符串******

1、回顾C中的字符串

  • 1)双引号常量字符串:“hello”
  • 2)字符指针:char*
  • 3)字符数组:char[]

2、C++中兼容C的字符串的同时,增加了string类型专门表示字符串。

  • 1)定义字符串
string s;//定义一个空字符串
string s = "hello";//定义字符串同时初始化

//下面两种写法和上面写法等价
string  s = string("hello");
string s("hello");
  • 2)字符串拷贝:=
    string s1 = “hello”;
    string s2 = “jiangguiliang”;
    s1 = s2;//拷贝
    cout << s1 << endl; //“jiangguiliang”

  • 3)字符串连接:+ +=

string s1 = "hello"
string s2 = " world";
string s3 = s1 + s2;
cout << s3 << endl;//hello world

s1 += s2;//s1 = s1 + s2;
cout << s1 << endl;//hello world
  • 4)字符串比较:== != > < >= <=
string s1 = "hello";
if(s1 == "hello"){//1、逻辑真
	……
}

if(s1 > "Jiangguiliang"){//逻辑真
	……
}
  • 5)获取字符串中某个字符:[]
string s1 = "hello";
cout << s1[0] << endl;//'h'
s1[0] = 'H'
cout << s1 << endl;//Hello
  • 6)string类型中常用函数
    • size()/length():获取字符串的长度
    • c_str():将string转换成C中const char*字符串
string s = "hello";
s.size();//5
s.length();//5

const char* p = s.c_str();

eg:08string.cpp

#include <iostream>
#include <cstring>
using namespace std;

int main(void){
	string s = "hello";
	cout << s << endl;//"hello"
	//拷贝
	string s2 = s;
	cout << s2 << endl;//"hello"
	//连接
	s = s + "world";
	cout << s << endl;//"hello world"
	//比较
	cout << (s == s2) << endl;//0
	cout << (s2=="hello") << endl;//1
 	//获取字符串中某个字符
	s[0] = 'H';
	s[6] = 'W';
	cout << s<< endl;//Hello World
	//获取字符串的大小(长度)
	cout << s.size() << endl;//11
	cout << s.length() << endl;//11
	cout << strlen(s.c_str()) << endl;//11 

	return 0;
}
  • 练习:使用string类型,从键盘读取一个字符串,统计该字符中包含多少个字符‘a’;
  • 提示:
    • string str;
    • cin >> str;//从键盘读取每一个字符串

string.cpp

#include <iostream>
using namespace std;

int main(void){
	string str;
	cout << "请输入一个字符串:" << endl;
	//cin >> str;//碰到空白符结束
	getline(cin, str);//碰到回车结束
	int count = 0;
	for(int i=0; i<str.size(); i++){
		if(str[i]  ==  'a'){
			count += 1}
	}
	cout << "字符a的个数是:" << count <<endl;
	return0;
}

六、C++的布尔类型(bool)

  • 1、bool类型是C++中基本的数据类型,专门表示逻辑值
  • 2、bool在内存上占一个字节,1表示逻辑真,0表示逻辑假
  • 3、bool类型的变量可以接受任何表达式的结果,值非零则为true(1),
    值为0则为false(0).

bool.cpp

#include <iostream>
using namespace std;

int main(void){
	bool b = false;
	cout << "size=" << sizeof(b) << endl;//1
	cout << b << endl;//0

	b = 123;
	cout << b << endl;//1
	b = 3.14;
	cout << b << endl;//1
	char* p = NULL;//NULL-->(void *)0
	b = p;
	cout << b << endl;//0
	return 0;
}

七、操作符别名

  • && <=等价=> and
  • || <=等价=> or
  • { <=等价=> <%
  • } <=等价=> %>
    ……

operator.cpp

#include <iostream>
using namespace std;

int main(void)<%
	int a = 1;	
	int b = 0;
	if(a and b)<%
		cout << "true" << endl;
	%>
	else<%
		cout << "false" << endl;
	%>
	return 0;
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值