📢 声明:
1)本文仅供学术交流,非商用。
2)博主才疏学浅,文中如有不当之处,请各位指出,共同进步,谢谢。
3)此属于第一版本,若有错误,还需继续修正与增删。还望大家多多指点。
4)大家都共享一点点,一起为祖国科研的推进添砖加瓦。
文章目录
👉 学习路线
- 📂 【资源】一个C/C++开发工程师的学习路线(已经无路可退,唯有逆风飞翔)【内附资源页】
- 💻 【GitHub】Cpp-Prime5 + Cpp-Primer-Plus6 源代码和课后题
- 📚 《C++ Primer》学习笔记/习题答案 总目录
〇、✏ 前言
为了更好地记录学过的C++知识,作者在过去的两个月里,将每个知识点对应的小例子总结下来,分享给大家 💖,希望大家喜欢 😁。
下面开始正文,全程英文注释:
一、🚀 你好,世界
牙牙学语的开始。
#include <iostream>
int main()
{
using namespace std;
cout << "Hello,World" << endl;
system("pause");
return 0;
}
二、🍄 数据类型
int、short、long、long long、char、float、double、long double
的位数,最大值,最小值。
#include <iostream>
#include <climits>
int main()
{
using namespace std;
int n_int = INT_MAX; // initialize n_int to max int value
short n_short = SHRT_MAX; // symbols defined in climits file
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
char n_char = CHAR_MAX;
float n_float = FLT_MAX;
double n_double = DBL_MAX;
long double n_ldouble = LDBL_MAX;
// sizeof operator yields size of type or of variable
cout << "Sizeof:" << endl;
cout << "int is " << sizeof (int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << "char is " << sizeof n_char << " bytes." << endl;
cout << "float is " << sizeof n_float << " bytes." << endl;
cout << "double is " << sizeof n_double << " bytes." << endl;
cout << "long double is " << sizeof n_ldouble << " bytes." << endl;
cout << endl;
cout << "Maximum values:" << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl;
cout << "char is " << n_char << endl;
cout << "float is " << n_float << endl;
cout << "double is " << n_double << endl;
cout << "long double is " << n_ldouble << endl;
cout << endl;
cout << "Minimum values:" << endl;
cout << "int: " << INT_MIN << endl;
cout << "short: " << SHRT_MIN << endl;
cout << "long: " << LONG_MIN << endl;
cout << "long long: " << LLONG_MIN << endl;
cout << "char is " << CHAR_MIN << endl;
cout << "float is " << FLT_MIN << endl;
cout << "double is " << DBL_MIN << endl;
cout << "long double is " << LDBL_MIN << endl;
cout << endl;
cout << "Bits per byte = " << CHAR_BIT << endl;
system("pause");
return 0;
}
三、🍅 算术运算符
加法、减法、乘法、除法、求模。
#include <iostream>
int main()
{
using namespace std;
int hats, heads;
cout << "Enter a number: ";
cin >> hats;
cout << "Enter another number: ";
cin >> heads;
cout << "hats = " << hats << "; heads = " << heads << endl;
cout << "hats + heads = " << hats + heads << endl;
cout << "hats - heads = " << hats - heads << endl;
cout << "hats * heads = " << hats * heads << endl;
cout << "hats / heads = " << hats / heads << endl;
cout << "hats % heads = " << hats % heads << endl;
system("pause");
return 0;
}
四、🍆 复合类型
4.1、🍉 一维数组、字符串、string
#include <iostream>
#include <cstring>
#include <string> // make string class available
const int Size = 15;
int main()
{
using namespace std;
int yams[3]; // creates array with three elements
yams[0] = 7; // assign value to first element
yams[1] = 8;
yams[2] = 6;
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << "." << endl;
cout << "The second yams = ";
cout << yams[1] << ".";
cout << "\nSize of yams array = " << sizeof yams;
cout << " bytes.\n";
cout << endl;
cout << "********************************分割线********************************" << endl;
cout << endl;
char name[Size] = "C++owboy"; // initialized array
cout << "Well, " << name << ", your name ";
cout << "is stored in an array of " << sizeof(name) << " bytes.\n";
cout << "Your initial is " << name[0] << ".\n";
name[3]