【1】.this指针(借鉴网址:C++ this指针详解(精辟) (biancheng.net))
this 是C++中的一个关键字,也是一个 const指针 ,它指向当前对象,通过它可以访问当前对象的所有成员。(所谓当前对象,是指正在使用的对象。例如对于stu.show();
stu 就是当前对象,this 就指向 stu。)
一般,成员函数最终被编译成与对象无关的普通函数,除了成员变量,会丢失所有信息,所以编译时要在成员函数中添加一个额外的参数,即this,作为桥梁,将成员函数和成员变量联系在一起。
#include <iostream>
using namespace std;
class Student{
public:
void setname(char *name);
void setage(int age);
void setscore(float score);
void show();
private:
char *name;//只有在对象被创建后,才能通过this访问
int age;
float score;
};
void Student::setname(char *name){
this->name = name;//本例中成员函数变量名和形参name命名相同,
//而this->说明左边的name是Student的成员变量。
//this 只能用在类的内部,通过 this 可以访问类的所有成员,
//包括 private、protected、public 属性的
}
void Student::setage(int age){
this->age = age;
}
void Student::setscore(float score){
this->score = score;
}
void Student::show(){
cout<<this->name<<"的年龄是"<<this->age<<",成绩是"<<this->score<<endl;
}
int main(){
Student *pstu = new Student;
pstu -> setname("李华");
pstu -> setage(16);
pstu -> setscore(96.5);
pstu -> show();
return 0;
}
运行结果:李华的年龄是16,成绩是96.5
【2】.pair类型(定义在#include <utility>头文件中)
模板:pair<class T1,class T2>T3(对象);
用途:将两个可以为不同类型的数据T1,T2 合并为一个新的T3,用T3.first访问T1,用T3.second访问T2
用法:
typedef pair<string,string> Type;
Type T1("Tony","Jenny");
Type T2("James","Curry");
还可以用于对map的插入
#include <map>
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01(){
//插入
map<int, int> m;
//第一种插入方式
m.insert(pair<int, int>(1, 10));
//第二种插入方式
m.insert(make_pair(2, 20)); printMap(m);
//删除 m.erase(m.begin()); printMap(m); m.erase(3); printMap(m);
//清空 m.erase(m.begin(),m.end()); m.clear(); printMap(m);}
int main()
{
test01();
system("pause");
return 0;
}
当有些函数以pair对象作为返回值时,可以在main函数中使用tie(class1,class2)=函数名(); 去接收。
#include <iostream>
#include<map>
#include<utility>
using namespace std;
pair<string, int>getperson()
{
return make_pair("nb", 7);
}
int main()
{
string ch; int q;
tie(ch, q) = getperson();
//输出ch,q可以得到nb和7
map<string, int>zz;
zz.insert(getperson());//不能使用zz.insert(tie(ch,q))
return 0;}
【3】栈
例题(出自算法竞赛入门经典-第二版):有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且 支持以下操作。
PUSH:空集“{}”入栈。 DUP:把当前栈顶元素复制一份后再入栈。 UNION:出栈两个集合,然后把二者的并集入栈。 INTERSECT:出栈两个集合,然后把二者的交集入栈。 ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈。
每次操作后,输出栈顶集合的大小(即元素个数)。例如,栈顶元素是A={{}, {{}}},下一个元素是B={{},{{{}}}},则: UNION操作将得到{{},{{}},{{{}}}},输出3。 INTERSECT操作将得到{{}},输出1。 ADD操作将得到{{},{{{}}},{{},{{}}}},输出3。 输入不超过2000个操作,并且保证操作均能顺利进行(不需要对空栈执行出栈操作)。
typedef set<int> Set;//构造新的数据类型
map<Set,int> IDcache; //把集合映射成ID
vector<Set> Setcache; //根据ID 取集合
//查找给定集合x的ID。如果找不到,分配一个新ID
int ID (Set x) {
if (IDcache.count(x)) return IDcache[x];
Setcache.push_back(x); //添加新集合
return IDcache[x] = Setcache.size() - 1;//Setcache是个vector数组,ID从0开始(数组下标)
}
#define ALL(x) x.begin(),x.end()//这里定义了两个宏(类似于函数)
#define INS(x) inserter(x,x.begin())//用法体现在下面
stack<int> s; //题目中的栈(收集ID)
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string op;
cin >> op;
if (op[0] == 'P') s.push(ID(Set()));
else if (op[0] == 'D') s.push(s.top());
else {
Set x1 = Setcache[s.top()]; s.pop();//将出栈元素分别用x1和x2接受
Set x2 = Setcache[s.top()]; s.pop();
Set x;
if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x));//将x1,x2的始末地址用一个宏来表示
if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));//可替换成inserter
if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
s.push(ID(x));//把x1和x2进行了3种不同操作后得到的一个集合元素输进栈s中
}
cout <<Setcache[s.top()].size() << endl;//求上面刚输进去s的元素(set类型)自身的元素个数
}
set_union函数(求并集)
一般格式:
using namespace std;
vector<int> set1 {1, 2, 3, 4, 5, 6};
vector<int> set2 {4, 5, 6, 7, 8, 9};
vector<int> result;
set_union(begin (set1), end(set1), // set1的始末位置
begin(set2), end(set2), //set2的始末位置
back_inserter(result)); //交集要输出给谁
//最后一个参数可以为inserter(result,result.begin()的形式,
set_intersection函数(求交集)
set_difference(求差集,前减后,即前面集合有而后面集合没有的)
set_symmetric_difference(求对称差集,即并集减掉交集)
这些函数格式类似
关于inserter:
原理:其内部调用insert()
功能:在容器的指定位置插入元素。
限制:只能用在提供了insert()成员函数的容器中, 所有STL容器都提供了insert()函数。
适用:所有STL容器。
注意:使用inserter的时候,插入的起始位置是在指定位置的前方。