cout

Coverity finding: Not restoring ostream format (STREAM_FORMAT_STATE)

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We are catching a Coverity finding CID 156014: Not restoring ostream format (STREAM_FORMAT_STATE) (text below and image at the end). 938 const std::streamsize oldp = cout.precision(6); 5. format_changed: setf changes the format state of std::cout for category floatfield. 939 const std::ios::fmtflags oldf = cout.setf(std::ios::fixed, std::ios::floatfield); 940 cout << " Maurer Randomness Test returned value " << mv << endl; 6. format_changed: precision changes the format state of std::cout for category precision. 941 cout.precision(oldp); 7.

C++11 lambda returning lambda

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: this piece of code is not something unknown to JS developers function get_counter () { return ( function () { var c = 0 ; return function () { return ++ c ; }; })(); } it basically creates a which creates different enumerators. So I was wondering if same thing can be done in C++11 with new lambda semantics? I ended up writing this piece of C++ which unfortunately does not compile! int main () { int c ; auto a = [](){ int c = 0 ; return [&](){ cout << c ++; }; }; return 0 ; } so I was wondering if there is a workaround to get it

C++编程基础一 28-编程练习一

匿名 (未验证) 提交于 2019-12-03 00:42:01
1 // 28-编程练习一.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include " stdafx.h " 5 #include <iostream> 6 #include <climits> 7 #include <array> 8 #include < string > 9 #include <math.h> 10 11 using namespace std; 12 13 int main() 14 { 15 // 1.下面代码会打印什么内容? 16 int i; 17 for ( int i = 0 ; i < 5 ; i++ ) 18 cout << i; 19 cout << endl; 20 // 答:01234 21 22 // 2.下面代码会打印什么内容 23 int j; 24 for (j = 0 ; j < 11 ; j += 3 ) 25 cout << j; 26 cout << endl << j << endl; 27 // 答:0369 28 // 12 29 // 30 31 // 3.下面代买会打印什么内容? 32 int f = 5 ; 33 while (++f < 9 ) // 单独使用++i和i++时候没有区别,但是放在表达式中时候会有区别。 34 cout << f++ << endl; // i+

C++编程基础一 29-if语句

匿名 (未验证) 提交于 2019-12-03 00:42:01
1 // 29-if语句.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include " stdafx.h " 5 #include <iostream> 6 #include <climits> 7 #include < string > 8 #include <array> 9 #include <math.h> 10 using namespace std; 11 12 int main() 13 { 14 int hp = 0 ; 15 if (hp <= 0 ) 16 { 17 cout << " 游戏结束 " << endl; 18 } 19 // 还可以这么写 20 if (hp <= 0 ) 21 cout << " 游戏结束 " << endl; // 这么写只能默认第一条语句是body 22 23 // if...else语句 24 if (hp <= 0 ) 25 { 26 cout << " 游戏结束 " << endl; 27 } 28 else 29 { 30 cout << " 游戏继续 " << endl; 31 } 32 33 // 年龄保护游戏 34 int age = 60 ; 35 if (age < 18 ) 36 { 37 cout << " 你可以玩3个小时 " << endl; 38 } 39 else 40 { 41

C++编程基础一 15-枚举类型

匿名 (未验证) 提交于 2019-12-03 00:42:01
1 // 15-枚举类型.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include " stdafx.h " 5 #include <iostream> 6 #include <climits> 7 using namespace std; 8 9 enum HeroType // 枚举类型是整型。适合用作标签Tag。 10 { 11 Tank, // 0 12 Magic, // 1 13 ADC, // 2 14 Assist // 3 15 }; 16 17 enum HumanType 18 { 19 Teacher = 1 , // 可以修改其中的值 20 Student = 4 , 21 Engineer = 5 , 22 Famer = 7 23 }; 24 int main() 25 { 26 HeroType heroType = Magic; // 定义一个枚举。 27 heroType = ADC; // 修改枚举的值。 28 cout << heroType << endl; 29 30 HumanType humanType = Famer; 31 // 32 auto myType = humanType + heroType; 33 cout << myType << endl; // 是可以的 34 35 int i =

C++中优先队列的使用

匿名 (未验证) 提交于 2019-12-03 00:40:02
既然是队列那么先要包含头文件 #include <queue> 优先队列具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的 。 定义: priority_queue<Type, Container, Functional> Type Container Functional // 升序队列 priority_queue < int ,vector< int >,greater< int > > q; // 降序队列 priority_queue < int ,vector< int >,less< int > > q; // greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了) 1.基本类型例子: #include<iostream> #include <queue> using namespace std; int main() { // 对于基础类型 默认是大顶堆 priority_queue< int > a; // 等同于 priority_queue<int, vector<int>, less<int> > a; // 这里一定要有空格,不然成了右移运算符↓ priority_queue< int ,

c++学习笔记(一)---数据类型和对象

匿名 (未验证) 提交于 2019-12-03 00:39:02
c++简介: c++是面向对象的编程,特点可以概括为4个字:封装、抽象、继承、多态,把对象的属性和方法集合成一个独立的系统单位,尽可能隐藏对象的内部细节,对具体的问题进行概括的过程,子类对象拥有与基类相同的全部属性和方法,子类继承基类的属性和行为后,可以具有不同的数据类型和表现行为等特性。 常用代码、函数介绍: cout:console out(控制台输出) 数据类型:数组、指针、结构: 一个数组可以把很多同类型的数值存在一个变量名下 e.g #include <iostream> #define ITEM 10 #define input_add() int main() { //const unsigned short ITEM = 10; int num[ITEM]; std::cout << "please input" << ITEM << "int number!\n\n"; input_add(); return 0; } void input_add() { for(int i=0; i<ITEM; i++) { std::cout << "please input" << i+1 << "number\n"; std::cin >> num[i] while(!(std::cin >> num[i])) { std::cin.clear(); std::cin

实验7

匿名 (未验证) 提交于 2019-12-03 00:32:02
11-7 #include <iostream> using namespace : : std ; int main () { ios_base : : fmtflags original_flags = cout . flags (); // 保存cout参数设置 cout << 812 << '|' ; cout . setf ( ios_base : : left , ios_base : : adjustfield ); // 设置cout的对齐方式为左对齐 cout . width ( 10 ); // 将cout宽度改为 10 cout << 813 << 815 << '/n' ; cout . unsetf ( ios_base : : adjustfield ); // 取消cout对齐方式的设置 cout . precision ( 2 ); // 设置浮点数输出的精度值 cout . setf ( ios_base : : uppercase | ios_base : : scientific ); // 设置浮点数的显示参数 cout << 831.0 ; cout . flags ( original_flags ); // 恢复cout原参数设置 return 0 ; } 运行结果 源码: #include <iostream> #include

数据结构之树篇1——二叉树

这一生的挚爱 提交于 2019-12-03 00:26:30
性质 基本性质: 二叉树_百度百科 关于二叉树的深度(高度)涉及到结点的层数,有的教材规定根结点在第 \(0\) 层,有的则规定根结点在第 \(1\) 层。 这里全部以 《数据结构-C语言版》(严蔚敏,吴伟民版) 为准: 高度和深度 为同一个概念, 根节点在第1层 ,树的高度是看一共有几层。 高度为 \(h\) 的完全二叉树,最多有 \(2^{n}-1\) 个节点,第 \(k\) 层最多有 \(2^{k-1}\) 个节点。 含有 \(n\) 个节点的完全二叉树,高度为 \(log(n)+ 1\) 下取整。 完全二叉树: 如果第一个元素下标为 \(1\) ,则第 \(k\) 个元素的左孩子为 \(2*k\) ,右孩子为 \(2*k+1\) ,父节点为 \(k / 2\) 下取整,最后一个父节点为 \(length / 2\) 下取整。 如果第一个元素下标为 \(0\) ,则第 \(k\) 个元素的左孩子为 \(2*k+1\) ,右孩子为 \(2*k+2\) ,父节点为 \((k - 1)/ 2\) 下取整,最后一个父节点为 \(length / 2\) 下取整后减一。 二叉树的前驱后继节点,表示中序遍历时,一个节点的前一个和后一个。 高度为 \(h\) 的完全二叉树, 最多有 \(2^{h}-1\) 个节点。 节点为 \(n\) 的完全二叉树, 高度为 \(log(n)\) 。

C++

匿名 (未验证) 提交于 2019-12-03 00:22:01
列表内容 using namespace std ; //优化编译 没有用到的就不包含进来 // 泛型的概念 自动推理数据类型 //CPP类型检测严格 精准 属于强类型 int *p1(p2) //() 初始化 for each ( int i in a) //遍历a所有的变量 #include<locale> setlocale(LC_ALL, "zh-cn" ); //宽字符本地化 //C++重定向: >重写 >>追加 < <<输入 ``` ###函数模板 函数重载: 根据参数来识别 函数模板: template T add(T a, T b) { return a + b; } “` 原生函数优先于模板函数 强行调用模板 add(1,2) 调用的时候编译 不调用的时候不编译 函数模板意义: 通用 泛型 模板接口: template < class T> void show(T num) { cout << num << endl; } //泛型接口 任何的数据类型 任何指针 template < class T, class F> void run(T t, F f){ f(t); } run( "sss" , show< const char *>); //接口 严格类型 命名空间的扩展: 名称相同 同一个命名空间 命名空间的权限: using namespace data