文章目录
1.c++ 程序结构
关键字
关键字事程序保留的,程序员不能使用,c++的常见关键字如下图:
标识符、操作符、标点
:: 这个也是操作符,不是标点。
预处理指令
注释
main 主函数
一个程序只能有一个入口。
代码练手:
#include<iostream>
using namespace std;
int main(){
cout << "hello" << endl;
return 0;
}
代码练手
#include<iostream>
using namespace std;
//argc 参数数量
//argv 参数列表
int main(int argc,char** argv){
cout << "参数数量:" << argc << endl;
cout << "==== 参数列表 =====" << endl;
for (int i = 0;i < argc; i ++){
cout << "参数:" <<argv[i] << endl;
}
return 0;
}
命名空间
想不发生冲突,还是直接std::cout,比较好一点。
2.c++ 变量和常量
变量
变量名实际上就是你的内存地址。只不过对于不同的对象就是在堆上还是在栈上。因为是一块地址,所以是可以变化的,想放什么就放什么。
变量的初始化。
代码演示:
#include<iostream>
using namespace std;
int main(){
int age;
cout << "age is : " << age << endl;
return 0;
}
如果变量没有初始化,会触发警告。如下图。警告不影响运行,但是最好都要做初始化。
。
代码练手,计算房子面积:
#include<iostream>
using namespace std;
int main(){
int room_width {0};
cout << "请输入房间宽度:" ;
cin >> room_width;
int room_height {0};
cout << "请输入房间高度" ;
cin >> room_height;
cout << "=====================" << endl;
cout << "房间的面积是:" << room_height * room_width << endl;
return 0;
}
数据基本类型都有哪些:
附上ASCII编码表:
变量声明的时候,因为变量是有大小限制的,如果声明的超过了范围,使用花括号,就会报错。如下图,这也是花括号声明的好处之一。
代码练手:
#include<iostream>
using namespace std;
int main(){
cout << "=====字符型" << endl;
char my_char {'f'};
cout << "字符:" << my_char << endl;
cout << "=====短整型" << endl;
short my_short {59};
cout << "短整型:" << my_short << endl;
// cout << "======浮点数======" << endl;
// 不会报错
short overflow_num_1 = 32769;
cout << "短整型溢出" << overflow_num_1 << endl;
// short overflow_num_2 {32768}; // 会报错,无法编译
// cout << "短整型溢出" << overflow_num_2 << endl;
cout << "########int#######" << endl;
int my_height {182};
cout << "int类型:" << my_height << endl;
long peolple {10360000};
cout << "杭州人数:" << peolple << endl;
long long people_in_world {80'0000'0000}; // 方便阅读 c++14标准
cout << "全世界的人数:" << people_in_world << endl;
//浮点型
cout << "=======浮点型=======" << endl;
float book_price {24.21f};
cout << "书的价格" << book_price << endl;
double pi {3.14159};
cout << "圆周率:" << pi << endl;
cout << "#######bool#########" << endl;
bool add_to_cart {true};
cout << boolalpha; // 以bool值的形式输出
cout << "是否加入购物车:" << add_to_cart << endl;
return 0;
}
- sizeof 和climits
代码示例:
#include<iostream>
using namespace std;
// 想要使用看大小的函数,需要引入climits
#include<climits>
int main(){
cout << "char:" << sizeof(char) << endl;
cout << "short:" << sizeof(short) << endl;
cout << "int:" << sizeof(int) << endl;
cout << "long:" << sizeof(long) << endl;
cout << "long long:" << sizeof(long long) << endl;
cout << "float:" << sizeof(float) << endl;
cout << "double:" << sizeof(double) << endl;
cout << " min and max" << endl;
cout << "char min:" << CHAR_MIN << ",max:" <<CHAR_MAX << endl;
cout << "short min:" << SHRT_MIN << ",max:" <<SHRT_MAX << endl;
cout << "long long min:" << LLONG_MIN << ",max:" << LLONG_MAX << endl;
cout << "使用变量名称看大小" << endl;
int age {11};
cout << "age is : " << sizeof age << endl;
cout << "age is : " << sizeof(age) << endl;
double salary {123123.44};
cout << "salary is : " << sizeof(salary) << endl;
return 0;
}
- 常量
代码练手:
#include<iostream>
using namespace std;
int main(){
const double pi {3.1415926};
cout << "请输入半径:" ;
double radius {};
cin >> radius;
cout << "圆的面积:" << pi * radius * radius << endl;
}
3.c++ 数组和容器
注意:访问的超出范围会报错。
代码演示:
#include<iostream>
using namespace std;
int maopao(int array[]){
// 写一段冒泡排序的代码
return 0;
}
int main(){
char vowels[] {'a','e'};
cout << "第1个元素:" << vowels[0] << endl;
cout << "第2个元素:" << vowels[1] << endl;
// cin >> vowels[2];
// cout << "第3个元素:" << vowels[2] << endl;
double hi_tmps [] {100,101,102,103};
hi_tmps[0]= 200;
cout << "第五天的温度:" << hi_tmps[4] << endl; // 放到到一个未知的地址空间,数据每次都不同
int student_score[5];
cout << "第一个学生的成绩是:" << student_score[0] << endl;
cout << "第二个学生的成绩是:" << student_score[1] << endl;
cout << "第三个学生的成绩是:" << student_score[2] << endl;
cout << endl;
cin >> student_score[0];
cin >> student_score[1];
cin >> student_score[2];
cin >> student_score[3];
cin >> student_score[4];
cout << "第一个学生的成绩是:" << student_score[0] << endl;
cout << "第二个学生的成绩是:" << student_score[1] << endl;
cout << "第三个学生的成绩是:" << student_score[2] << endl;
cout << "第四个学生的成绩是:" << student_score[3] << endl;
cout << "第五个学生的成绩是:" << student_score[4] << endl;
cout << "数组的名称是:" << student_score << endl; // 数组的名称是数组的首地址
cout << "数组的名称是:" << *student_score << endl; // 直接用指针指一下,输出的就是第一个数值
cout << "定义一个二维数组" << endl;
int array_2d[3][4] {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
cout << "第一行第一列的值是:" << array_2d[0][0] << endl;
cout << "第3行第2列的值是:" << array_2d[2][1] << endl;
cout << endl;
return 0;
}
- 容器
代码练手:
#include<iostream>
#include<vector>
using namespace std;
int main(){
// vector<char> vowels;
// vector<char> vowels2 (5);
// cout << "第一个元素是:" << vowels2[0] << "\n";
// vector<char> vowels {'a', 'e', 'i', 'o', 'u'};
// cout << "第一个元素是:" << vowels[0] << "\n";
// cout << "第二个元素是:" << vowels[1] << "\n";
// vector<int> test_scores (3);
// cout << "第一个元素是:" << test_scores[0] << "\n";
// vector<int> students_socre(3,100);
// cout << "第一个元素是:" << students_socre[0] << "\n";
// vector<int> students_socre {100, 98, 89};
// cout << "array方式访问:" << endl;
// cout << "三个元素分别是:" << students_socre[0] << " " << students_socre[1] << " " << students_socre[2] << endl;
// cout<< "======================" << endl;
// cout << "vector方式访问:" << endl;
// cout << "三个元素是:" << students_socre.at(0) << " " << students_socre.at(1) << " " << students_socre.at(2) << endl;
// // 添加元素
// cout << "======================" << endl;
// int add_new_value {0};
// cout << "请输入一个新的值:";
// cin >> add_new_value;
// students_socre.push_back(add_new_value); // 添加到最后一个元素
// cout << "在添加一个新的值:" ;
// cin >> add_new_value;
// students_socre.push_back(add_new_value); // 添加到最后一个元素
// cout << "添加后一共有多少个元素 :" << students_socre.size() << endl;
// cout << "第一个元素是:" << students_socre.at(0) << endl;
// cout << "第二个元素是:" << students_socre.at(1) << endl;
// cout << "第三个元素是:" << students_socre.at(2) << endl;
// cout << "第四个元素是:" << students_socre.at(3) << endl;
// cout << "最后一个元素是:" << students_socre.at(students_socre.size() - 1) << endl;
// cout << "获取不存在的元素:" << students_socre.at(10) << endl; // 报错
// cout << "获取不存在的元素:" << students_socre[10] << endl; // 不报错,显示0
cout << "======================" << endl;
vector<vector<int>> movie_ratings {
{1, 2, 3, 4},
{1, 2, 4, 4},
{1, 3, 4, 5}
};
cout << "数组风格的第一个电影第一个评分是:" << movie_ratings[0][0] << endl;
cout << "vector风格的第一个电影第一个评分是:" << movie_ratings.at(0).at(0) << endl;
cout << "第三个电影的第四个评分是:" << movie_ratings.at(2).at(3) << endl;
cout <<endl;
return 0;
}
4.c++ 程序流程
if代码练手:
#include<iostream>
using namespace std;
int main(){
int input_num {0};
const int lower_limit {10};
const int upper_limit {100};
cout << "Enter a number: ";
cin >> input_num;
if(input_num > lower_limit){
cout << "\nNumber is greater than or equal to " << lower_limit << ",大" << (input_num - lower_limit) <<endl;
}
if (input_num < upper_limit){
cout << "\nNumber is less than or equal to " << upper_limit << ",小" << (upper_limit - input_num) << endl;
}
if (input_num > lower_limit && input_num < upper_limit){
cout << "\nNumber is in range " << endl;
}
if (input_num == lower_limit || input_num == upper_limit){
cout << "\nNumber is on the boundary" << endl;
}
}
if-esle代码练手
#include<iostream>
using namespace std;
int main(){
int num {0};
const int target_num {10};
cout << "请输入一个数字:";
cin >> num;
if (num <= target_num){
cout << "你输入的数字小于等于目标数字" << endl;
}else{
cout << "你输入的数字大于目标数字" << endl;
}
return 0;
}
switch代码练手:
#include <iostream>
using namespace std;
int main(){
char input_grade {};
cout << " 请输入你的成绩:";
cin >> input_grade;
switch (input_grade)
{
case 'a':
case 'A':
cout << "优秀!";
break;
case 'b':
case 'B':
cout << "良好!";
break;
default:
break;
}
return 0;
}
switch配合枚举练手
#include <iostream>
using namespace std;
int main() {
enum Traffic_light {red,yellow,green};
Traffic_light light_color {red};
switch(light_color){
case red:
cout << "红灯停" << endl;
break;
case yellow:
cout << "黄灯亮" << endl;
break;
case green:
cout << "绿灯行" << endl;
break;
default:
cout << "未知状态" << endl;
break;
}
return 0;
}
for循环练手
#include<iostream>
#include<vector>
using namespace std;
int main(){
// for (int i {0}; i < 10; i++)
// {
// cout << i << endl;
// }
// for(int i {0};i<10;i+=2){
// cout << i << endl;
// }
// for(int i {10};i>0;i--){
// cout << i << endl;
// }
// for(int i {1};i<=10;i++){
// if(i % 2 == 0){
// cout << i << "是偶数!" << endl;
// }else{
// cout << i << "是奇数!" << endl;
// }
// }
// for (int i {1},j {5}; i <= 5 ; ++i,++j)
// {
// /* code */
// cout << i << "*" << j << "=" << i * j << endl;
// }
vector<int> nums {10,20,30,40,50};
for (unsigned i = 0; i < nums.size(); i++)
{
/* code */
cout << nums[i] << endl;
}
return 0;
}
类似java中的增强for循环。
for循环练手:
#include<iostream>
#include<vector>
// 设置浮点精度
#include <iomanip>
using namespace std;
int main() {
int students_scores [] {100, 90, 80};
for (auto score: students_scores) {
cout << score << endl;
}
vector<double> temps {22,23,24,25,26,27.1,28};
double tmp_average {0};
double tmp_total {0};
for (auto tmp: temps) {
tmp_total+=tmp;
}
tmp_average = tmp_total / temps.size();
cout << fixed << setprecision(2);
cout << "Average temperature is: " << tmp_average << endl;
for(auto i : {1,2,3,4,5,6,7,8,9,10}){
cout << i << endl;
}
for(auto i : "this is a test"){
cout << i ;
}
cout << endl;
return 0;
}
do-while练手:
#include<iostream>
using namespace std;
int main(){
char intput_char {};
do
{
/* code */
cout << endl;
cout << "菜单选项" << endl;
cout << "1.查看记录" << endl;
cout << "2.修改记录" << endl;
cout << "3.新增记录" << endl;
cout << "请输入q或者Q,退出!" << endl;
cout << "请输入你的选择:";
cin >> intput_char;
switch (intput_char)
{
case '1':
cout << "查看记录" << endl;
break;
case '2':
cout << "修改记录" << endl;
break;
case '3':
cout << "新增记录记录" << endl;
break;
case 'q':
case 'Q':
cout << "退出" << endl;
break;
default:
cout << "输入错误!" << endl;
break;
}
} while (intput_char!= 'q' && intput_char!='Q');
return 0;
}
5.c++字符和字符串
5.1 c风格字符串
cstr练手:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char first_name[20] {};
char last_name[20] {};
char full_name[50] {};
char temp[50] {};
// cout << "请输入你的姓氏:";
// cin >> first_name;
// cout << "请输入你的名:";
// cin >> last_name;
// cout << "=========================" << endl;
// cout << "您的姓:" << first_name << " 一共有" << strlen(first_name) << "个字符" << endl;
// cout << "您的名:" << last_name << " 一共有" << strlen(last_name) << "个字符" << endl;
// strcpy(full_name,first_name);
// strcat(full_name," ");
// strcat(full_name,last_name);
// cout << "您的全名是:" << full_name << endl;
// cout << "=========================" << endl;
// 这种方法遇到空格就拆分了,识别成2个参数
// cout << "请输入你的全名:";
// cin >> full_name;
// cout << "您的全名是:" << full_name << endl;
cout << "请输入你的全名:";
cin.getline(full_name,50);
cout << "您的全名是:" << full_name << endl;
// cout << "=========================" << endl;
// strcpy(temp,full_name);
// if (strcmp(temp,full_name) == 0)
// {
// cout << temp << "和" << full_name << "是相同的" << endl;
// }
// else
// {
// cout << temp << "和" << full_name << "是不相同的" << endl;
// }
for (size_t i {0}; i < strlen(full_name); i++) {
/* code */
if (isalpha(full_name[i])){
full_name[i] = toupper(full_name[i]);
}else{
full_name[i] = '#';
}
}
cout << "转换之后:" << full_name << endl;
return 0;
}
5.2 c++风格字符串
注意:2个常量相加赋值给string,会报错。但是2个引用相加,再加上字面量就没问题。
注意:字符在存储的时候,使用的是ascii编码。
c++ 风格字符串练手
#include<iostream>
#include<string>
using namespace std;
int main(){
// 声明和初始化
// string s1; // 默认初始化,s1是一个空串
// string s2 {"hello"}; // 初始化为字符串字面值
// string s3 {s2}; // 初始化为s2的副本 拷贝初始化
// string s4 {"hello", 3}; // 初始化为字符串字面值的前3个字符
// string s5 {s3, 1, 3}; // 初始化为s3的第1个字符开始的3个字符
// string s6 (10, 'c'); // 初始化为10个字符c的串
// // 读取字符串
// cout <<"s1 : " << s1 << endl;
// cout <<"s2 : " << s2 << endl;
// cout <<"s3 : " << s3 << endl;
// cout <<"s4 : " << s4 << endl;
// cout <<"s5 : " << s5 << endl;
// cout <<"s6 : " << s6 << endl;
// 赋值
// string s1 = "hello";
// cout << "s1:" << s1 << endl;
// string s2 {"course"};
// cout << "s2:" << s2 << endl;
// s2 = s1;
// cout << "s2:" << s2 << endl;
// 拼接
// string part1 {"c++"};
// string part2 {"是一门强大的"};
// string sentence = part1 + part2 + "语言";
// cout << "sentence:" << sentence << endl;
// sentence = "c++" + "语言";
// 获取字符,更新
// string s1 {"hello"};
// cout << s1[0] << endl;
// cout << s1.at(0) << endl;
// s1[0] = 'H';
// s1[4] = 'x';
// cout << s1 << endl;
string s1 {"hello"};
for (auto c: s1)
{
cout << c << endl;
}
for (int c: s1)
{
cout << c << endl;
}
return 0;
}