C++速学学习心得笔记

C++学习心得(本文是我在拥有一定c语言、java入门基础上,花费四天学习c++的一些笔记,由于健忘,故作此可以在日后对于一些容易忘记的内容可以及时查看,复习c++的内容):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

//7-16
/void bubbleSort(int arr, int len)
{
for(int i=0;i for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
struct Student
{
string name;
int age;
int score;
};
void out(Student stu)
{
cout << stu.name <<" “<< stu.age <<” "<< stu.score << endl;
}
void out1(const struct Student * p)
{
//p->score=99;
cout << p->name << " " << p->age << " " << p->score << endl;
}
int main()
{
srand((unsigned int)time(NULL));
int e = rand() % 60 + 40;

int arr[] = { 3,7,1,5,4,9,2,8,6,0 };
int len = sizeof(arr) / sizeof(arr[0]);
for (int n = 0; n < len; n++)
{
	cout << arr[n] << " ";
}
cout << endl;
bubbleSort(arr, len);
for (int k = 0; k < len; k++)
{
	cout << arr[k]<<" ";
}
cout << endl;


struct Student s1;
s1.name = "xiaoming";
s1.age = 12;
s1.score = 98;
struct Student s2 = { "xiaoqiang",14,88 };
struct Student stuArr[2] = {
	{"xiaomi",20,76},
    {"xiaohua",17,80}
};
Student * l = &s2;
cout << l->age << endl;
out(s2);
out1(&s1)



system("pause");
return 0;

}
*/

//7-17
/int func()//栈区
{
int a = 10;
return &a;
}
intfunc1()//堆区
{
int
w = new int(10);
return w;
}
void test()
{
int*arr = new int[10];
delete[]arr;
}

void mySwap(int &a, int &b) //引用传递
{
int temp = a;
a = b;
b = temp;
}

int & test1()
{
int a = 10;
return a;

}
int & test2()
{
static int a = 10;
return a;

}

//封装
const double PI = 3.14;
class Circle
{
public:
int r;
double calculate()
{
return 2 * PI*r;
}
};

class Student
{
public:
string name;
int id;
void showStudent()
{
cout << name << " " << id << endl;
}
void setName(string name)
{
this->name = name;
}
void setId(int id)
{
this->id = id;
}

};

//多态
class Animal
{
public:
virtual void speak()
{
cout << “动物在说话” << endl;
}
};
class Cat :public Animal
{
public:
virtual void speak()
{
cout << “小猫在说话” << endl;
}
};
class Dog :public Animal
{
public:
virtual void speak()
{
cout << “小狗在说话” << endl;
}
};
void doSpeak(Animal &animal)
{
animal.speak();
}
void test1()
{
Cat cat;
doSpeak(cat);
Dog dog;
doSpeak(dog);
}

//计算器类
class AbstractCalculator
{
public:
virtual int getResult()//纯虚函数 virtual void func()=0
{
return 0;
}
int numA;
int numB;
};
class mulCalculator :public AbstractCalculator
{
public:
int getResult()
{
return numA * numB;
}
};
void test3()
{
AbstractCalculator *abc = new mulCalculator;
abc->numA = 10;
abc->numB = 23;
cout << abc->getResult() << endl;
}

int main()
{
/intp = func();//栈区
cout << *p << endl;
//cout << *p << endl;

int*p1 = func1();//堆区
cout << *p1 << endl;
delete p1;

int a = 10;//引用 
int &b = a;//int* const b=&a;
int c = 20;
b = c;//赋值*b=20;
cout << a << b << c << endl;
mySwap(a, b);

int &ref = test1();
cout << ref << endl;
//cout << ref << endl;
int &ref2 = test2();
test2() = 1000;
cout << ref2 << endl;
//防止误操作 const int &b

Circle c1;
c1.r = 10;
cout << c1.calculate() << endl;
Student s1;
s1.setName("xiaoming");
s1.id = 1;
s1.showStudent();
//point.h
//#pragma  once
//#include
//using namespace std;
//源代码
//point.cpp
//成员行为声明
//void Point::getX();

//拷贝构造函数 Person(const Person &p)
//括号法
//Person p1;
//Person p2(10);
//Person p3(p2);拷贝构造函数的调用
//显示法
//Person p2=Person(10);
//Person p3=Person(p2);
//Person(10);匿名对象
//隐式法
//Person p4=10;
//Person p5=p4;
//初始化列表
//Person():m_A(10),m_B(20),m_C(30)
//{}
//Person(int a,int b,int c):m_A(a),m_B(b),m_C(c)
//{}
//静态成员函数
//1.通过对象访问 p.func();2.通过类名访问Person::func();
//静态成员函数只能访问静态成员变量

//this
//person& personadd(person p)
//{
//	return *this
//}
//Person *p=NULL;
//p->func()
//const Person p;常对象,只能调用常函数
//func1() const{}常函数


//friend 类内用friend声明全局函数 friend void good();
//friend  class goodgay;声明友元类
//friend void Good::test();声明类成员函数
//类外写成员函数 Building::Building(){}
//operator+

//继承
//class Java:public/protected/private BasePage
//s.Base::m_A   同名成员
//s.Base::func()
//多继承 class Son:public Base1,public Base2*/

//虚析构
//virtual ~Animal()
//{}
//纯虚析构
//virtual ~Animal=0;
//Aniamal::~Aniamal()
//{}

/*	test1();
test3();
system("pause");
return 0;

}
*/

//7-18
/*#include
void test()
{
ofstream ofs;
ofs.open(“test.txt”, ios::out);
ofs << “zhangsan” << endl;
ofs << “xioaming” << endl;
ofs.close();

}
void test1()
{
ifstream ifs;
ifs.open(“test.txt”, ios::in);
if (!ifs.is_open())
{
cout << “打开失败” << endl;
return;
}
//读数据
//第一种
char buf[1024] = { 0 };
while (ifs >> buf)
{
cout << buf << endl;
}

//第二种
char buf[1024] = { 0 };
while (ifs.getline(buf, sizeof(buf)))
{
	cout << buf << endl;
}

//第四种
char c;
while ((c = ifs.get()) != EOF)
{
	cout << c;
}
//第三种
string buf;
while (getline(ifs, buf))
{
	cout << buf << endl;
}

}

class Person
{
public:

string name;
int age;
Person(string name, int age)
{
	this->age = age;
	this->name = name;
}

};
void test2()
{
ofstream ofs;
ofs.open(“person.txt”, ios::out | ios::binary);
Person p = { “xiaoqiang”,20 };
ofs.write((const char*)&p, sizeof(Person));
ofs.close();
}
/*void test3()
{
ifstream ifs;
ifs.open(“person.txt”, ios::in | ios::binary);
if (!ifs.is_open())
{
cout << “打开失败了” << endl;
return;

}
Person p;
ifs.read((char *)&p, sizeof(Person));
cout << p.name << "  " << p.age << endl;
ifs.close();

}

template
void test01(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
template
bool test03(T &a, T &b)
{
if (a == b)
{
return true;
}
}
template<> bool test03(Person &p1, Person &p2)
{
if (p1.age == p2.age&&p1.name==p2.name)
{
return true;
}
else
{
return false;
}
}
void test02()
{
Person p1(“xiaoming”, 22);
Person p2(“xiaobai”, 22);
if(test03(p1, p2))
{
cout << “对象一样” << endl;
}
else
{
cout << “对象不一样” << endl;
}
}
void test04()
{
int a = 10;
int b = 10;
if (test03(a, b))
{
cout << “对象一样” << endl;
}
else
{
cout << “对象不一样” << endl;
}
}

template
class bigPerson
{
public:
bigPerson(NameType name, AgeType age);
void setPerson();

NameType name;
AgeType age;

};
//成员函数类外实现
template
bigPerson::bigPerson(NameType name, AgeType age)
{
this->name = name;
this->age = age;
}
template
void bigPerson::setPerson()
{

}

void test05()
{
bigPerson p1(“xiaoli”, 25);
}

//继承
class Son :public bigPerson
{

};
template
class Son2 :public bigPerson
{
T1 obj;
};

//容器
void myPrint(int val)
{
cout << val << endl;
}
void myPrintPerson(Person* val)
{
cout << val->name << " " << val->age << endl;
}
void test06()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector::iterator itBegin = v.begin();
vector::iterator itEnd = v.end();
//第一种遍历
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
//第二种遍历
for (vector::iterator it = v.begin(); it != v.end(); it++)
//for(vector // for(vector::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *it << endl;
}
//第三种遍历
for_each(v.begin(), v.end(), myPrint);

}
void test07()
{
vector v;
Person p1(“aa”, 12);
Person p2(“bb”, 15);
Person p3(“cc”, 16);
Person p4(“dd”, 21);
Person p5(“ee”, 32);
Person p6(“ff”, 53);
Person p7(“gg”, 87);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
v.push_back(&p6);
v.push_back(&p7);
for_each(v.begin(), v.end(), myPrintPerson);
}

//string容器
void test08()
{
const char* str = “nihao”;
string s2(str);
int pos = s2.find(“ha”);
int pos1 = s2.rfind(“ha”);//从右往左找
s2.replace(1, 3, “11111”);
for (int i = 0; i < s2.size(); i++)
{
cout << s2[i] << " “;//cout< }
s2.insert(1, “2222”);
s2.erase(2, 2);
string s7 = s2.substr(3, 2);

cout << s2 << endl;
string s3(s2);
int pos2 = s2.compare(s3);
string s4(10, 'g');
//s4.assign(10,'d');
cout << s4 << endl;
string s5;
s5.assign("halohalo",5);
s2 += s3;
cout << s2 << endl;
s2 += ':';
s5.append("nishishui",3,7);

}

//vector容器
void printVector(vector& v)
{
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
}
void test09()
{
vector v;
v.push_back(10);
vectorv2(v.begin(), v.end());
vectorv3(10, 5);
vectorv4(v3);//v4=v3;v4.assign(v3.begin(),v3.end());
printVector(v);
//int p=v.empty();p1=v.capacity();p2=v.size();v.resize(16);v.resize(13,0);v.clear();
//v.pop_back();v.insert(v1.begin(),100);v.insert(v1.begin(),3,99);v.erase(v1.begin());v.erase(v1.begin(),v1.end());
//v[1] v.at(i) ;int p1=v1.front();int p2=v1.back();
//v1.swap(v2); vector(v).swap(v); v.reserve(9999);预留空间

}

int main()
{

//ofs.open("文件路径",打开方式);
//ofs<<"写入的数据";
//ofs..close();
//ios::in
//ios::out
//ios::ate
//ios::app   
//ios::trunc  如果文件存在,删除并重新创建
//ios::binary
//|
//test();
//test1();
//test2();
//test3();
//ifs>>ch;判断文件是否为空文件
//ifs.eof();


//模板
//函数模板(可以实现重载)  template  template
int a = 10;
int b = 20;
char c = 'c';
//自动类型推导
//test01(a, b);
cout << a << "  " << b << endl;
//显示指定类型
test01(a, b);
cout << a << "  " << b << endl;
//test01<>(a,b)  通过空模板可以强制调用函数模板(一般优先普通函数)
test02();
test04();
//类模板
//template

//T* p;
//p=new T[5];
//拷贝构造
//MyArray(const MyArry& arr)
//{
//	this->p = new T[arr.A];
//}

//容器
//test06();
//test07();
test08();
test09();





system("pause");
return 0;

}
*/

//7-19
//deque容器
void printDeque( const deque&d)//只读状态
{
for (deque::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100;
cout << *it << " ";
}
cout << endl;
}
void test1()
{
deque d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
dequed2;
d1 = d1;
dequed3(10,55);
d3.assign(d2.begin(), d2.end());
printDeque(d1);
d1.push_front(20);
d2.pop_front();
d2.insert(d2.begin(), 999);
d2.insert(d2.begin(), 2,9999);
d3.insert(d3.begin(), d1.begin(), d1.end());
deque::iterator it = d1.begin();
it++;
d1.erase(it);
d1.erase(d1.begin(), d1.end());
sort(d2.begin(), d2.end());//升序
}

//stack容器
void test2()
{
stacks;
s.push(10);
while (!(s.empty()))
{
cout << s.top() << endl;
}
s.pop();
}

//queue容器
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
void test3()
{
queueq;
Person p1(“xiao1”, 12);
Person p2(“xiao2”, 23);
Person p3(“xiao3”, 45);
q.push(p1);
q.push(p2);
q.push(p3);
while (!q.empty())
{
cout << q.front().name << " " << q.front().age << endl;
q.pop();
}
}

//list容器
void printList(const list&l)
{
for (list::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test4()
{
listl;
l.push_back(15);
l.push_back(25);
l.push_back(18);
l.push_back(18);
printList(l);
l.reverse();//反转
printList(l);
//sort(l.begin(), l.end());错误
l.sort();
printList(l);
l.remove(18);
printList(l);
}

//set容器,从小到大排序
void printSet(set&s)
{
for (set::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test5()
{
sets1;
s1.insert(12);
s1.insert(25);
s1.insert(54);
s1.insert(38);
printSet(s1);
set::iterator pos = s1.find(38);//找不到返回s1.end()迭代器
int num = s1.count(54);//统计个数
multisets2;
//pair ret=s2.insert(12);
pairp(“xiaoming”, 20);
cout << p.first << " " << p.second << endl;
pairp2 = make_pair(“xiaobai”, 27);
cout << p2.first << " " << p2.second << endl;
s2.insert(25);
s2.insert(25);
s2.insert(38);
}

//map/multimap容器
void printMap(map&m)
{
for (map::iterator it = m.begin(); it != m.end(); it++)
{
cout << (*it).first << " " << it->second << endl;
}
}
void test6()
{
mapm;
m.insert(pair(4, 45));
m.insert(pair(1, 10));
m.insert(pair(3, 30));
m.insert(pair(2, 20));
printMap(m);
m.insert(make_pair(6, 68));
printMap(m);
m.insert(map::value_type(5, 57));
m[0] = 3;
m.erase(0);//按key删
m.erase(m.begin(), m.end());
m.clear();
printMap(m);
map::iterator pos = m.find(4);//找不到返回end()迭代器;
}
class compare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void printMap(map&m)
{
for (map::iterator it = m.begin(); it != m.end(); it++)
{
cout << (*it).first << " " << it->second << endl;
}
}
void test7()
{
mapm;//compare仿函数
m.insert(pair(4, 45));
m.insert(pair(1, 10));
m.insert(pair(3, 30));
m.insert(pair(2, 20));
printMap(m);
}

//函数对象(仿函数)
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
class greater50
{
public:
bool operator()(int v1)
{
return v1>50;
}
};
void test8()
{
MyAdd myadd;
cout << myadd(10, 10) << endl;
}
//一元谓词/二元谓词(仿函数返回类型为bool)
//内建仿函数及算法
void print(int val)
{
cout << val << " ";
}
void test9()
{
//算术仿函数
negaten;//取反仿函数
cout << n(50) << endl;
plusp;
cout << p(10, 11)< minusm;//减法仿函数
multipliesm1;//乘法仿函数
dividesd;//除法仿函数
modulusm2;//取模仿函数
//关系仿函数
vectorv;
v.push_back(10);
v.push_back(99);
sort(v.begin(), v.end(), greater());//大于仿函数
//逻辑仿函数
vectorv1;
v1.push_back(true);
v1.push_back(false);
v1.push_back(false);
vectorv2;
v2.resize(2*v1.size()+v.size());
transform(v1.begin(), v1.end(), v2.begin(), logical_not());
//算法
//遍历算法
for_each(v1.begin(), v1.end(), print);
cout << endl;
//查找算法
vector::iterator pos = find(v.begin(), v.end(), 99);
vector::iterator it = find_if(v.begin(), v.end(), greater50());
if (it != v.end())
{
cout << “找到了” << endl;
}
vector::iterator pos1=adjacent_find(v.begin(), v.end());//查找相邻重复的两个数
bool ret=binary_search(v.begin(), v.end(),10);//必须是有序序列
//排序算法
random_shuffle(v.begin(), v.end());
merge(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());//合并两个有序序列后还是有序序列
//拷贝替换算法
copy(v.begin(), v.end(), v2.begin);
replace(v2.begin(), v2.end(), 99, 9999);
replace_if(v2.begin(), v2.end(), greater50(), 5555);
//算术生成算法
int total=accumulate(v.begin(), v.end(),0);
fill(v2.begin(), v2.end(), 4);//填充
//集合算法
vectorv3;
v3.resize(v.size());
vector::iterator itEnd=set_intersection(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());//获取交集
vectorv4;
v4.resize(v.size() + v2.size());
vector::iterator itEnd1 = set_union(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());//获取并集
vectorv5;
v5.resize(v.size());
vector::iterator itEnd2 = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v5.begin());//获取差集

int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
//test8();
test9();
system(“pause”);
return 0;
}
由于注释的原因,很多代码不完整,只是适用于提醒一些语法的书写和应用,希望对大家有用(本人第一次写博客,很多写法格式啥的都不懂,望见谅)。

你可能感兴趣的:(c++)