cout

C++继承

馋奶兔 提交于 2019-12-01 05:31:47
继承与派生 一、相关概念: 继承:保持已有类的特性而构造新类的过程 派生:在已有类的基础上新增自己的特性而产生新类的过程 小结:同一过程从不同角度看 好处: 代码的 可重用性 和 可扩充性 以及基类的 可靠性 二、访问控制 从基类继承的成员,其访问属性由继承方式控制。类的继承方式有 public (公有继承)、 protected (保护)、 private (私有继承)三种。不同的继承方式,导致原来具有不同访问属性的基类成员在派生类中的访问属性也有所不同。 公有继承 当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问。 例如: #include<iostream> #include<cmath> using namespace std; class Point { //基类:点类 public: void initPoint(float x = 0, float y = 0) { this->x = x; this->y=y; } void move(float offX, float offY) { x += offX; y += offY; } float getX()const { return x; } float getY()const { return y; } private: float x, y; };

Indenting Paragraph With cout

邮差的信 提交于 2019-12-01 04:52:55
Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl; And the desired output: This is a short string that isn't indented. This is a very long string that

替换密码技术【密码学笔记】

心不动则不痛 提交于 2019-12-01 04:34:16
概念   替换密码是基于符号替换的密码技术,以符号的置换来达到掩盖文明信息的目的。 1、单字符单表替换密码技术 单表单字符的含义是对明文中所有的字符都使用一个固定的映射,即: 任何明文加密、密文解密均使用同一个密码表。 明文中相同的字母,必被加密成相同的密文字母。 (1)乘法密码技术 乘法密码技术的密匙是k。k满足gcd(k,n)=1,若n为素数,则有n-2个密匙(k=1是恒等变换,即加密后的密文和明文是一样的,应当舍弃);若n不是素数,则有φ(n)-1个密匙(同样是舍弃了为1的情况)。 解密变换中的 k -1 为 k 对模 n 的逆,满足:    所以,在已知密匙的情况下,k -1 是可以求出来的。 【例1】 英文字母 n = 26,取密匙 k = 9。 明文:m = a man liberal in his views 密文:c = a ean vujkxav un lug hukqg 根据k=9,n=26可以求出来k -1 为3。 测试代码: #include <iostream> #include <string> #include <stdio.h> #include <vector> #include <algorithm> #include <fstream> using namespace std; char encypt(char c) { if (c != '

C++ What is wrong with using a toString() method

最后都变了- 提交于 2019-12-01 04:21:37
I just came across this question which is about how to be able to print an object via std::cout << x << std::endl; As I understood, the standard way to accomplish this is to overload the ostreams << operator. However, this is adding a feature to the ostream rather than to my class. The alternative (also given as answer to the above mentioned question) is to override the string conversion operator. However, this comes with the warning of leading to "unintentional conversions and hard-to-trace bugs". Now i wonder if there are any drawbacks of writing a toString() method and then using it via std

C++ What is wrong with using a toString() method

我与影子孤独终老i 提交于 2019-12-01 02:07:32
问题 I just came across this question which is about how to be able to print an object via std::cout << x << std::endl; As I understood, the standard way to accomplish this is to overload the ostreams << operator. However, this is adding a feature to the ostream rather than to my class. The alternative (also given as answer to the above mentioned question) is to override the string conversion operator. However, this comes with the warning of leading to "unintentional conversions and hard-to-trace

How to create functions like std::cout?

故事扮演 提交于 2019-12-01 01:55:01
I'm creating my own logging utility for my project, I want to create a function like iostream's std::cout, to log to a file and print to the console as well. Here's what i want: enum { debug, error, warning, info }; LOG(level) << "test"; // level - from the above enum The result should be like this: int iPlayerID = 1337; LOG(info) << "Player " << iPlayerID << "Connected"; [Thu Jan 29 18:32:11 2015] [info] Player 1337 Connected jrok std::cout is not a function, it's an object of type std::ostream which overloads operator<< . A quick sketch of how you could do it: enum Level { debug, error,

C++ cout with prefix

喜你入骨 提交于 2019-12-01 01:54:55
I want a ostream with a prefix at the beginning of every line redirected on cout; I try this: #include <iostream> #include <thread> class parallel_cout : public std::ostream { public: parallel_cout(std::ostream& o):out(o){} template <typename T> std::ostream& operator<< (const T& val) { out << "prefix " << val; return *this; } std::ostream& out; }; int main() { parallel_cout pc(std::cout); pc<<"a\nb"<<"c\n"; } but i have in output prefix a b without c. why this? The way you modify the behavior of std::ostream is not by overloading any of the output operators! Instead, you derive a class from

Strange output, not as expected

[亡魂溺海] 提交于 2019-12-01 01:43:30
sorry for asking you a stupid question, but I just can't figure out why I keep on getting this output. So here is my code: #include <cstdio> #include <iostream> using namespace std; unsigned n = 4242; int getRemainderOf(int m, int n, int& quotient); static int l = 0; int main() { int quotient; // the value of which should be changed after calling the func. for(int i=-1; i<=1; ++i) { for(int j=-1; j<=1; ++j) { if( i && j ) { cout << "("<< i*3 << "," << j*7 << ") " <<( getRemainderOf(i*3, 7*j, quotient) ) << " " << quotient <<endl; cout << "("<< i*7 << "," << j*3 << ") " << getRemainderOf(i*7, 3

Codeforces Round #591

旧巷老猫 提交于 2019-12-01 01:31:34
目录 Contest Info Solutions A. Save the Nature B. Sequence Sorting C. Paint the Tree D. Stack Exterminable Arrays Contest Info Practice Link Solved A B C D E F 4/6 O O O O - - O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Solutions A. Save the Nature 题意: 有 \(n\) 个数 \(p_i\) 。 如果一个下标它是 \(a\) 的倍数,那么这个位置的价值就是 \(a_i \cdot x\%\) 如果一个下标它是 \(b\) 的倍数,那么这个位置的价值就是 \(a_i \cdot y\%\) 如果一个下标它是 \(lcm(a, b)\) 的倍数,那么这个位置的价值就是 \(a_i \cdot (x + y)\%\) 现在可以给 \(p_i\) 重新排序,问排序最最少多少个前 \(y\) 个数,使得前 \(y\) 个数的价值和大于等于 \(k\) 。 思路: 二分答案,然后将大的贪心放在大的位置上。 代码: view code #include <bits/stdc++.h> #define debug(...) { printf("# "); printf(_

Educational Codeforces Round 74 (Rated for Div. 2)

拟墨画扇 提交于 2019-11-30 23:36:42
传送门 A. Prime Subtraction 判断一下是否相差为 \(1\) 即可。 B. Kill 'Em All 随便搞搞。 C. Standard Free2play 题意: 现在有一个高度为 \(h\) 的悬崖,每一层有平台,但可能是隐藏状态。 高度为 \(h\) 的那层平台一定是在外面的,假设当前高度为 \(x\) ,那么每次可以改变 \(x\) 和 \(x-1\) 层平台的状态。 规定一个人若从 \(x\) 掉到 \(x-1\) 或者 \(x-2\) 都没事,否则就出事了。 问最少改变多少平台的状态,能够使在 \(h\) 高度的人顺利到达地面(高度为 \(0\) )。 思路: 我大概模拟了一下这个过程,然后发现对于连续的 \(x,x-1,\cdots,x-k\) ,最终答案是否加一与 \(x\) 和 \(x-k\) 的奇偶性相关。 并且从 \(h_i\) 到 \(h_{i+1}\) ,假设中间有空平台,其实最后到达的是 \(h_{i+1}-1\) ,然后就这样模拟一下这个过程就行了。 注意一下细节:最后到地面的时候记得判断一下高度是否大于 \(2\) ,否则答案会加一。 代码如下: Code #include <bits/stdc++.h> #define MP make_pair #define fi first #define se second #define