cout

不一般的C++多文件程序结构

♀尐吖头ヾ 提交于 2020-02-08 20:35:08
预处理指令 C++中要把.h或者.cpp文件翻译成可执行文件主要经过 预处理——编译——链接 预处理主要是检测文件的可执行性; 预处理指令以**#**开始 命名空间是C++语言的新特征 命名空间是类、函数、对象、类型等名字的声明 集合 std;是C++语言的 标准命名空间 ,包含了标准头文件中各种名字的声明; 标准头文件:iostrenm cstring iomanip 其中 namespace 是C++的 关键字 ,用于说明,命名空间。声明之后,程序可以使用iostream中的 全部 组件(例如cin、cout) 如果不用using声明名空间,则需要在使用时指定组件的名空间 # include <iostream> using std :: cin ; using std :: cout ; //指定使用std的cout元素 int main ( ) { int a , b ; cin >> a ; cin >> b ; cout << "a+b" << a + b << '\n' ; } 方发二 # include <iostream> int main ( ) { int a , b ; std :: cin >> a ; //声明使用std中的cin std :: cin >> b ; std :: cout << "a+b" << a + b << '\n' ; }

Interview_C++_day2

青春壹個敷衍的年華 提交于 2020-02-08 19:43:23
函数指针 在编译过程中,每一个函数都有一个入口地址,而函数指针就是指向该入口地址的指针。 #include<iostream> using namespace std; void fun1(int x) { cout << x << endl; } void fun2(int x) { cout << x+x <<endl; } int main() { void (*pf)(int); pf = fun1; pf(222); pf = fun2; pf(222); } 多态性和虚函数(virtual) 静态多态主要表现为重载,在编译时就确定了。 动态多态的基础是虚函数机制,在运行期间动态绑定,决定了基类调用哪个函数。 #include<iostream> using namespace std; class Shape { public: void show() { // 未定义为虚函数 cout << "Shape::show()" << endl; } void virtual show() { // 定义为虚函数 cout << "Shape::show()" << endl; } }; class Line : public Shape { public: void show() { cout << "Line::show()" << endl; } }; class

PAT1073 Scientific Notation

随声附和 提交于 2020-02-08 19:23:51
原文: 我的个人博客 原题链接 1073 Scientific Notation 思路   简单的字符串操作。不过要细心一点。n保存E后面的字符串所对应的数字,t保存E前面的字符串,不包括符号位。当n小于0时表示向前移动,那么先输出0. 然后输出abs(n)-1个0,然后继续输出t中的所有数字;当n>0时候表示向后移动,那么先输出第一个字符,然后将t中尽可能输出n个字符,如果t已经输出到最后一个字符(j == t.length())那么就在后面补n-cnt个0,否则就补充一个小数点。 然后继续输出t剩余的没有输出的字符~ 代码 #include <iostream> using namespace std; int main() { string s; cin >> s; int i = 0; while (s[i] != 'E') i++; string t = s.substr(1, i-1); int n = stoi(s.substr(i+1)); if (s[0] == '-') cout << "-"; if (n < 0) { cout << "0."; for (int j = 0; j < abs(n) - 1; j++) cout << '0'; for (int j = 0; j < t.length(); j++) if (t[j] != '.') cout

C++ 截取字符串

荒凉一梦 提交于 2020-02-07 08:40:51
#include <iostream> #include <string> using namespace std; int main() { string s1("HI I am a pretty boy!"); cout << "s1.substr(10, 6) = " << s1.substr(10, 6) << endl; //截取子字符串,从第10个字符开始截取长度为6的子字符串 string s2("I am a s and am a b"); cout << "s2.find(am) = " << s2.find("am") << endl; //寻找子字符串的位置 cout << "s2.rfind(am) = " << s2.rfind("am") << endl; //从字符串末尾开始寻找 string s3("interesting"); cout << "s3 = " << s3 << endl; cout << "s3.erase(3, 5) = " << s3.erase(3, 5) << endl; //删除子字符串,从第3个字符开始删除长度为5的子字符串,调用之后会直接改变原字符串 string s4("amazing"); cout << "s4 = " << s4 << endl; cout << "s4.replace(3, 2) = " <<

设计模式-工厂模式

元气小坏坏 提交于 2020-02-06 08:57:13
简单工厂模式 属于设计模式类型中的创建型模式,所谓创建型模式是指:在创建对象的同时隐藏创建逻辑的方式,而不是用new运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加的灵活。 工厂模式是我们常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。我们经常用户new来创建对象,例如,B b = new B()。 为什么需要工厂模式?面向对象的封装和分派告诉我们,尽量将长的代码分派“切割”成每一段,将每段再“封装”起来(减少段与段之间的耦合联系性),这样就会将风险分散,以后如果需要修改,只要更改每段,不会再发生牵一动百的事情。 简单工厂模式将创建实例的工作和使用实例的工作分开,将对象的创建交给专门的工厂负责。缺点在于工厂类不够灵活,增加新的产品需要修改工厂类的判定逻辑。 #include "stdafx.h" #include <iostream> using namespace std; // 产品基类 class Book { public : virtual void read() = 0; // 抽象基类 }; // 书籍1 class Book1 : public Book { public : void read() { cout << "This book is c++.\n" ; } }; // 书籍2 class Book2 :

C++ 大学MOOC 北大课程(郭炜老师)听课整理 第四周

给你一囗甜甜゛ 提交于 2020-02-05 18:59:07
运算符重载基本概念 1)目的是拓展原C程序运算符的作用范围,使程序看起来更加简洁 2)本质是函数,可以称之为运算符函数 3)可以定义为普通函数,也可定义为成员函数 4)把含运算符的表达式转换成函数的调用 5)运算符操作数转换为函数的参数 6)运算符函数可以重载,调用时根据参数类型选择 例如: class complex { public : double real , imag ; complex ( double r = 0.0 , double i = 0.0 ) : real ( r ) , imag ( i ) { } complex operator - ( const complex & r ) ; } ; complex operator + ( const complex & c1 , const complex & c2 ) { return complex ( c1 . real + c2 . real , c1 . imag + c2 . imag ) ; } complex complex :: operator - ( const complex & r ) { return complex ( real - r . real , imag - r . imag ) ; } int main ( ) { complex a ( 4 , 4 ) , b (

How do I change a C++ output stream to refer to cout?

眉间皱痕 提交于 2020-02-04 11:41:29
问题 I have a class that I want to give an output stream as a member to, to wit: class GameBase { protected: ofstream m_OutputWriter; ... } There is a method in this class that takes a string argument and opens m_OutputWriter to point to that file, so data may be output to that file by using the standard << operator; However, what I would like is to make the stream point to cout by default, so that if the output path is not specified, output goes to the console output instead of to a file, and it

预定义函数对象和函数适配器

旧时模样 提交于 2020-02-04 06:34:38
1 )预定义函数对象基本概念:标准模板库STL 提前定义了很多预定义函数对象, #include <functional> 必须包含。 //1使用预定义函数对象: //类模板plus<> 的实现了: 不同类型的数据进行加法运算 void main() { plus<int> intAdd; int x = 10; int y = 20; int z = intAdd(x, y); //等价于 x + y cout << z << endl; plus<string> stringAdd; string myc = stringAdd("aaa", "bbb"); cout << myc << endl; vector<string> v1; v1.push_back("bbb"); v1.push_back("aaa"); v1.push_back("ccc"); v1.push_back("zzzz"); //缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。 //为了降序,可以传递预定义的类模板greater,它调用底层元素类型的大于操作符: cout << "sort()函数排序" << endl;; sort(v1.begin(), v1.end(), greater<string>() ); //从大到小 for (vector<string>:

蓝桥杯训练 日期训练

对着背影说爱祢 提交于 2020-02-04 03:07:19
这道题做起来还是有些麻烦的,做了一个小时多点,码力不行呀。。。 思路: 首先输入a的年月日,b存储2011,11,11,交换两个日期变量,让日期a 存储较小的日期,日期b存储较大的。 我们需要计算出两个日期之间相差多少天,我想的是计算 a日期所在年过去了多少天sum1,再计算b日期所在年还有多天没过sum2,最后用a的年份到b的年份的总天数-(sum1+sum2),得到两个日期差了多少天。 最后还得分类一下两个日期是否交换过,有无交换过处理不同。。注释有。。 代码 #include <iostream> #include <malloc.h> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> using namespace std; typedef long long LL; const int maxn = 1000 + 10; const int inf = 0x3f3f3f3f; int c[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Data { int year, month, day; }; bool check(int n) { if ((n % 100 != 0 && n % 4

google protocol 入门 demo

扶醉桌前 提交于 2020-02-03 01:02:08
ubunbu系统下google protobuf的安装 说明: 使用protobuf时需要安装两部分: 第一部分为*.proto文件的编译器,它负责把定义的*.proto文件生成对应的c++类的.h和.cpp文件; 第二部分是protobuf的c++动态库(由protobuf的源码编译生成),该部分在生成链接生成可执行文件时需要使用到。 1. 安装编译google protobuf源文件时需要的依赖文件 sudo apt-get install autoconf automake libtool curl make g++ unzip 2. 下载google protobuf 的c++对应的源码,并解压至当前目录中 wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.2/protobuf-cpp-3.11.2.tar.gz tar -xf protobuf-cpp-3.11.2.tar.gz 3. 进入解压后的目录,并安装 ./configure make make check sudo make install sudo make ldconfig google protobuf的使用 1. 新建一个表示电话薄的addressbook.proto文件 touch addressbook