cc++复习(一):知识点部分梳理

it2023-07-16  77

 

1.如何扣出int型的每个字节的内容?

2.分出不同的字符串

3.用容器实现给五个人打分的代码

4.二分法

5.stack容器

6.queue容器

7.list容器

8.set/mutliset容器

9.map容器

10.调用显示当前时间

11.map容器中条件查找

12.取反适配器

函数对象适配器 给函数对象中的operator()多传参数

一元取反适配器 注意: not1() 继承时使用unary_function继承

二元取反适配器 注意: not2() 继承时使用 binary_function 继承

函数指针适配器 将函数指针适配成函数对象 ptr_fun

成员函数适配器 将成员函数适配器函数指针 mem_fun_ref

13.函数指针适配器

14.成员函数适配器

15.算法

16.给公司员工分配部门

17.演讲比赛

1.如何扣出int型的每个字节的内容?

用位移运算符等。

int a =0x0a;//00 00 00 0a int b =0x00; printf("%02x\n",a|=b); char *p = (char *)&a; printf("%02x\n",*p);

2.分出不同的字符串

用string自带的substr和find函数

string str = "you.love.my.baby"; vector<string> v; int start = 0; int pos = 0; while(1) { pos = str.find(".", start); if (pos<0) { string s = str.substr(start,str.size()- start); v.push_back(s); break; } tring s=str.substr(start, pos - start); v.push_back(s); start = pos + 1; }

分割字符串应用

//求 “apple an is this” void text(char *p) { string str(p); int start = 0; int pos = 0; vector<string> v; while (1) { pos = str.find(" ", start); if (pos<0) { string s = str.substr(start, str.size() - start); v.push_back(s); break; } string s = str.substr(start, pos - start); v.push_back(s); start = pos + 1; } for (int i = v.size() - 1;i >= 0;i--) { cout << v[i] << " "; } } int main() { char *p = "this is an apple"; text(p); string str = "hello"; char *s = const_cast<char *>(str.c_str()); cout << s << endl; return 0; }

3.用容器实现给五个人打分的代码

用vector存储五个人,用deque容器存储打分的分数。

#include <iostream> #include <string.h> #include <string> #include <deque> #include <vector> #include <algorithm> #include <stdlib.h> #include <time.h> #include <stdio.h> using namespace std; class person { public: int grade; string name; person(int grade, string name) { this->grade = grade; this->name = name; } }; void srart_person(vector<person> &v) { deque<int> d; int a[10]; float sorce = 0; int sum = 0; vector<person>::iterator it = v.begin(); for (;it != v.end();it++) { d.clear(); sum = 0; for (int i = 0;i < 10;i++) { a[i] = rand() % 50 + 50; d.push_back(a[i]); } sort(d.begin(), d.end()); d.pop_back(); d.pop_front(); for (int i = 0;i < d.size();i++) { sum += d.at(i); //cout << d.at(i) << " "; } sorce = sum / d.size(); //cout << sorce << endl; (*it).grade = sorce; } } void create_person(vector<person> &v) { string person_name = "ABCDE"; for (int i = 0;i < 5;i++) { string str(1, person_name[i]); person p(0, str); v.push_back(p); } } int main() { // 设置随机种子 srand(time(NULL));//1 是种子 vector<person> v; create_person(v);//初始化对象 srart_person(v); for (int i = 0;i<5;i++) { cout << v[i].grade << v[i].name << endl; } return 0; }

 

4.二分法

前提:可连续,并已排序

#include <stdio.h> int main() { int a[10] = {1,2,3,4,5,6,7,8,9,10}; //用二分法查找a[2]的值 int low = 0; int height =sizeof(a)/sizeof(a[0])-1; int mid = (height + low) / 2;; while (low<=height) { if (a[mid]==2) { printf("找到了,下标为%d\n",mid); break; } else if (a[mid] <2) { low = mid+1; mid = (height + low) / 2; } else if (a[mid] >2) { height = mid-1; mid = (height + low) / 2; } } return 0; }

5.stack容器

先入后出

#include <iostream> #include <string.h> #include <string> #include <stack> #include <algorithm> #include <stdlib.h> using namespace std; class person { public: int age; string name; person(int age,string name) { this->age = age; this->name = name; } }; int main() { person p(10,"孙悟空"); person p1(30, "蔡文姬"); person p2(20, "后羿"); person p3(40, "安其拉"); person p4(50, "白起"); stack<person> s; s.push(p); s.push(p1); s.push(p2); s.push(p3); s.push(p4); while (!s.empty()) { person tmp = s.top(); cout << tmp.age << tmp.name << endl;//输出 s.pop(); } cout << s.size() << endl; return 0; }

6.queue容器

#include <iostream> #include <string.h> #include <string> #include <queue> #include <algorithm> #include <stdlib.h> using namespace std; class person { public: int age; string name; person(int age, string name) { this->age = age; this->name = name; } }; int main() { person p(10, "孙悟空"); person p1(30, "蔡文姬"); person p2(20, "后羿"); person p3(40, "安其拉"); person p4(50, "白起"); queue<person> q; q.push(p); q.push(p1); q.push(p2); q.push(p3); q.push(p4); while (!q.empty()) { cout << q.back().age << q.back().name << endl;//输出最后一个数 cout << q.front().age << q.front().name << endl;//输出 q.pop(); } return 0; }

7.list容器

在list容器中,使用查找函数find,需要重写operator==(const person &p)。

list<person>::iterator it1 =find(it,p.end(),p3);//查找函数 class person { public: int age; string name; person(int age, string name) { this->age = age; this->name = name; } bool operator<(person &p1) { return this->age<p1.age; } ~person() {} bool operator==(const person &p) { return this->age == p.age&&this->name == p.name; } private: };

8.set/mutliset容器

set容器不可以有相同的键值。

set<int>::const_iterator it3 = s.lower_bound(20);//返回第一个>=20的值 cout <<"第一个大于等于20的值"<< *it3 << endl; set<int>::const_iterator it4 = s.upper_bound(20);//返回第一个>20的值 cout << "第一个大于20的值" << *it4 << endl; pair<set<int>::const_iterator, set<int>::const_iterator> it5 = s.equal_range(20); cout << "区间:" << *(it5.first)<< *(it5.second) << endl;

set存储自定义类型

class person { public: int age; string name; person(int age, string name) { this->age = age; this->name = name; } bool operator<(const person &p1) { return this->age<p1.age; } ~person() {} bool operator==(const person &p) { return this->age == p.age&&this->name == p.name; } private: }; class compare1 { public: bool operator()(const person &p1,const person &p2)const { return p1.age<p2.age; } }; ostream& operator<<(ostream &cout, const person &p) { cout << p.age << p.name; return cout; } int main() { person p(10, "孙悟空"); person p1(30, "蔡文姬"); person p2(20, "后羿"); person p3(40, "安其拉"); person p4(50, "白起"); multiset<person,compare1> s; s.insert(p); s.insert(p1); s.insert(p2); s.insert(p3); s.insert(p4); multiset<person>::iterator it = s.begin(); for (;it!= s.end();it++) { cout << *it <<endl; } return 0; }

9.map容器

#include <iostream> #include <string.h> #include <string> #include <map> #include <algorithm> #include <stdlib.h> using namespace std; int main() { map<int, string> m; m.insert(make_pair(10,"李华")); m.insert(make_pair(20, "维护")); m.insert(make_pair(15, "是谁")); m.insert(make_pair(30, "蔡文姬")); m.insert(make_pair(25, "韩信")); m[45] = "huo"; map<int, string>::iterator it = m.begin(); for (;it!=m.end();it++) { cout << (*it).first << " " << (*it).second << endl; } return 0; }

10.调用显示当前时间

头文件:ctime

time_t now = time(0); // 把 now 转换为字符串形式 char* dt = ctime(&now); cout << "本地日期和时间:" << dt << endl;

11.map容器中条件查找

//条件查找 map<int, string>::iterator it1 =find_if(m.begin(),m.end(),compare()); cout << (*it1).first << " " << (*it1).second << endl; class compare { public: bool operator()(const pair<int, string> &m1) { return m1.first >25; } };

12.取反适配器

函数对象适配器 给函数对象中的operator()多传参数

一元取反适配器 注意: not1() 继承时使用unary_function继承

二元取反适配器 注意: not2() 继承时使用 binary_function 继承

函数指针适配器 将函数指针适配成函数对象 ptr_fun

成员函数适配器 将成员函数适配器函数指针 mem_fun_ref

not1:是一元取反适配器

vector<int>::iterator it = find_if(v.begin(),v.end(),not1(greater2())); //取反,>2变为<=2 class greater2:public unary_function<int,bool> { public: bool operator()(int a)const { return a > 2; } };

not2:是二元取反适配器

//取反,<变为> class compare:public binary_function<int,int,bool> { public: bool operator()(int a,int b)const { return a < b; } }; sort(v.begin(),v.end(),not2(compare()));

13.函数指针适配器

//将函数指针适配成函数对象 for_each(v.begin(),v.end(),bind2nd(ptr_fun(point),1000)); void point(int a,int num) { cout << a+num << endl; }

14.成员函数适配器

//成员函数适配器 for_each(it1,it2,mem_fun_ref(&person::show));

15.算法

//搬移,将其他容器的值搬移到里一个容器中去。 transform(v.begin(),v.end(),v1.begin(),filter); //查找计算总数。 cout<<count_if(v1.begin(), v1.end(),bind2nd(greater<int>(),2))<<endl;//条件查找个数 cout << count_if(v1.begin(), v1.end(),dayu2()) << endl;//条件查找个数 //随机输出 srand(time(NULL)); random_shuffle(v1.begin(),v1.end()); //反转 reverse(v1.begin(), v1.end()); //求两个容器里值得交集,需要是有序的数 vector<int> v2; v2.resize(min(v.size(),v1.size())); set_intersection(v.begin(),v.end(),v1.begin(),v1.end(),v2.begin()); //替换,把4换成10 replace(v1.begin(), v1.end(),4,10); //求并集 v2.resize(v.size()+v1.size()); set_union(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin()); //求减掉交集后的 v2.resize(v.size()); set_difference (v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());

16.给公司员工分配部门

#include <iostream> #include <string.h> #include <string> #include <map> #include <vector> #include <algorithm> #include <stdlib.h> #include <ctime> #include <functional> #include <time.h> using namespace std; //公司分配工作 #define department 1 #define moneyment 2 #define technolyment 3 class person { public: int age;//年龄 string name;//姓名 string tel;//电话 person(int age,string name,string tel) { this->age = age; this->name = name; this->tel = tel; } }; void create_person(vector<person> &v) { int age[5]; string t_name[5] = {"张三","李四","王五","赵四","老刘"}; string t_tel[5] = { "13566551111","13965762442","13155247689","15274553709","15274553789" }; for (int i=0;i<5;i++) { age[i] = rand() %30+10; person p1(age[i], t_name[i], t_tel[i]); v.push_back(p1); } vector<person>::iterator it = v.begin(); for (;it!=v.end();it++) { cout << (*it).age << " " << (*it).name << " " << (*it).tel << endl; } } //没有分配 void savetomap_person(vector<person> &v, multimap<int, person> &m) { vector<person>::iterator it1 = v.begin(); int depart[3]; for (int i=0;i<3;i++) { for (;it1 != v.end();it1++) { m.clear(); depart[i] = rand() % 3 + 1; m.insert(make_pair(depart[i], *it1)); map<int, person>::iterator it = m.begin(); for (;it != m.end();it++) { cout<<m.count(1)<<endl; if ((*it).first==department) { cout << "管理部门 :" << endl; cout << (*it).second.age << (*it).second.name << (*it).second.tel << endl; } else if ((*it).first == moneyment) { cout << "销售部门:" << endl; cout << (*it).second.age << (*it).second.name << (*it).second.tel << endl; } else if ((*it).first == technolyment) { cout << "技术部门 :" << endl; cout << (*it).second.age << (*it).second.name << (*it).second.tel << endl; } } } } } void ctearte(vector<person> &v, multimap<int, person> &m) { vector<person>::iterator it1 = v.begin(); for (;it1!=v.end();it1++) { m.insert(make_pair(rand()%3+1,*it1)); } map<int,person>::iterator it = m.begin(); for (;it != m.end();it++) { cout << (*it).first << " " << (*it).second.age<< (*it).second.name << (*it).second.tel << endl; } } void show(multimap<int, person> &m) { int count = 0; map<int, person>::iterator it = m.begin(); for (int i=1;i<4;i++) { if (i == department) { cout << "管理部门 :" << endl; } else if (i == moneyment) { cout << "销售部门:" << endl; } else if (i == technolyment) { cout << "技术部门 :" << endl; } count=m.count(i);//统计数量 it = m.find(i); if (it != m.end()) { for (int j = 0; j < count; j++) { cout << (*it).second.age << (*it).second.name << (*it).second.tel << endl; it++; } } } } int main() { srand(time(NULL)); vector<person> v; multimap<int, person> m; create_person(v);//创建5个员工 ctearte(v,m); show(m); return 0; }

17.演讲比赛

#include <iostream> #include <string.h> #include <string> #include <map> #include <vector> #include <deque> #include <algorithm> #include <stdlib.h> #include <ctime> #include <functional> #include <time.h> #include <numeric> #include <cstring> using namespace std; class player { public: int score[3]; string name; player() {} player(string name) { memset(this->score,0,sizeof(score)); this->name = name; } }; void create_player(vector<player> &v) { string p_name="ABCDEFGHIJKLMNOPQRSTUVEWX"; for (int i=0;i<24;i++) { string str(1,p_name[i]); player p(str); v.push_back(p); } vector<player>::iterator it = v.begin(); } void create_joinplayer(vector<player> &v,map<int, player> &m,vector<int> &v1) { vector<player>::iterator it = v.begin(); for (int i=100;i<124;i++) { v1.push_back(i); m.insert(make_pair(i, *it)); it++; } map<int, player>::iterator it1 = m.begin(); for (;it1 != m.end();it1++) { cout << (*it1).first << " " << (*it1).second.name <<" "<< (*it1).second.score[0] << endl; } } void start_play(map<int, player> &m, vector<int> &v1, vector<int> &v2,int n) { int count = 0; multimap<int, int, greater<int>> tmpmap; int sum = 0; deque<int> d; for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) { if (count == 5) { count = 0; multimap<int, int, greater<int>>::const_iterator cit = tmpmap.begin(); for (int i = 0; i < 3; i++) { v2.push_back(cit->second);//晋级的编号插入到v2 cit++; } tmpmap.clear(); } //打分 10次 sum = 0; d.clear(); for (int i = 0; i < 10; i++) { d.push_back(rand() % 101); } sort(d.begin(), d.end()); d.pop_back();//去除最高 d.pop_front();//祛除最低 int avg = accumulate(d.begin(), d.end(), 0) / d.size();//求平均分 m[*it].score[n - 1] = avg; tmpmap.insert(make_pair(avg, *it)); count++; } } void point(map<int, player> &m, vector<int> &v1, vector<int> &v2, int n) { cout << "参数比赛选手得分信息" << endl; for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) { cout << m[*it].name << " " << m[*it].score[n - 1] << endl; } cout << "晋级选手得分信息" << endl; for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) { cout << m[*it].name << " " << m[*it].score[n - 1] << endl; } } int main() { srand(time(NULL)); vector<player> v; map<int, player> m; vector<int> v1;//保存编号 create_player(v);//创造选手 create_joinplayer(v,m,v1); vector<int> v2;//保存晋级的编号 random_shuffle(v1.begin(), v1.end()); start_play(m,v1,v2,1); point(m, v1, v2, 1); vector<int> v3;//保存晋级的编号 random_shuffle(v2.begin(), v2.end()); start_play(m, v2, v3, 2); point(m, v2, v3, 2); vector<int> v4;//保存晋级的编号 random_shuffle(v3.begin(), v3.end()); start_play(m, v3, v4,3); point(m, v3, v4, 3); return 0; }

 

 

最新回复(0)