#include <list>
int main() {
list<int> mylist;
for (int i = 0; i < 10; i++) {
mylist.push_back(rand() % 100 + 1);
}
for (int i = 0; i < 10; i++) {
mylist.push_front(rand() % 100 + 1);
}
list<int>::iterator it = mylist.begin();
while (it != mylist.end()) {
cout << *it << " ";
++it;
}
cout << endl;
for (it = mylist.begin(); it != mylist.end(); ) {
if (*it % 2 == 0) {
it = mylist.erase(it);
}
else {
++it;
}
}
for (int val : mylist) {
cout << val << " ";
}
cout << endl;
for (it = mylist.begin(); it != mylist.end(); ++it) {
if (*it % 2 == 1) {
it = mylist.insert(it, *it - 1);
++it;
}
}
for (int val : mylist) {
cout << val << " ";
}
cout << endl;
return 0;
}
#include <deque>
int main() {
deque<int> deq;
for (int i = 0; i < 10; i++) {
deq.push_back(rand() % 100 + 1);
}
for (int i = 0; i < 10; i++) {
deq.push_front(rand() % 100 + 1);
}
deque<int>::iterator it = deq.begin();
while (it != deq.end()) {
cout << *it << " ";
++it;
}
cout << endl;
for (it = deq.begin(); it != deq.end();) {
if (*it % 2 == 0) {
it = deq.erase(it);
}
else {
++it;
}
}
for (int val : deq) {
cout << val << " ";
}
cout << endl;
for (it = deq.begin(); it != deq.end(); ++it) {
if (*it % 2 == 1) {
it = deq.insert(it, *it - 1);
it++;
}
}
for (int val : deq) {
cout << val << " ";
}
cout << endl;
return 0;
}
#include <vector>
int main() {
vector<int> vec;
vec.reserve(20);
cout << vec.size() << endl;
/*vec.resize(20);
cout << vec.size() << endl;*/
for (int i = 0; i < 20; i++) {
vec.push_back(rand() % 100 + 1);
}
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
vector<int>::iterator it = vec.begin();
while (it != vec.end()) {
cout << *it << " ";
++it;
}
cout << endl;
for (it = vec.begin(); it != vec.end(); ) {
if (*it % 2 == 0) {
it=vec.erase(it);
}
else {
++it;
}
}
for (int val : vec) {
cout << val << " ";
}
cout << endl;
for (it = vec.begin(); it != vec.end(); ++it) {
if (*it % 2 == 1) {
it = vec.insert(it, *it - 1);
++it;
}
}
for (int val : vec) {
cout << val << " ";
}
cout << endl;
return 0;
}
class A {
public:
A(int data)
:ma(data)
{
cout << "A()" << endl;
}
~A() {
cout << "~A()" << endl;
}
protected:
int ma;
};
class B :virtual public A{
public:
B(int data)
:A(data)
,mb(data)
{
cout << "B()" << endl;
}
~B() {
cout << "~B()" << endl;
}
private:
int mb;
};
class C :virtual public A {
public:
C(int data)
:A(data)
, mc(data)
{
cout << "C()" << endl;
}
~C() {
cout << "~C()" << endl;
}
private:
int mc;
};
class D :public B, public C {
public:
D(int data)
:A(data)
,B(data)
,C(data)
,md(data)
{
cout << "D()" << endl;
}
~D() {
cout << "~D()" << endl;
}
private:
int md;
};
int main() {
D d(20);
cout <<"sizeof(D):"<< sizeof(d) << endl;
return 0;
}
class A {
public:
virtual void func() { cout << "A::func" << endl; }
private:
int ma;
};
class B :virtual public A{
public:
void func() { cout << "B::func" << endl; }
private:
int mb;
};
int main() {
A* p = new B();
p->func();
//delete p;widowns下不允许linux下可以
B b;
A* p1 = &b;
p1->func();
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
return 0;
}
class Base {
public:
virtual void func() = 0;
};
class Derive1 :public Base {
public:
void func() { cout << "Derive1::func" << endl; }
};
class Derive2 :public Base {
public:
void func() { cout << "Derive2::func" << endl; }
void func01() { cout << "Derive2::func01" << endl; }
};
void showFunc(Base* p) {
Derive2* p2 = dynamic_cast<Derive2*> (p);
if (p2 != nullptr) {
p2->func01();
}
else {
p->func();
}
}
int main() {
//const_cast
const int a = 10;
int* p = (int*)&a;
int* p1 = const_cast<int*> ( &a);
double* p2 = (double*)&a;
//double* p3 = const_cast<double*> (&a);No 类型不同
int b = const_cast<int&> (a);
//static_cast 有联系类型转换
char ch = static_cast<char>(a);
int c = 20;
char ch1 = static_cast<char>(c);
int* p4 = nullptr;
//double* p5 = static_cast<double*>(&c);
double d = static_cast<double> (c);
//Derive de();
//Base b1 = static_cast<Base> (de);
double* p6 = reinterpret_cast<double*>(&c);
Derive1 d1;
Derive2 d2;
showFunc(&d1);
showFunc(&d2);
return 0;
}