0705第七讲标准模版库
1、namespace:命名空间,解决相同作用域同名函数或变量问题
要求:使用时必须以’命名空间::成员名称’的格式
类型转换:强制类型转换(向下转换)
动态类型转换:dynamic_cast,把父类指针转换子类指针(如果转型成功返回子类指针,如果转型失败返回NULL,向下转型安全)
格式:dynamic_cast<子类指针>(父类指针)
如果转型成功,那么得到一个子类指针
如果转型失败,那么得到一个NULL
静态类型转换:static_cast 和强制类型转换效果相同,不能判别是否成功
动态类型转换:必须有virtual函数
eg:
class Base{
public:
virtual void print()const{
cout<<"virtual Base..."<<endl;
}
void outB() const{
cout<<"Base..."<<endl;
}
};
class D1:public Base{
public:
void print()const{
cout<<"virtual D1..."<<endl;
}
void outD1()const{
cout<<"D1..."<<endl;
}
};
class D2:public Base{
public:
void print()const{
cout<<"virtual D2..."<<endl;
}
void outD2()const{
cout<<"D2..."<<endl;
}
};
int main(int argc, const char * argv[]) {
D1 d1;
D2 d2;
Base* b[2] = {&d1,&d2};
for (int i = 0; i < 2; ++i) {
if (D1* p1 = dynamic_cast<D1*>(b[i]) ) {
cout<<i<<"..."<<endl;
p1->print();
p1->outD1();
}
else if(D2* p2 = dynamic_cast<D2*>(b[i])){
cout<<i<<"..."<<endl;
p2->print();
p2->outD2();
}
}
return 0;
}
2、字符串:string,实际上是一个模版类basic_string
头文件:#include,使用时必须包含using namespace std;
构造函数:
1)默认构造函数,初始化一个空字符串。 string str
2)构造函数,使用字符串初始化对象 string str(“good day”)
3)构造函数使用一个隐式转换 string sr = “i am a student”拷贝构造函数
4)类字符串还有拷贝构造函数、赋值操作符重载函数和输出运算符重
string str = “i am a student”;
cout<
#include <iostream>
using namespace::std;
#include <vector>
#include <list>
#include <map>
class Point{
int x;
int y;
public:
Point(int _x,int _y):x(_x),y(_y){
}
void print(){
cout<<"x = "<<x<<" y = "<<y<<endl;
}
};
typedef list<Point*>PointList;
void funcVector(){
Point p1(1,1),p2(1,2),p3(1,3),p4(1,4),p5(1,5);
vector<Point*> v;
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
cout<<"v[2]: ";
v[2]->print();
cout<<"v.at(2): ";
v.at(2)->print();
cout<<"size before insert: "<<v.size()<<endl;
v.insert(v.begin(),&p5);
cout<<"size after insert: "<<v.size()<<endl;
cout<<"using for recycle:"<<endl;
for (int i = 0;i < v.size() ; ++i) {
cout<<"v.at("<<i<<"):";
v.at(i)->print();
}
cout<<"using iteraror:"<<endl;
cout<<"size : "<<v.size()<<endl;
vector<Point*>::iterator itor;//创建对象
iter = v.begin();
while (itor!=v.end()) {
(*itor++)->print();
}
cout<<"the first element:";
v.front()->print();
cout<<"the last element:";
v.back()->print();
cout<<"maxsize\t\t"<<v.max_size()<<endl;
cout<<"capacity\t"<<v.capacity()<<endl;
v.clear();
cout<<"size after clear:\t"<<v.size()<<endl;
}
v[2]: x = 1 y = 3
v.at(2): x = 1 y = 3
size before insert: 4
size after insert: 5
using for recycle:
v.at(0):x = 1 y = 5
v.at(1):x = 1 y = 1
v.at(2):x = 1 y = 2
v.at(3):x = 1 y = 3
v.at(4):x = 1 y = 4
using iteraror:
size : 5
x = 1 y = 5
x = 1 y = 1
x = 1 y = 2
x = 1 y = 3
x = 1 y = 4
the first element:x = 1 y = 5
the last element:x = 1 y = 4
maxsize 2305843009213693951
capacity 8
size after clear: 0
void funcList(){
PointList lst;
Point p1(1,1),p2(1,2),p3(1,3),p4(1,4),p5(1,5);
lst.push_front(&p1);
lst.push_back(&p1);
lst.push_front(&p2);
lst.push_back(&p2);
lst.push_front(&p3);
lst.push_back(&p3);
lst.push_front(&p4);
lst.push_back(&p4);
// lst.insert(5, &p5);
lst.erase(lst.begin());
lst.remove(&p2);// 移除内容为&p2的项
PointList::iterator itor = lst.begin();
while (itor != lst.end()) {
(*itor++)->print();
}
cout<<endl;
// for (int i = 0;i < lst.size() ; ++i) {
// cout<<"lst.at("<<i<<"):"<<endl;
// lst.at(i)->print();
// }
}
x = 1 y = 3
x = 1 y = 1
x = 1 y = 1
x = 1 y = 3
x = 1 y = 4
void funcSet(){
set<int>s;
s.insert(10);
s.insert(-5);
s.insert(123);
s.insert(2);
s.insert(44);
s.insert(10);//10不能插入,重复(自动被忽略)
set<int>::iterator here;
for(here=s.begin();here!=s.end();++here){
cout<<*here<<" ";
}
cout<<endl;
int key;
cout<<"Enter an integer to search.\n";
cin>>key;
here=s.find(key);
if(here!=s.end()){
cout<<key<<"is in this set"<<endl;
}
else{
cout<<key<<"is not in this set"<<endl;
}
}
-5 2 10 44 123
Enter an integer to search.
5
5is not in this set
void funcMap(){
map<string,char>m;
m.insert(map<string,char>::value_type("key1",'1'));
m["key2"] = '2';///使用multimap 键值可以重复但不能使用这种赋值方式,使用map自动忽略重复键值项
m["key4"] = '4';
m["key3"] = '3';
map<string, char>::iterator itor = m.begin();
while (itor != m.end()) {
cout<<itor->first<<endl;
cout<<itor->second<<endl;
++itor;
}
}
(使用insert函数 结果自动排序)
key1
1
key2
2
key3
3
key4
4