C++中的类型转换与STL标准模板库
一、C++类型转换
在C++中类型转换包含 static_cast、const_cast、dynamic_cast、reinterpret_cast 四种类型转换。使用原始类型转换,所有转换都是一种写法,可读性不高,有可能有潜在的风险
1、static_cast
该类型转换函数用于不同类型的属性之间的转换
#include <iostream>
using namespace std;
void* func(){
int i = 0;
return &i;
}
void main(){
//自动类型转换
//int i = 0;
//double d = i;
//或
//double d = 9.5;
//int i = d;
// int i = 8;
// double d = 9.5;
// i = static_cast<int>(d);
// cout << "i = " << i << endl;
int i = 9;
int *i_p = &i;
}
void* 转 char*
void* func(int type){
switch(type){
case 1:
int i = 11;
return &i;
case 2:
int a = 'X';
return &a;
default:
return NULL;
}
}
void main(){
//void* 转 char*
//char* c_p = (char*)func(2);
char* c_p = static_cast<char*>(func(2));
//C++
func
//C
}
2、const_cast
该类型转换函数用于去常量,一般使用在常量属性转为非常量属性时使用
void func(char c[]){
//可以修改
c[0] = 'H';
c[1] = 'a';
}
void func2(const char c[]){
//c[0] = 'H'; //不能修改
//c[1] = 'a';//不能修改
//通过指针间接赋值
//其他人并不知道,这次转型是为了去常量
//char* c_p = (char*)c;
//c_p[1] = 'a';
//cout << c << endl;
//C++ 去常量,可读性高
char* c_p = const_cast<char*>(c);
c_p[1] = 'y';
cout << c << endl;
}
void main(){
char c[] = "hello";
func2(c);
getchar();
}
3、dynamic_cast
该类型转换用于父类对象转换为子类时使用,处理父类转为子类时的不确定性(类型不匹配时转型失败,返回NULL)
class Person{
public:
virtual void print(){
cout << "人" <<endl;
}
};
class Man : public Person{
public:
void print(){
cout << "男人" <<endl;
}
void smoking(){
cout << "吸烟" <<endl;
}
};
class Woman : public Person{
public:
void print(){
cout << "女人" <<endl;
}
void makeup(){
cout << "化妆" <<endl;
}
};
void func(Person* obj){
obj->print();
//调用子类的特有函数,需要转为实际类型
//当传入Woman对象时,转型为Man,转型失败,但是编译器并为察觉
//Man* man = (Man*)obj;
//man->print();
//转型失败,返回NULL
//Man* man = dynamic_cast<Man*>(obj);
//man->print();
Man* man = dynamic_cast<Man*>(obj);
if(man!=NULL){
man->smoking();
}
Woman* woman = dynamic_cast<Woman*>(obj);
if(woman!=NULL){
woman->makeup();
}
}
void main(){
Woman w1;
//父类类型的指针指向子类对象
Person* p = &w1;
func(p);
getchar();
}
4、reinterpret_cast
该类型转换用于函数指针之间的转型,在使用函数指针转型时使用
void func1(){
}
char* func2(){
return "abc";
}
typedef void(*f_p)();
void main(){
f_p f_array[6];
f_array[0] = func1;
//C 方式
f_array[1] = (f_p)func2;
f_array[1] = reinterpret_cast<f_p>func2;
getchar();
}
二、IO 流
在 C++ 中,通过<fstream> 头文件中的 ifstream 读取文件,ofstream 写入文件,完成 IO 流操作
1、文本文件操作
对于文本文件,可进行使用 ifstream 中的 get(ch) 读取一个字符;使用 fout << 写入一个字符串
#include <fstream>
void main(){
char fname[] = "D://dest.text";
//输出流
ofstream fout(fname);
//创建失败
if(fout.bad()){
return -1;
}
fout << "Jack" << endl;
fout << "Rose" << endl;
fout.close();
//读取
ifstream fin(fname);
if(fin.bad()){
return -2;
}
char ch;
while(fin.get(ch)){
cout << ch;
}
fin.close();
}
2、二进制文件操作
对于二进制文件,可进行使用 ifstream中的read(buff,1024) 读取二进制文件,通过 ofstream中的write(buff,1024) 写入到对应文件中
#include <fstream>
void main(){
char src[] = "D://xueshan.jpg";
char src_copy[] = "D://xueshan_copy.jpg";
//读取
ifstream fin(src,ios::binary);
//输出流
ofstream fout(src_copy,,ios::binary);
//创建失败
if(fin.bad()||fout.bad()){
return -1;
}
while(!fin.eof()){
char buff[1024] = {0};
fin.read(buff,1024);
//写入
fout.write(buff, 1024);
}
//关闭
fin.close();
fout.close();
}
3、对象持久化
class Person{
private:
char* name;
int age;
public:
Person(){}
Person(char* name,int age){
this->name = name;
this->age = age;
}
char* getName(){
return this->name;
}
int getAge(){
return this->age;
}
void print(){
cout << this->name << "," << this->age << endl;
}
};
void main(){
Person p1(const_cast<char*>("Jack"),25);
Person p2(const_cast<char*>("Rose"),22);
//输出流
ofstream fout("D://c_obj.data",ios::binary);
//指针能够读取到正确的数据,读取内存区的长度
fout.write((char*)(&p1), sizeof(Person));
fout.write((char*)(&p2), sizeof(Person));
fout.close();
//输入流
ifstream fin("D://c_obj.data",ios::binary);
Person tmp;
fin.read((char*)(&tmp), sizeof(Person));
tmp.print();
fin.read((char*)(&tmp), sizeof(Person));
tmp.print();
}
三、STL(standard template library)标准模板库
在 C++ 中包含很多标准模板库,可以让代码更加简洁
1、string 模板库
使用 string 模板库,在边写代码时更改简洁易用,但是在JNI中 string 需要转为 char ,,在转为jstring返回*
1.1 string 初始化
#include <string>
void main(){
string s1 = "Hello ";
string s2(" every day");
string s3 = s1 + s2;
cout << s3 << endl;
//string 转 c 字符串,在JNI中要转为C的str 然后再转为jstring返回
const char* c_str = s1.c_str();
cout << c_str << endl;
}
1.2 string 遍历
#include <string>
//string遍历
void main()
{
string s1 = "craig david";
// ^
//1 数组方式
for (int i = 0; i < s1.length(); i++)
{
cout << s1[i] << endl;
}
//2 迭代器指针
for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//3 at函数(charAt)
// 可能会抛出异常
try
{
for (int i = 0; i < s1.length() + 3; i++)
{
cout << s1.at(i) << " ";
}
}
catch (...)
{
cout << "异常" << endl;
}
system("pause");
}
1.3 string 字符串查找替换
#include <string>
#include <iostream>
using namespace std;
//字符串查找替换
void main()
{
string s1 = "apple google apple iphone";
//从0开始查找"google"的位置
int idx = s1.find("google", 0);
cout << idx << endl;
//统计apple出现的次数
int idx_app = s1.find("apple",0);
//npos大于任何有效下标的值
int num = 0;
while (idx_app != string::npos)
{
num++;
cout << "找到的索引:" << idx_app << endl;
idx_app+=5;
idx_app = s1.find("apple", idx_app);
}
cout << num << endl;
system("pause");
}
1.4 string 删除(截取)和插入
#include <string>
#include <iostream>
using namespace std;
//删除(截取)和插入
void main()
{
string s1 = "apple google apple iphone";
//删除a,找到a所在的指针
string::iterator it = find(s1.begin(),s1.end(),'g');
//只能删除一个字符
s1.erase(it);
//开头末尾插入字符串
s1.insert(0, "macos");
s1.insert(s1.length(), " facebook");
cout << s1 << endl;
system("pause");
}
1.5 string 大小写转换
#include <string>
#include <iostream>
#include <algorithm> //算法
using namespace std;
//java StringBuffer才可变
//String 不可变
//大小写转换
void main()
{
string s1 = "JASON";
//原始字符串的起始地址,原始字符串的结束地址, 目标字符串的起始地址, 函数名称
transform(s1.begin(), s1.end()-1,s1.begin(), tolower);
cout << s1 << endl;
transform(s1.begin(), s1.end() - 1, s1.begin(), toupper);
cout << s1 << endl;
system("pause");
}
2、容器 vector 模板库
使用 vector 模板库,不需要使用动态内存分配,就可以使用动态数组
2.1 vector 初始化与遍历
#include <vector>
void printVector(vector<int> &v)
{
//通过数组的方式遍历
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
}
void main(){
//动态数组
vector<int> v;
v.push_back(12);
v.push_back(118);
v.push_back(1);
v.push_back(52);
v.push_back(20);
//遍历
for(int i = 0; i<v.size(); i++){
cout << v[i] << endl;
}
//部分复制
vector<int> v3(v1.begin(),v1.begin()+2);
for(int i = 0; i<v3.size(); i++){
cout << v3[i] << endl;
}
}
2.2 vector 添加和删除
//添加 删除
void main()
{
//添加到结尾
vector<int> v1;
v1.push_back(20);
v1.push_back(40);
v1.push_back(15);
v1.push_back(7);
//访问头部
v1.front() = 11;
//访问尾部
v1.back() = 90;
//删除结尾的元素
//v1.pop_back();
while (v1.size() > 0)
{
cout << "末尾的元素:" << v1.back() << endl;
v1.pop_back();
}
printVector(v1);
system("pause");
}
2.3 vector 数组方式
//数组的方式
void main()
{
vector<int> v1;
v1.push_back(20);
v1.push_back(40);
v1.push_back(15);
v1.push_back(7);
v1[2] = v1[2] +10;
//容器等价于动态数组
vector<int> v2(10);
for (int i = 0; i < v2.size(); i++)
{
v2[i] = i + 1;
}
printVector(v2);
system("pause");
}
2.4 vector 迭代器遍历
//迭代器遍历
//迭代器的种类(正向,反向迭代器)
void main()
{
vector<int> v1;
v1.push_back(20);
v1.push_back(40);
v1.push_back(15);
v1.push_back(7);
//正向
for (vector<int>::iterator it = v1.begin(); it < v1.end(); it++)
{
cout << *it << endl;
}
cout << "-----------------" << endl;
//反向迭代
for (vector<int>::reverse_iterator it = v1.rbegin(); it < v1.rend(); it++)
{
cout << *it << endl;
}
system("pause");
}
2.5 vector 插入和删除
void main()
{
vector<int> v1(10);
for (int i = 0; i < v1.size(); i++)
{
v1[i] = i + 1;
}
//删除指定位置
vector<int>::iterator it = v1.begin();
it += 3;
v1.erase(it);
//distance(v1.begin(), it);
//删除区间
v1.erase(v1.begin(), v1.begin() + 3);
for (vector<int>::iterator it = v1.begin(); it < v1.end(); it++)
{
if (*it == 5)
{
printf("%x\n", it);
vector<int>::iterator tmp = v1.erase(it); //注意以后开发中编译器版本问题
printf("%x,%x\n",it,tmp);
}
}
//插入
v1.insert(v1.begin() + 2, 100);
v1.insert(v1.end() - 1, 200);
printVector(v1);
system("pause");
}
3、队列 deque 模板库
3.1 双向队列
//双向队列
#include <deque>
void printDeque(deque<int>& q)
{
for (int i = 0; i < q.size(); i++)
{
cout << q[i] << endl;
}
}
void main()
{
deque<int> d1;
//添加到尾部
d1.push_back(2);
d1.push_back(10);
//添加到头部
d1.push_front(-90);
d1.push_front(-30);
//printDeque(d1);
//cout << d1.front() << endl;
//cout << d1.back() << endl;
//两个方向弹出
//d1.pop_back();
//d1.pop_front();
printDeque(d1);
//查找第一个-90元素索引位置,无需遍历
deque<int>::iterator it = find(d1.begin(), d1.end(), -90);
if (it != d1.end())
{
int idx = distance(d1.begin(), it);
cout << "索引位置为:" << idx << endl;
}
system("pause");
}
3.2 没有迭代器的队列
#include <iostream>
#include <queue>
using namespace std;
void main()
{
queue<int> q;
q.push(78);
q.push(18);
q.push(20);
q.push(33);
//q.front();
//q.back();
while (!q.empty())
{
int tmp = q.front();
cout << tmp << endl;
q.pop();
}
system("pause");
}
3.3 优先级队列
#include <iostream>
#include <queue>
using namespace std;
void main()
{
//默认 最大值优先级
priority_queue<int> pq1;
pq1.push(12);
pq1.push(3);
pq1.push(40);
pq1.push(15);
while (!pq1.empty())
{
int tmp = pq1.top();
cout << tmp << endl;
pq1.pop();
}
cout << "----------" << endl;
//最小值优先级队列
priority_queue<int, vector<int>, greater<int>> pq2;
pq2.push(12);
pq2.push(3);
pq2.push(40);
pq2.push(15);
while (!pq2.empty())
{
int tmp = pq2.top();
cout << tmp << endl;
pq2.pop();
}
system("pause");
}
4、stack 栈 模板
4.1 stack 栈的初始化
#include <iostream>
#include <stack>
using namespace std;
void main()
{
stack<int> s;
for (int i = 0; i < 10; i++)
{
s.push(i + 1);
}
while (!s.empty())
{
int tmp = s.top();
cout << tmp << endl;
s.pop();
}
system("pause");
}
5、list 模板
5.1 list 初始化
#include <iostream>
#include <list>
using namespace std;
void printList(list<int>& lst)
{
//迭代器
//没有重载“<”运算符
for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
{
cout << *it << endl;
}
}
//基本操作
/*
void main()
{
list<int> lst;
for (int i = 0; i < 10; i++)
{
//尾部插入元素
lst.push_back(i);
}
//头部插入元素
lst.push_front(80);
lst.push_front(90);
list<int>::iterator it = lst.begin();
it++;
cout << *it << endl;
//it = it + 3; 注意:不支持随机访问
printList(lst);
system("pause");
}
5.2 list 插入
#include <iostream>
#include <list>
using namespace std;
void main()
{
list<int> lst;
for (int i = 0; i < 10; i++)
{
//尾部插入元素
lst.push_back(i);
}
list<int>::iterator it = lst.begin();
it++;
lst.insert(it, 100);
printList(lst);
system("pause");
}
5.3 list 初始化
#include <iostream>
#include <list>
using namespace std;
void main()
{
list<int> lst;
for (int i = 0; i < 10; i++)
{
//尾部插入元素
lst.push_back(i);
}
list<int>::iterator it = lst.begin();
//删除
it++;
//删除第二个元素
//lst.erase(it);
//删除区间(已经被删除了元素不能再删除)
list<int>::iterator it_begin = lst.begin();
list<int>::iterator it_end = lst.begin();
it_end++;
it_end++;
it_end++;
lst.erase(it_begin, it_end);
//直接根据内容删除元素
lst.remove(5);
printList(lst);
system("pause");
}
6、set 模板
6.1 set 初始化
#include <iostream>
#include <set>
using namespace std;
//set 元素唯一 默认从小到大
void printSet(set<int> &s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
}
void main()
{
set<int> s;
//添加元素
for (int i = 0; i < 10; i++)
{
s.insert(i+1);
}
s.insert(20);
s.insert(15);
s.insert(15);
//删除
set<int>::iterator it = s.begin();
it++;
s.erase(it);
printSet(s);
system("pause");
}
6.2 set 元素按照从大到小排列
#include <iostream>
#include <set>
#include <functional>
using namespace std;
void main()
{
//同Java中:Map<String,List<String>>
set<int,greater<int>> s;
s.insert(10);
s.insert(5);
s.insert(20);
s.insert(99);
for (set<int,greater<int>>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
system("pause");
}
6.3 set 自定义排序规则
#include <iostream>
#include <set>
#include <functional>
using namespace std;
//元素类型为Teacher对象,按照年龄排序
class Teacher
{
public:
Teacher(char* name, int age)
{
this->name = name;
this->age = age;
}
void print()
{
cout << name << "," << age << endl;
}
public:
char* name;
int age;
};
//自定义排序规则
//仿函数
struct MyAgeSorter
{
bool operator()(const Teacher &left, const Teacher &right)
{
return left.age < right.age;
}
};
void main()
{
set<Teacher, MyAgeSorter> s;
s.insert(Teacher(const_cast<char*>("jack"),18));
s.insert(Teacher(const_cast<char*>("rose"), 20));
s.insert(Teacher(const_cast<char*>("jason"), 22));
s.insert(Teacher(const_cast<char*>("alan"), 5));
//s.insert(Teacher("jimy", 5)); //不会插入
for (set<Teacher>::iterator it = s.begin(); it != s.end(); it++)
{
cout << (*it).name << "," << (*it).age << endl;
}
system("pause");
}
6.3 set 查找
#include <iostream>
#include <set>
using namespace std;
void main()
{
set<int> s;
//添加元素
for (int i = 0; i < 10; i++)
{
s.insert(i + 1);
}
//printSet(s);
//等于4的元素指针
set<int>::iterator s_4 = s.lower_bound(4);
//cout << *s_4 << endl;
//大于4的元素指针
set<int>::iterator s_5 = s.upper_bound(4);
//cout << *s_5 << endl;
//一次性获取等于4的元素指针,和大于4的元素指针\
//BasicNameValuePair
pair<set<int>::iterator, set<int>::iterator> p = s.equal_range(4);
cout << *p.first << endl;
cout << *p.second << endl;
system("pause");
}
6.4 multiset 允许重复的元素
#include <iostream>
#include <set>
using namespace std;
void main()
{
multiset<int> s;
s.insert(2);
s.insert(8);
s.insert(2);
s.insert(8);
for (multiset<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << endl;
}
system("pause");
}
7、map 模板
7.1 map 初始化
#include <iostream>
#include <map>
#include <string>
using namespace std;
void main()
{
//key -> value
//1.
map<int, string> map1;
map1.insert(pair<int, string>(1, "jack"));
map1.insert(pair<int, string>(2, "rose"));
//2
map1.insert(make_pair(3, "jason"));
//3
map1.insert(map<int, string>::value_type(4, "alan"));
//4
map1[5] = "jimmy"; //map["NO1"] = 90;
//前三种方式,如果key已经存在,重复添加会报错
//第四种方式,如果key已经存在,重复添加会覆盖
//遍历输出
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "," << it->second << endl;
}
system("pause");
}
7.2 map 删除元素的方式
#include <iostream>
#include <map>
#include <string>
using namespace std;
void printMap(map<int, string> &map1)
{
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "," << it->second << endl;
}
}
void main()
{
map<int, string> map1;
map1.insert(pair<int, string>(1, "jack"));
map1.insert(pair<int, string>(2, "rose"));
map1.insert(pair<int, string>(3, "jason"));
map<int, string>::iterator it = map1.begin();
it++;
map1.erase(it);
printMap(map1);
system("pause");
}
7.3 map 添加元素的结果
#include <iostream>
#include <map>
#include <string>
using namespace std;
void main()
{
map<int, string> map1;
map1.insert(pair<int, string>(1, "jack"));
map1.insert(pair<int, string>(2, "rose"));
map1.insert(pair<int, string>(3, "jason"));
//获取添加的结果(first元素指针,second 是否成功)
pair<map<int, string>::iterator, bool> res = map1.insert(pair<int, string>(3, "alan"));
if (res.second)
{
cout << "添加成功" << endl;
}
else
{
cout << "添加失败" << endl;
}
printMap(map1);
system("pause");
}
7.3 map 查找
#include <iostream>
#include <map>
#include <string>
using namespace std;
void main()
{
map<int, string> map1;
map1.insert(pair<int, string>(1, "jack"));
map1.insert(pair<int, string>(2, "rose"));
map1.insert(pair<int, string>(3, "jason"));
printMap(map1);
cout << "---------" << endl;
//获取key等于大于5的元素的值
pair<map<int, string>::iterator, map<int, string>::iterator> p = map1.equal_range(2);
if (p.first != map1.end()){
//等于2的元素key value
cout << p.first->first << p.first->second << endl;
//大于2的元素key value
cout << p.second->first << p.second->second << endl;
}
system("pause");
}
7.3 multimap
#include <iostream>
#include <map>
#include <string>
using namespace std;
//一个key对应多个value
//一个部门多个员工
//multimap
class Employee
{
public:
Employee(char* name,int age)
{
this->name = name;
this->age = age;
}
public:
char* name;
int age;
};
void main()
{
multimap<string, Employee> map1;
//开发部
map1.insert(make_pair("开发", Employee(const_cast<char*>("搁浅"), 20)));
map1.insert(make_pair("开发", Employee(const_cast<char*>("彪哥"), 20)));
//财务
map1.insert(make_pair("财务", Employee(const_cast<char*>("小颖"), 16)));
map1.insert(make_pair("财务", Employee(const_cast<char*>("rose"), 20)));
//销售
map1.insert(make_pair("销售", Employee(const_cast<char*>("阿呆"), 30)));
map1.insert(make_pair("销售", Employee(const_cast<char*>("呵呵"), 30)));
//遍历输出
for (multimap<string, Employee>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "," << it->second.name << "," << it->second.age << endl;
}
cout << "----------------" << endl;
//只获取“财务”部的员工
//获取“财务部”员工的个数,key对应的value的个数
int num = map1.count("财务");
multimap<string, Employee>::iterator it = map1.find("财务");
int c = 0; //控制循环的次数
while (it != map1.end() && c < num)
{
cout << it->first << "," << it->second.name << "," << it->second.age << endl;
it++;
c++;
}
system("pause");
}
8、深拷贝与浅拷贝
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
class Employee
{
public:
//构造函数
Employee(char* name, int age)
{
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
//析构函数
~Employee()
{
if (this->name != NULL)
{
delete[] this->name;
this->name = NULL;
this->age = 0;
}
}
//拷贝构造函数
//Employee e =
Employee(const Employee &obj)
{
this->name = new char[strlen(obj.name) + 1];
strcpy(this->name, obj.name);
this->age = obj.age;
}
//重载=
//e1 = e2;
Employee& operator=(const Employee &obj)
{
//释放旧的内存
if (this->name != NULL)
{
delete[] this->name;
this->name = NULL;
this->age = 0;
}
//重新分配
this->name = new char[strlen(obj.name) + 1];
strcpy(this->name, obj.name);
this->age = obj.age;
return *this;
}
public:
char* name;
int age;
};
void func()
{
vector<Employee> v1;
Employee e1(const_cast<char*>("jack"), 20);
v1.push_back(e1);
}
void main()
{
//vector<Employee> v1;
//Employee e1("jack",20);
//将e1拷贝到vector中
//v1.push_back(e1);
func();
system("pause");
}