北大C++
北大C++学习笔记
WarYan
这个作者很懒,什么都没留下…
展开
-
039:我自己的 ostream_iterator
039:我自己的 ostream_iterator题面描述程序填空输出指定结果#include <iostream>#include <list>#include <string>using namespace std;template <class T1,class T2>void Copy(T1 s,T1 e, T2 x){ for(; s != e; ++s,++x) *x = *s;} template<cla原创 2020-10-24 19:24:30 · 716 阅读 · 0 评论 -
037:函数对象的过滤器
037:函数对象的过滤器题面描述程序填空输出指定结果#include <iostream>#include <vector>using namespace std;struct A { int v; A() { } A(int n):v(n) { }; bool operator<(const A & a) const { return v < a.v; }};// 在此处补充你的代码template <class T&原创 2020-10-24 19:24:07 · 927 阅读 · 0 评论 -
035:按距离排序
035:按距离排序题面描述程序填空,输出指定结果#include <iostream>#include <cmath>#include <algorithm>#include <string>using namespace std;template <class T1,class T2>struct Closer {// 在此处补充你的代码};int Distance1(int n1,int n2) { return原创 2020-10-24 19:23:22 · 1172 阅读 · 0 评论 -
034:goodcopy
034:goodcopy题面描述编写GoodCopy类模板,使得程序按指定方式输出#include <iostream>using namespace std;template <class T>struct GoodCopy {// 在此处补充你的代码};int a[200];int b[200];string c[200];string d[200];template <class T>void Print(T s,T e) {原创 2020-10-24 19:22:21 · 737 阅读 · 3 评论 -
033:排序,又见排序!
033:排序,又见排序!题面描述自己编写一个能对任何类型的数组进行排序的mysort函数模版。只能写一个mysort模板,不能写mysort函数!#include <iostream>using namespace std;bool Greater2(int n1,int n2) { return n1 > n2;}bool Greater1(int n1,int n2) { return n1 < n2;}bool Greater3(double d1原创 2020-10-24 19:21:27 · 765 阅读 · 0 评论 -
032:这个模板并不难
032:这个模板并不难题面描述程序填空,输出指定结果#include <iostream>#include <string>#include <cstring>using namespace std;template <class T> class myclass {// 在此处补充你的代码~myclass( ) { delete [] p; } void Show() { for( int i = 0;i < si原创 2020-10-24 19:20:55 · 1061 阅读 · 2 评论 -
031:山寨版istream_iterator
031:山寨版istream_iterator题面描述模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出#include <iostream>#include <string>using namespace std;template <class T>class CMyistream_iterator{// 在此处补充你的代码};int main() { int t原创 2020-10-24 19:20:07 · 557 阅读 · 0 评论 -
030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?
030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?题面描述读入两个整数,输出两个整数 ,直到碰到-1#include <iostream>using namespace std;class MyCin{// 在此处补充你的代码};int main(){ MyCin m; int n1,n2; while( m >> n1 >> n2) cout << n1 <原创 2020-10-22 22:06:44 · 2832 阅读 · 0 评论 -
029:简单的Filter
029:简单的Filter题面描述编写Filter模板,使得程序产生指定输出 不得编写 Filter函数#include <iostream>#include <string>using namespace std;// 在此处补充你的代码bool LargerThan2(int n){ return n > 2;}bool LongerThan3(string s) { return s.length() > 3;}string as1原创 2020-10-22 22:06:13 · 769 阅读 · 0 评论 -
028:简单的foreach
028:简单的foreach题面描述编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数#include <iostream>#include <string>using namespace std;// 在此处补充你的代码void Print(string s){ cout << s;}void Inc(int & n){ ++ n;}string array[100];int a[100];int原创 2020-10-22 22:05:39 · 1194 阅读 · 0 评论 -
027:简单的SumArray
027:简单的SumArray题面描述填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数#include <iostream>#include <string>using namespace std;template <class T>T SumArray(// 在此处补充你的代码}int main() { string array[4] = { "Tom","Jack","Ma原创 2020-10-22 22:04:55 · 1821 阅读 · 0 评论 -
026:怎么又是Fun和Do
026:怎么又是Fun和Do题面描述程序填空输出指定结果#include <iostream>using namespace std;class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; virtual void Do() { cout << "A::Do" << endl; }};class B:public A原创 2020-10-22 22:03:51 · 236 阅读 · 0 评论 -
025:这是什么鬼delete
025:这是什么鬼delete题面描述程序填空输出指定结果#include <iostream> using namespace std;class A { public: A() { }// 在此处补充你的代码}; class B:public A { public: ~B() { cout << "destructor B" << endl; } }; int main() { A * pa; pa = new B;原创 2020-10-22 22:02:36 · 369 阅读 · 0 评论 -
024:Fun和Do
024:Fun和Do题面描述程序填空输出指定结果#include <iostream> using namespace std;class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; void Do() { cout << "A::Do" << endl; } }; class B:public A {原创 2020-10-22 21:59:44 · 409 阅读 · 3 评论 -
023:看上去像多态
023:看上去像多态题面描述程序填空产生指定输出#include <iostream>using namespace std;class B { private: int nBVal; public: void Print() { cout << "nBVal="<< nBVal << endl; } void Fun() {cout << "B::Fun" << endl; }原创 2020-10-22 21:58:45 · 640 阅读 · 0 评论 -
022:编程填空:统计动物数量
022:编程填空:统计动物数量题面描述代码填空,使得程序能够自动统计当前各种动物的数量#include <iostream>using namespace std;// 在此处补充你的代码void print() { cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::numbe原创 2020-10-22 21:58:23 · 634 阅读 · 0 评论 -
020:继承自string的MyString
020:继承自string的MyString描述程序填空,输出指定结果#include <cstdlib>#include <iostream>#include <string>#include <algorithm>using namespace std;class MyString:public string{// 在此处补充你的代码};int main(){ MyString s1("abcd-"),s2,s3("efgh原创 2020-10-22 21:56:37 · 2185 阅读 · 0 评论 -
019:全面的MyString
019:全面的MyString题面描述程序填空,输出指定结果#include <cstdlib>#include <iostream>using namespace std;int strlen(const char * s) { int i = 0; for(; s[i]; ++i); return i;}void strcpy(char * d,const char * s){ int i = 0; for( i = 0; s[i]; ++i)原创 2020-10-21 23:00:01 · 2078 阅读 · 1 评论 -
043:冷血格斗场
043:冷血格斗场题面描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。我们假设格斗的实力可以用一个非负整数表示,称为实力值,两人的实力值可以相同。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有多个人的实力值与他差别相同,则他会选择id最小的那个。不幸的原创 2020-10-21 21:30:48 · 919 阅读 · 0 评论 -
042:热血格斗场
042:热血格斗场题面描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家热血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。我们假设格斗的实力可以用一个正整数表示,成为实力值。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有两个人的实力值与他差别相同,则他会选择比他弱的那个(显然,虐人必被虐好)。不幸的是,原创 2020-10-20 23:19:38 · 1085 阅读 · 0 评论 -
041:Set
041:Set题面描述现有一整数集(允许有重复元素),初始为空。我们定义如下操作:add x 把x加入集合del x 把集合中所有与x相等的元素删除ask x 对集合中元素x的情况询问对每种操作,我们要求进行如下输出。add 输出操作后集合中x的个数del 输出操作前集合中x的个数ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。输入第一行是一个整数n,表示命令数。0<=n<=100000。后面n行命令,如Descr原创 2020-10-20 23:19:18 · 738 阅读 · 0 评论 -
040:List
040:List题面总时间限制:4000ms内存限制:65536kB描述写一个程序完成以下命令: new id ——新建一个指定编号为id的序列(id<10000) add id num——向编号为id的序列加入整数num merge id1 id2——合并序列id1和id2中的数,并将id2清空 unique id——去掉序列id中重复的元素 out id ——从小到大输出编号为id的序列中的元素,以空格隔开输入第一行一个数n,表示有多少个命令( n<=200000原创 2020-10-20 23:02:34 · 933 阅读 · 2 评论 -
017:第四周程序填空题3
017:第四周程序填空题3题面描述写一个二维数组类 Array2,使得下面程序的输出结果是:0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,程序:#include <iostream>#include <cstring>using namespace std;class Array2 {// 在此处补充你的代码};int main() { Array2 a(3,4);原创 2020-10-17 19:30:45 · 1013 阅读 · 0 评论 -
016:惊呆!Point竟然能这样输入输出
016:惊呆!Point竟然能这样输入输出题面描述程序填空#include <iostream> using namespace std;class Point { private: int x; int y; public: Point() { };// 在此处补充你的代码}; int main() { Point p; while(cin >> p) { cout << p << endl;原创 2020-10-17 19:30:15 · 682 阅读 · 0 评论 -
015:看上去好坑的运算符重载
015:看上去好坑的运算符重载题面描述程序填空#include <iostream> using namespace std;class MyInt { int nVal; public: MyInt( int n) { nVal = n ;}// 在此处补充你的代码}; int Inc(int n) { return n + 1;}int main () { int n; while(cin >>n) { MyInt objInt(n原创 2020-10-17 19:29:45 · 1080 阅读 · 0 评论 -
014:MyString
014:MyString题面描述补足MyString类,使程序输出指定结果#include <iostream>#include <string>#include <cstring>using namespace std;class MyString { char * p;public: MyString(const char * s) { if( s) { p = new char[strlen(s) + 1]; strcpy(p,原创 2020-10-17 19:29:13 · 582 阅读 · 2 评论 -
012:这个指针哪来的
012:这个指针哪来的题面描述填空,按要求输出#include <iostream>using namespace std;struct A{ int v; A(int vv):v(vv) { }// 在此处补充你的代码};int main(){ const A a(10); const A * p = a.getPointer(); cout << p->v << endl; return 0;}思路首先看到aaa是c原创 2020-10-11 12:10:26 · 551 阅读 · 0 评论 -
011:Big & Base 封闭类问题
011:Big & Base 封闭类问题题面描述程序填空,输出指定结果#include <iostream>#include <string>using namespace std;class Base {public: int k; Base(int n):k(n) { }};class Big{public: int v; Base b;// 在此处补充你的代码};int main(){ int n; while(cin >原创 2020-10-11 12:09:56 · 1482 阅读 · 0 评论 -
0010:返回什么才好呢
0010:返回什么才好呢题面描述程序填空,使其按要求输出#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码};int main(){ int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj() = m; cout原创 2020-10-11 12:09:29 · 594 阅读 · 0 评论 -
009:哪来的输出
009:哪来的输出题面描述程序填空,输出指定结果#include <iostream>using namespace std;class A { public: int i; A(int x) { i = x; }// 在此处补充你的代码};int main(){ A a(1); A * pa = new A(2); delete pa; return 0;}思路先构造的后析构,所以是先输出2在输出1然后写好析构函数就可以#include <原创 2020-10-11 12:08:55 · 306 阅读 · 0 评论 -
008:超简单的复数类
008:超简单的复数类题面描述下面程序的输出是:3+4i5+6i请补足Complex类的成员函数。不能加成员变量。#include <iostream>#include <cstring>#include <cstdlib>using namespace std;class Complex {private: double r,i;public: void Print() { cout << r <原创 2020-10-11 12:07:38 · 2033 阅读 · 6 评论 -
007:返回什么才好呢
007:返回什么才好呢题面描述程序填空,使其按要求输出#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码};int main(){ int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj() = m; cout原创 2020-10-11 12:03:25 · 1330 阅读 · 5 评论 -
006:奇怪的类复制
006:奇怪的类复制题面描述程序填空,使其输出9 22 5#include <iostream>using namespace std;class Sample {public: int v;// 在此处补充你的代码};void PrintAndDouble(Sample o){ cout << o.v; cout << endl;}int main(){ Sample a(5); Sample b = a; PrintAndDou原创 2020-10-11 12:02:55 · 535 阅读 · 0 评论 -
005:编程填空:学生信息处理程序
005:编程填空:学生信息处理程序题面描述实现一个学生信息处理程序,计算一个学生的四年平均成绩。要求实现一个代表学生的类,并且类中所有成员变量都是【私有的】。补充下列程序中的 Student 类以实现上述功能。#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <sstream>#include <cstdlib&原创 2020-10-11 12:02:24 · 741 阅读 · 0 评论 -
004:神秘的数组初始化
004:神秘的数组初始化题面描述填空,使得程序输出指定结果#include <iostream>using namespace std;int main(){ int * a[] = {// 在此处补充你的代码}; *a[2] = 123; a[3][5] = 456; if(! a[0] ) { cout << * a[2] << "," << a[3][5]; } return 0;}思路首先我们看到!a[0原创 2020-10-11 12:01:25 · 1093 阅读 · 0 评论 -
003:好怪异的返回值
003:好怪异的返回值题面描述填空,使得程序输出指定结果#include <iostream>using namespace std;// 在此处补充你的代码getElement(int * a, int i){ return a[i];}int main(){ int a[] = {1,2,3}; getElement(a,1) = 10; cout << a[1] ; return 0;}思路容易观察的是getElementgetEleme原创 2020-10-11 11:59:57 · 407 阅读 · 0 评论 -
002:难一点的swap
题面描述填空,使得程序输出结果是:5,3#include <iostream>using namespace std;void swap(// 在此处补充你的代码){ int * tmp = a; a = b; b = tmp;}int main(){ int a = 3,b = 5; int * pa = & a; int * pb = & b; swap(pa,pb); cout << *pa << "," &原创 2020-10-06 12:37:24 · 615 阅读 · 0 评论 -
001:简单的swap
题面描述填空,使得程序输出结果是:5,3#include <iostream>using namespace std;class A{ public: int x; int getX() { return x; } };void swap(// 在此处补充你的代码){ int tmp = a.x; a.x = b.x; b.x = tmp;}int main(){ A a,b; a.x = 3; b.x = 5; swap(a,b); cou原创 2020-10-06 12:36:53 · 499 阅读 · 0 评论 -
第三周:程序设计预算法(三)
题面001:返回什么才好呢未懂1.A& GetObj()//没有明白此处的this指针,定义这个函数的作用为没有明白#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码 n=123) { val = n; } A& GetOb...原创 2019-12-14 22:53:49 · 341 阅读 · 0 评论 -
第二周:程序设计预算法(三)
题面001:编程填空:学生信息处理程序#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <sstream>#include <cstdlib>using namespace std;class St...原创 2019-12-14 22:38:01 · 237 阅读 · 0 评论