1.异常在类层次间的使用
//案例:设计一个数组类 MyArray,重载[]操作,
// 数组初始化时,对数组的个数进行有效检查
// index<0 抛出异常eNegative
// index = 0 抛出异常 eZero
// 3)index>1000抛出异常eTooBig
// 4)index<10 抛出异常eTooSmall
// 5)eSize类是以上类的父类,实现有参数构造、并定义virtual void printErr()输出错误。
#include <iostream>
using namespace std;
class MyArray
{
public:
MyArray(int len);
// 在类的内部定义一个类,叫内部类
class eSize
{
public:
eSize(int size)
{
this->size = size;
}
virtual void printErr() = 0;
protected:
int size;
};
class eNegative:public eSize
{
public:
eNegative(int size):eSize(size)
{
}
virtual void printErr()
{
printf ("数组长度不能 小于0,当前长度:%d\n", size);
}
};
class eZero:public eSize
{
public:
eZero(int size):eSize(size)
{
}
virtual void printErr()
{
printf ("数组长度不能 等于0,当前长度:%d\n", size);
}
};
class eTooBig:public eSize
{
public:
eTooBig(int size):eSize(size)
{
}
virtual void printErr()
{
printf ("数组长度不能 大于1000,当前长度:%d\n", size);
}
};
class eTooSmall:public eSize
{
public:
eTooSmall(int size):eSize(size)
{
}
virtual void printErr()
{
printf ("数组长度不能 小于10,当前长度:%d\n", size);
}
};
};
MyArray::MyArray(int len)
{
if (len < 0)
throw eNegative(len);
else if (len == 0)
throw eZero(len);
else if (len > 1000)
throw eTooBig(len);
else if (len < 10)
throw eTooSmall(len);
}
int main()
{
try
{
MyArray a(1001);
}
catch (MyArray::eSize &e)
{
e.printErr(); // 多态
}
return 0;
}
2.标准异常库
#include <iostream>
#include <stdexcept>
using namespace std;
class MyException:public exception
{
public:
MyException(char *msg)
{
this->msg = msg;
}
virtual const char * what() const
{
return msg;
}
private:
char *msg; // 存异常信息
};
class Student
{
public:
Student(int age)
{
if (age > 200)
// throw out_of_range("年龄太大");
throw MyException("我的异常类对象");
this->age = age;
}
private:
int age;
};
int main()
{
try
{
Student a(250);
}
catch (exception &e)
{
printf ("捕获一个异常: %s\n", e.what());
}
return 0;
}
3。标准输入流
#include <iostream>
using namespace std;
int main1()
{
int a;
int b;
int c;
cin >> a >> b >> c;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
return 0;
}
int main2()
{
//char str[100];
//cin >> str;
//cout << "str = " << str << endl;
char ch;
int count_space = 0;
int count_letter = 0;
int count_num = 0;
// cin.get() 读取一个字符,结束标志 EOF(Ctrl+z)
while ((ch=cin.get())!= EOF)
{
if (ch == ' ')
count_space++;
else if(ch >= '0' && ch <= '9')
count_num++;
else if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
count_letter++;
}
cout << "空格: " << count_space << endl;
cout << "字母: " << count_letter << endl;
cout << "数字: " << count_num << endl;
return 0;
}
int main3()
{
char str[100];
// fgets(str, sizeof(str), stdin);
cin.getline(str, sizeof(str));
cout << str << endl;
return 0;
}
// cin.ingore(n) 忽略缓冲区中n个字符
int main4()
{
int a;
cin.ignore(4);
cin >> a;
cout << "a = " << a << endl;
return 0;
}
int main()
{
int a = 0x123456;
cout << hex << a << endl;
return 0;
}
4.文件输入输出流
#include <iostream>
#include <fstream>
using namespace std;
// 写文件
void func1()
{
cout << "hello world" << endl;
// 写文件
// 定义一个输出文件流对象, 关联一个文件
// 关联成功,相当于打开了一个文件,之后文件流对象的操作相当于对文件的操作
ofstream fout("test.txt");
if (!fout) // ofstream 重载了!运算符,如果打开失败,返回真
cout << "文件打开失败" << endl;
fout << "hello world1111111111111" << endl;
fout << "hello world2222222222222" << endl;
fout << "hello world3333333333333" << endl;
fout << "hello world4444444444444" << endl;
int a = 10;
fout << "a = " << a << endl;
// 文件关闭
fout.close();
}
// 读文件
void func2()
{
ifstream fin("test.txt");
if (!fin)
cout << "文件打开失败" << endl;
char str[100];
for (int i = 0; i < 4; i++)
{
fin.getline(str, sizeof(str));
cout << str << endl;
}
fin.ignore(4);
int a;
fin >> a;
cout << a << endl;
//char ch;
//while ((ch=fin.get()) != EOF)
// cout << ch;
fin.close();
}
// 二进制文件读写
class Student
{
public:
Student(int id, char *name)
{
this->id = id;
strcpy(this->name, name);
}
void show()
{
printf ("id = %d, name = %s\n", id, name);
}
private:
int id;
char name[20];
};
// 写 二进制文件
void func3()
{
Student s1(1, "小明1");
Student s2(2, "小明2");
Student s3(3, "小明3");
ofstream fout("student", ios::binary);
if (!fout)
cout << "文件打开失败" << endl;
fout.write((char *)&s1, sizeof(Student));
fout.write((char *)&s2, sizeof(Student));
fout.write((char *)&s3, sizeof(Student));
fout.close();
}
// 读二进制文件
void func4()
{
ifstream fin("student", ios::binary);
if (!fin)
cout << "文件打开失败" << endl;
Student s(1133, "133");
for (int i = 0; i < 3; i++)
{
fin.read((char *)&s, sizeof(Student));
s.show();
}
fin.close();
}
int main()
{
func1();
func2();
func3();
func4();
return 0;
}
5.算法容器迭代器
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void func()
{
// 容器:用来存储数据
// 定义一个 容器对象
vector<int> v;
v.push_back(1);
v.push_back(12);
v.push_back(3);
v.push_back(8);
v.push_back(2);
v.push_back(19);
// 迭代器:提供一种同一的方式遍历容器
// 大部分容器都有自己的迭代器类型
// 定义一个迭代器对象
// 拥有迭代器的容器都有两个函数:
// begin():返回指向当前容器第一个元素的迭代器
// end() :返回指向当前容器最后一个元素的下一个元素的迭代器
// 迭代器的用法类似指针,当作指针用也可以
vector<int>::iterator it = v.begin();
//for (; it != v.end(); it++)
//{
// cout << *it << endl;
//}
while (it != v.end())
{
cout << *it << endl;
it++;
}
// 算法:操作数据
// 1、排序
sort(v.begin(), v.end());
cout << "排序后:" << endl;
it = v.begin();
while (it != v.end())
{
cout << *it << endl;
it++;
}
reverse(v.begin(), v.end());
cout << "逆序后:" << endl;
it = v.begin();
while (it != v.end())
{
cout << *it << endl;
it++;
}
}
int main()
{
func();
return 0;
}
6,string
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// string 的构造
void func1()
{
string s1("123");
string s2 = "abc";
cout << s1 << ", " << s2 << endl;
string s3 = s1; // 拷贝构造
string s4;
s4 = s2; // 赋值
cout << s3 << ", " << s4 << endl;
string s5(10, 'a');
cout << s5 << endl;
}
// 2、string 的遍历
void func2()
{
string s1 = "hello world";
// 1、通过数组下标进行遍历
for (unsigned int i = 0; i < s1.length(); i++)
cout << s1[i];
cout << endl;
// 2、通过 at() 函数进行遍历
for (unsigned int i = 0; i < s1.length(); i++)
cout << s1.at(i);
cout << endl;
// 3、string 也有迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;
it++;
}
cout << endl;
// 4、[] 和 at 函数的区别
// [] 如果访问越界,终止程序运行,不会抛出异常
// at 如果访问越界,会抛出一个异常
try
{
for (unsigned int i = 0; i < s1.length()+5; i++)
// cout << s1[i];
cout << s1.at(i);
cout << endl;
}
catch(exception &e)
{
printf ("抓到一个异常: %s\n", e.what());
}
}
// 3、string 和 char *的转换
void func3()
{
string str = "123456";
// string ----> char * c_str()
printf ("Str = %s\n", str.c_str());
}
// 4、字符串的连接、拷贝、比较
void func4()
{
string s1 = "hello ";
string s2 = "world";
s1 = s1 + s2;
cout << s1 << endl;
s1 = "111 " + s1 + " 222";
cout << s1 << endl;
s1.append("abcdefg");
cout << s1 << endl;
// s1 = s2;
char str[100] = {0};
s1.copy(str, 6, 1);
cout << str << endl;
if (s1 == s2)
cout << "s1 == s2" << endl;
else
cout << "s1 != s2" << endl;
}
// 5、查找和替换
void func5()
{
string s1 = "111 hello 222 hello 333 hello 444 hello 555 hello";
// find 参数
// 第一个参数是要查找的子串
// 第二个参数是查找的位置下标
// 返回值:如果存在则返回第一个子串的下标,不存在返回-1
int index = s1.find("hello", 0);
if (index == -1)
cout << "不存在子串" << endl;
else
cout << "存在" << endl;
// 替换:先删除,再插入
// 第一个参数 是要替换的起始位置下标
// 第二个参数 要删除的个数
// 第三个参数 是要出入的字符串
s1.replace(4, 1, "abc");
cout << s1 << endl;
index = s1.find("hello", 0);
while (index != -1)
{
s1.replace(index, 5, "HELLO");
index = s1.find("hello", index+1);
}
cout << s1 << endl;
}
// 6、删除和插入
void func6()
{
string s1 = "111 hello 222 hello 333 hello 444 hello 555 hello";
// 1、通过迭代器
// 删除单个元素
s1.erase(s1.begin());
cout << s1 << endl;
// 删除多个元素: 左闭右开 [s1.begin(), s1.begin()+3)
s1.erase(s1.begin(), s1.begin()+3);
cout << s1 << endl;
//s1.erase(s1.begin(), s1.end());
//cout << s1 << endl;
// 2、通过下标:类似替换,只是不再插入
s1.erase(1,2);
cout << s1 << endl;
// 插入
// 第一个元素是插入位置的下标,第二个是要插入的元素
s1.insert(0, "111");
s1.insert(5, "abc");
s1.insert(s1.length(), "ZZZZ");
cout << s1 << endl;
}
// 7、可以用算法函数操作字符串
void func7()
{
string s1 = "111 hello 222 hello 333 hello 444 hello 555 hello";
// 转换算法
transform(s1.begin(), s1.end(), s1.begin(), toupper);
cout << s1 << endl;
transform(s1.begin(), s1.end(), s1.begin(), tolower);
cout << s1 << endl;
reverse(s1.begin(), s1.end());
cout << s1 << endl;
}
void func8()
{
string s = "You are from shanghai";
reverse(s.begin(), s.end());
int begin = 0;
int index = s.find(" ");
while (index != -1)
{
reverse(s.begin()+begin, s.begin()+index);
begin = index + 1;
index = s.find(" ", index+1);
}
reverse(s.begin()+begin, s.end());
cout << s << endl;
}
int main()
{
// func1();
// func2();
// func3();
// func4();
// func5();
// func6();
// func7();
func8();
return 0;
}