学习方法:理论-实践-经验-理论-实践-经验······
正文:
1.输入、输出、数组使用
涉及知识点:cin和 cout
数组的声明创建
sizeof 获取数组长度
strlen 获取字符串长度
int main()
{
using namespace std;
const int Size = 15;
char name1[Size];
char name2[Size] = "C++owboy";
cout << "Howdy! I am " << name2 << "What is your name?\n";
cin >> name1;
cout << "Well, " << name1 <<", your name has";
cout << strlen(name1) << " letters and is stored \n";
cout << "in an array of " << sizeof(name1) << " bytes. \n";
cout << "Your initial is " << name1[0] << ".\n";
name2[3] = '\0'; //使用【\0】截断字符,之后的都被忽略
cout << "Hear are the first 3 characters of my name: ";
cout << name2 << endl;
return 0;
}
2.cin.getline()
每次读取一行数据。避免出现cin读取到一个空格、制表和换行就结束的情况
int main()
{
using namespace std;
const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];
cout << "Enter your name:\n";
cin.getline(name, Arsize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert, Arsize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
//Enter your name:
//Fish man
//Enter your favorite dessert:
//cake
//I have some delicious cake for you, Fish man.
3.str的使用,对比c数组
c++的string对象使用与数组用法大致相同,更加方便和安全。
int main() {
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
cout << "Enter a kind of feline: ";
cin >> charr1;
cout << "Enter another kind of feline: ";
cin >> str1;
cout << "Here are some felines: \n";
cout << charr1 << " " << charr2 << " " << str1 << " " << str2 << endl;
cout << "The third letter in " << charr2 << " is " << charr2[2] << endl;
cout << "The third letter in " << str2 << " is " << str2[2] << endl;
string animal = {"The Big"};
cout << "animal:" << animal << endl;
//字符串拼接
string dog = animal + " dog";
cout << "dog:" << dog << endl;
//数组长度
cout << "size: " << dog.size() << endl;
return 0;
}
Enter a kind of feline:cat
Enter another kind of feline:lion
Here are some felines:
cat jaguar lion panther
The third letter in jaguar is g
The third letter in panther is n
animal:The Big
dog:The Big dog
size: 11
4.结构的使用
int main() {
struct student {
std::string name;
float height;
double score;
};
student s1[] =
{
{"Tom", 1.7, 98},
{"Amy", 1.9, 99}
};
std::cout << "student s1:"
<< s1[0].name << ", "
<< s1[0].height << ", "
<< s1[1].score << std::endl;
return 0;
}
//输出:student s1:Tom, 1.7, 998
5.共用体union的使用
常用于节省内存。其长度是最大成员的长度,每次只能存一个值。
int main() {
using namespace std;
struct student {
string name;
//成绩可以是1~100的分数,或‘A’,‘B’,'C'
union score {
int score_num;
char score_char[1];
} score_val;
};
student st1;
st1.name = "小明";
st1.score_val.score_num = 100;
cout << "st1 name: " << st1.name << "score: " << st1.score_val.score_num << endl;
//score修改成了score_char类型的‘A’,原来的100失效
st1.score_val.score_char[0]= 'A';
cout << "st1 name: " << st1.name << "score: " << st1.score_val.score_char[0] << endl;
return 0;
}
//st1 name: 小明score: 100
//st1 name: 小明score: A
6.枚举的使用
默认不指定值从0开始,每次加1。指定值后的也是每次自增1
enum Level {
ZhuJi = 1,LianQi = 2,YuanYin = 3,JinDan = 4,
DaCheng = 1000,
FaXian, //1001
ZhiZun, //1002
DiZun, //1003
ShenWang //1004
};
7.指针的使用,指针分配内存,释放内存
int dog = 6;
double price = 21.5;
int *d = &dog;
cout << &dog << ", " << &price << ", " << *d << endl;
int *cat = new int;//分配合适的内存
*cat = 99;
cout << "*cat: " << *cat;
delete cat;//释放内存
8.动态数组的创建,赋值,使用和释放
using namespace std;
auto *money = new double[3];
money[0] = 12.12;
money[1] = 25.5;
money[2] = 9.98;
cout << "print money: " << money[0] << ", " << money[1] << ", " << money[2] << endl;
delete[]money;
return 0;
9.指针数组和运算
数组名==数组首元素的地址==数组的地址
int main() {
using namespace std;
double wages[3] = {10000.0, 20000.0, 30000.0};
double *pw = wages;
cout << " 指针所指向的地址pw,*pw:" << pw << ", " << *pw << endl;//---:0x78553ffe40, 10000
cout << " 数组名指向的地址wages[]:" << wages << endl;//---0x78553ffe40
cout << " 首元素指向的地址wages[0]:" << wages[0] << endl;//---10000
pw = pw + 1;
cout << " 指针加1后的地址:pw,*pw:" << pw << ", " << *pw << endl;//---0x78553ffe48, 20000
return 0;
}
10.用new创建动态结构
两种方式赋值,(*指针名).xxx 或者指针名->属性名
using namespace std;
struct inf {
string name;
float volume;
double price;
};
inf *ps = new inf;
(*ps).name = "John";
ps->price = 200.01;
ps->volume = 20.87;
cout << "Hi," << ps->name << ",it is:"
<< (*ps).volume << ", "
<< (*ps).price << endl;
//Hi,John,it is:20.87, 200.01
delete ps;
return 0;
11.c++管理数据内存的方式:
自动存储:在函数内定义的常规变量,即自动变量,使用自动存储空间。存储在栈中,后进先出。
在函数被调用时产生,函数结束后自动释放。
静态存储:函数外定义的变量,或使用static修饰。
动态存储:使用new和delete手动控制,更加灵活。
12.数组Array和模板类Vector的使用
array<typeName, n_elem> arr;
vector<typeName> vt (n_elem);
其中typeName表示类型名称,n_elem代表元素个数
vector<int> vt1(3);
vt1[0] = 100;
vt1[1] = 200;
vt1[2] = 300;
array<double, 4> array1 = {10.5, 21.1, 9.99, 5.20};
13.前向引用声明
在引用未定义的类之前,把该类的名字告诉编译器,告诉它这是一个类名,不会报错。
class MyString; //在头文件处就用class关键字声明
int main() {
return 0;
}
//具体类定义在下面
class MyString{
string s;
}