12 2013 档案
摘要:反思两个问题 1. 带默认参数的函数,为何声明、定义不能同时有参数? 2. 带默认参数的函数, 为何带默认参数的参数靠后站?上程序#include #include using namespace std;class A{ public: A(const string &a = "hello, nihao!", int b = 67); private: string s; int sb;};A::A(const string &a, int b) : s(a), sb(b){ cout #include #include #...
阅读全文
摘要:会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab、abbbb 等 * 前面元素出现0次或多次 ab*:a、ab、abb 等 ? 匹配前面的一次或0次 Ab?: A、Ab 等 ^ 作为开始标记 ^a:abc、aaaaaa等 :abc、cccc 等 \d
阅读全文
摘要:上代码#include #include using namespace std;class A { public: A(const string &book = "ab") : s(book) {} int same_s(const A &a) const { return s == a.s; } private: string s;};int main(int argc ,char **argv){ A c("aaa"); string m = "aaa"; cout #include using na...
阅读全文
摘要:1. 类成员为const类型2. 类成员为引用类型#include using namespace std;class A{ public: A(int &v) : i(v), p(v), j(v) {} void print_val() { cout using namespace std;class Base{ public: Base(int a) : val(a) {} private: int val;};class A{ public: A(int v) : p(v), b(v) {} ...
阅读全文
摘要:上代码#include using namespace std;class A{ public: A(int v): j(v + 2), i(j) {} void print_val() { cout using namespace std;class A{ public: A(int v): i(v), j(v + 2) {} void print_val() { cout << "hello:" << i << " " << j << endl;} private: int i; int .
阅读全文
摘要:假设文件内容为1. hello1 hello2 hello3 hello42. dsfjdosi3. skfskj ksdfls输出每个单词代码#include #include #include #include #include using namespace std;int main(){ string word; ifstream infile("text"); if(!infile) { cout > word) cout >传到word(间隔福为Tab, Space, Enter)输出每一行代码#include #include...
阅读全文
摘要:本质 '\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以这么写。'\0'就是 字符串结束标志。 '\0'是转义字符,意思是告诉编译器,这不是字符0,而是空字符。空字符\0对应的二进制为00000000,而数字0为00110000 原来,在C语言中没有专门的字符串变量,通常用
阅读全文
摘要:源自c++primer 4th, 248页代码#include #include #include using namespace std;int main(){ int ival; while(cin >> ival, !cin.eof()) { cout ::max(), '\n'); continue; } } }几个地方1. 逗号表达式首先计算每一个操作数,然后返回最右边的操作数最为整个操作的结果,因此while(cin >> ival, !cin.eof())看重的只是!cin.eof(),而对前边的cin>>...
阅读全文
摘要:缘起#include #include #include using namespace std; int main(){ ofstream fs; //输出文件流 (正确) ostringstream os; //输出string流(正确) ostream o; //输出普通流 (错误)}解惑头文件iostream定义了三个类iostream, istream, ostream(三个普通流), 都存在这样的问题:不可以直接定义无参数的对象。原因可以从编译器提示看出 类中无参数的构造函数定义为protected,因此不可直接...
阅读全文
摘要:第4章96页,数组维数为变量第8章246. IO对象不可复制、赋值原因是类设计时复制构造函数、赋值函数是私有的,为什么这么设计呢?251. tie举例第15章484 派生类可以恢复,但不可扩大#include #include using namespace std;class base{ public: base(int p1=1, int p2=2, int p3=3) : pub(p1), pri(p2), pro(p3) {} int pub; private: int pri; protected: int p...
阅读全文
摘要:c++primer 页数:376-379 备份, 很有嚼头#include #include #include using namespace std;class Screen{ public: typedef string::size_type index; Screen(index r, index c) : row(r), col(c), contents(r * c, 0) {}; Screen& set(index r, index c, char val); Screen& set(char val); S...
阅读全文