C++的4个子集
C子集
class子集
STL
template模板
void fun(int& a)
{
cout << "fun(int & a)" << endl;
}
void fun(const int& a)
{
cout << "fun(const int &a)" << endl;
}
void fun(int&& a)
{
cout << "fun(int && a)" << endl;
}
int main()
{
int a = 10;
const int b = 20;
const int& x = b;
const int& y = 20;
fun(x);
fun(y);
fun(10);
return 0;
}
 void fun(const int& a)//常引用不管你是左值还是右值都以引用
void fun(int& a)
{
cout << "fun(int & a)" << endl;
}
void fun(const int& a)
{
cout << "fun(const int &a)" << endl;
}
int main()
{
int a = 10;
const int b = 20;
const int& x = b;
const int& y = 20;
fun(x);
fun(y);
fun(10);
return 0;
}

void fun(int& a)
{
cout << "fun(int & a)" << endl;
}
void fun(const int& a)
{
cout << "fun(const int &a)" << endl;
}
void fun(int&& a)
{
cout << "fun(int && a)" << endl;
}
template<class T>
void fac(T&& a)
{
fun(a);
}
int main()
{
int a = 10;
const int b = 20;
fac(a);
fac(b);
fac(20);
return 0;
}

完美转发 std::forward(a)
系统为了避免上述的右值引用的情况产生了新的概念完美转发
void fun(int& a)
{
cout << "fun(int & a)" << endl;
}
void fun(const int& a)
{
cout << "fun(const int &a)" << endl;
}
void fun(int&& a)
{
cout << "fun(int && a)" << endl;
}
template<class T>
void fac(T&& a)
{
fun(std::forward<T>(a));
}
int main()
{
int a = 10;
const int b = 20;
fac(a);
fac(b);
fac(20);
return 0;
}

template<class T>
class Object
{
T value;
public:
Object(T x = T()) :value(x) {}
static int num;
};
template<class T>
int Object<T>::num = 0;
class Base :public Object<int>
{
public: Base() { num += 1; }
void Print()const { cout << "Base" << num<<endl; }
};
class Test :public Object<int>
{
public:
Test() { num += 1; }
void Print()const { cout << "Test" << num << endl; }
};
int main()
{
Base b1, b2;
Test t1, t2;
b1.Print();
t1.Print();
return 0;
}

template<class T>
class Object
{
T value;
public:
Object(T x = T()) :value(x) {}
static int num;
};
template<class T>
int Object<T>::num = 0;
class Base :public Object<int>
{
public: Base() { num += 1; }
void Print()const { cout << "Base" << num<<endl; }
};
class Test :public Object<double>
{
public:
Test() { num += 1; }
void Print()const { cout << "Test" << num << endl; }
};
int main()
{
Base b1, b2;
Test t1, t2;
b1.Print();
t1.Print();
return 0;
}

菱形继承
数据冗余导致的不一致性
class Person
{
private:
string p_id;
string p_name;
string p_sex;
int p_age;
public:
Person(const string& id, const string& name, const string& sex, int age)
:p_id(id), p_name(name), p_sex(sex), p_age(age)
{}
void print()const
{
cout << "id: " << p_id << endl;
cout << "name: " << p_name << endl;
cout << "sex: " << p_sex << endl;
cout << "age: " << p_age << endl;
}
};
class Student :public Person
{
private:
string s_id;
public:
Student(const string& id, const string& name, const string& sex, int age, const string& d)
:Person(id, name, "男", age), s_id(d)
{}
};
class Staff :public Person
{
private:
string s_id;
public:
Staff(const string& id, const string& name, const string& sex, int age, const string& d)
:Person(id, name, "女", age), s_id(d)
{}
};
class Stud_Staff :public Student, public Staff
{
private:
public:
Stud_Staff(const string& id, const string& name, int age, const string& dd, const string ss)
:Student(id,name,"男",age,ss),Staff(id,name,"nv",age,dd)
{
}
};
int main()
{
Stud_Staff ss("610431199900000000", "王伟", 23, "20200", "303030");
ss.Student::print();
ss.Staff::print();
Student s1 = ss;
Staff s2 = ss;
return 0;
}
 
虚继承
class Person
{
private:
string p_id;
string p_name;
string p_sex;
int p_age;
public:
Person(const string& id, const string& name, const string& sex, int age)
:p_id(id), p_name(name), p_sex(sex), p_age(age)
{}
void print()const
{
cout << "id: " << p_id << endl;
cout << "name: " << p_name << endl;
cout << "sex: " << p_sex << endl;
cout << "age: " << p_age << endl;
}
};
class Student : virtual public Person
{
private:
string s_id;
public:
Student(const string& id, const string& name, const string& sex, int age, const string& d)
:Person(id, name, "男", age), s_id(d)
{}
};
class Staff :virtual public Person
{
private:
string s_id;
public:
Staff(const string& id, const string& name, const string& sex, int age, const string& d)
:Person(id, name, "女", age), s_id(d)
{}
};
class Stud_Staff :public Student, public Staff
{
private:
int num = 10;
public:
Stud_Staff(const string& id, const string& name, int age, const string& dd, const string ss)
:Student(id,name,"男",age,ss),Staff(id,name,"女",age,dd)
,Person("61010","yhping","男",23)
{
}
};
int main()
{
Stud_Staff ss("610431199900000000", "王伟", 23, "20200", "303030");
ss.Student::print();
ss.Staff::print();
Student s1 = ss;
Staff s2 = ss;
Person p = ss;
return 0;
}

class Object
{
int value;
public:
Object(int x = 10) :value(x) {}
};
class Base :virtual public Object
{
int num;
public:
Base(int x = 0) :num(x), Object(x + 10) {}
};
class Test :virtual public Object
{
int sum;
public:
Test(int x = 0) :sum(x), Object(x + 10) {}
};
class Det :public Base, public Test
{
private:
int total;
public:
Det(int x = 0) :total(x), Base(x + 10), Test(x + 20), Object(x + 100) {}
};
int main()
{
Det d(0);
return 0;
}

菱形继承的多继承
class Object
{
int value;
public:
Object(int x = 10) :value(x) {}
virtual void fun() { cout << "Object" << endl; }
virtual void add() { cout << "Object::add" << endl; }
};
class Base :public Object
{
int num;
public:
Base(int x = 0) :num(x), Object(x + 10) {}
virtual void fun() { cout << "Base" << endl; }
};
class Test : public Object
{
int sum;
public:
Test(int x = 0) :sum(x), Object(x + 10) {}
virtual void fun() { cout << "Test" << endl; }
void add() { cout << "Test::add" << endl; }
};
class Det :public Base, public Test
{
private:
int total;
public:
Det(int x = 0) :total(x), Base(x + 10), Test(x + 20) {}
void fun() { cout << "Det" << endl; }
void add() { cout << "Det::add" << endl; }
};
int main()
{
Det d(0);
Object* opa = (Base*)&d;
Object* opb = (Test*)&d;
opa->add();
opa->fun();
opb->add();
opb->fun();
return 0;
}


虚继承的多继承
class Object
{
int value;
public:
Object(int x = 10) :value(x) {}
virtual void fun() { cout << "Object" << endl; }
virtual void add() { cout << "Object::add" << endl; }
};
class Base :virtual public Object
{
int num;
public:
Base(int x = 0) :num(x), Object(x + 10) {}
virtual void fun() { cout << "Base" << endl; }
};
class Test : virtual public Object
{
int sum;
public:
Test(int x = 0) : sum(x), Object(x + 10) {}
virtual void fun() { cout << "Test" << endl; }
void add() { cout << "Test::add" << endl; }
};
class Det :public Base, public Test
{
private:
int total;
public:
Det(int x = 0) :total(x), Base(x + 10), Test(x + 20), Object(x + 100) {}
void fun() { cout << "Det" << endl; }
void add() { cout << "Det::add" << endl; }
};
int main()
{
Det d(0);
Object* opa = &d;
Object* opb = &d;
opa->add();
opa->fun();
opb->add();
opb->fun();
return 0;
}

STL
  
STL问的最多的就是配置器和迭代器的失效问题 
字符串库
#include<string>
#include<string.h>
#include<cstring>
int main()
{
string s1 = "yhpingll";
string s2("yhpingll");
string::value_type v;
string::pointer p = &v;
string::reference x = v;
string::const_reference y = v;
cout << typeid(p).name() << endl;
}
int main()
{
string s1 = "abcdefgh";
string::iterator it = s1.begin();
for (;it != s1.end(); ++it)
{
cout << *it;
}
cout << endl;
string::reverse_iterator rt;
for (rt = s1.rbegin(); rt != s1.rend(); ++rt)
{
cout << *rt;
}
cout << endl;
for (auto& x : s1)
{
cout << x;
}
cout << endl;
return 0;
}

int main()
{
string s1("yhping");
string s2("hello");
cout << s1 << endl;
cout << s2 << endl;
s1 = s2;
cout << s1 << endl;
cout << s2 << endl;
}

int main()
{
string s1("yhping");
string s2("hello");
cout << s1 << endl;
cout << s2 << endl;
s1 = std::move(s2);
cout << s1 << endl;
cout << s2 << endl;
}

#include<string>
int main()
{
string s1("yhping");
cout << "size: " << s1.size() << endl;
cout << "capacity: " << s1.capacity() << endl;
s1 += "hellohelloniahoanihaoa";
cout << "size: " << s1.size() << endl;
cout << "capacity: " << s1.capacity() << endl;
return 0;
}

int main()
{
string s1("yhping");
int n = s1.size();
for (int i = 0; i < n; ++i)
{
cout << s1[i] << " ";
}
cout << endl;
for (int i = 0; i < n; ++i)
{
cout << s1.at(i) << " ";
}
cout << endl;
return 0;
}

int main()
{
string s1("yhping");
char cha = s1[1];
char& chb = s1[1];
s1.clear();
s1 = "hello";
cout << chb << endl;
return 0;
}

int main()
{
string s1("yhping");
cout << "size: " << s1.size() << endl;
cout << "Capt: " << s1.capacity() << endl;
cout << "max_size: "<<s1.max_size() <<endl;
s1.reserve(128);
cout << "size: " << s1.size() << endl;
cout << "Capt: " << s1.capacity() << endl;
cout << "max_size: " << s1.max_size() << endl;
}

int main()
{
string s1("yhping");
cout << s1.size() << " " << s1.capacity() << endl;
s1.reserve(200);
for (int i = 0; i < 10; ++i)
{
s1 += "hello";
cout << s1.size() << " " << s1.capacity() << endl;
}
}

#include<vector>
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << "Object" << endl; }
Object(const Object& obj) :value(obj.value) { cout << "Object &" << endl; }
Object(Object&& obj) :value(obj.value) { cout << "move Object&&" << endl; }
Object& operator=(const Object& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
Object& operator=(const Object&& obj)
{
value = obj.value;
cout << "operator=" << endl;
return *this;
}
~Object()
{
cout << "~Object" << endl;
}
};
int main()
{
vector<Object> vecobj;
for (int i = 0; i < 5; ++i)
{
vecobj.push_back(Object(10));
cout << vecobj.size() << endl;
cout << vecobj.capacity() << endl;
}
return 0;
}
反复的扩容导致产生更多的构造和析构函数并且产生了更多的对象 增空间将原来的对象挪到新空间,在释放原来的旧空间 
int main()
{
vector<Object> vecobj;
vecobj.reserve(200);
for (int i = 0; i < 5; ++i)
{
vecobj.push_back(Object(10));
cout << vecobj.size() << endl;
cout << vecobj.capacity() << endl;
}
return 0;
}

随机性迭代器和双向迭代器的区别
int main()
{
vector<int>ivec = { 12,2,3,34,45 };
vector<int>::iterator it = ivec.begin();
it++;
it += 5;
list<int>ilist = { 12,2,3,34,45 };
list<int>::iterator i = ilist.begin();
i++;
i += 5;
return 0;
}
|