cout

c++可变参数模板函数

风流意气都作罢 提交于 2019-11-29 17:14:59
可变参数模版函数 类型一致 可变参数 “...” 使用头文件(cstdarg, va_list arg_ptr//开头指针 va_start(arg_ptr, n)//从开头开始读取n个 va_arg(arg_ptr,T)//根据数据类型取出数据 va_end(arg_ptr)//结束读取 template < typename T > T addSum( int n , T t ...) { va_list arg_ptr; //创建开头指针 va_start (arg_ptr, n); //从开头开始读取n个 T res(0); for ( int i = 0; i < n ; i++) { res += va_arg (arg_ptr, T ); //根据数据类型读取数据 } va_end (arg_ptr); return res; } cout << addSum(4, 1.1, 2.1, 3.1, 4.1) << endl; 类型不一致 tempname<typename T>//typename 比class作用域更广 <typename...Args>//typename ...Args可变参数 show(...arg)//不能省略“...” //可变参数的函数模版需要使用递归终止 void show() //在可变参数调用完后调用 { } template <

What is run first inside a cout statement? (C++17)

自古美人都是妖i 提交于 2019-11-29 16:44:38
Say for example I have a long statement like cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n"; would findCurrent() be run before findLowest() like logic dictates? Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent() is called first, then findLowest() and so on. C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2 ): The expression E1 is sequenced before the expression E2 . [over.match.oper]/2: (describing overloaded operators) the operands are sequenced in the order prescribed for the

C++ 11 Lambda表达式

荒凉一梦 提交于 2019-11-29 16:43:40
C++11的一大亮点就是引入了Lambda表达式。利用Lambda表达式,可以方便的定义和创建匿名函数。对于C++这门语言来说来说,“Lambda表达式”或“匿名函数”这些概念听起来好像很深奥,但很多高级语言在很早以前就已经提供了Lambda表达式的功能,如C#, Python 等。今天,我们就来简单介绍一下C++中Lambda表达式的简单使用。 声明Lambda表达式 Lambda表达式完整的声明格式如下: [capture list] (params list) mutable exception-> return type { function body } 各项具体含义如下 capture list:捕获外部变量列表 params list:形参列表 mutable指示符:用来说用是否可以修改捕获的变量 exception:异常设定 return type:返回类型 function body:函数体 此外,我们还可以省略其中的某些成分来声明“不完整”的Lambda表达式,常见的有以下几种: 序号 格式 1 [capture list] (params list) -> return type {function body} 2 [capture list] (params list) {function body} 3 [capture list] {function

redirecting cout into file c++

不打扰是莪最后的温柔 提交于 2019-11-29 16:34:51
my problem is I have a couple of cout's in various files in the project. I would like all of them to be redirected and saved in .txt file, and what I achieved by now is that only one cout is saved in the file. I don't want to create separate .txt for each cout , for the sake of reading them at once. My code looks now like this: #include <fstream> #include <string> #include <iostream> int main() { std::ofstream out("out.txt"); std::cout.rdbuf(out.rdbuf()); std::cout << "get it3"; std::cout << "get it4"; } Both cout are in one file, but assuming they are in two different, how to redirect and

constexpr and std::cout working on function but not in lambda

有些话、适合烂在心里 提交于 2019-11-29 14:46:18
问题 Why constexpr does not work with std::cout , but works with printf ? #include <iostream> constexpr void f() { std::cout << ""; } //error constexpr void g() { printf(""); } //ok And why std::cout works with lambdas constexpr ? #include <iostream> int main () { auto h = []() constexpr { std::cout << ""; }; //ok } 回答1: Technically, it doesn't work with any of them. From [dcl.constexr]: For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument

加速C++ cin,cout的速度

这一生的挚爱 提交于 2019-11-29 14:32:45
用以下两行代码: ios::sync_with_stdio(false); //加速 cin.tie(0); 首先sync_ with_ stdio(false)是为了打断iostream输入输出到缓存,可以节约很多时间,使之与scanf相差无几。 tie是将两个stream绑定的函数,空参数的话返回当前的输出指针,即tie(0)与tie(nullptr)来解决cin与cout的绑定。 来源: https://www.cnblogs.com/Bella2017/p/11519670.html

C++ Change Output From “cout”

懵懂的女人 提交于 2019-11-29 14:21:23
Is it possible to change text printed with "cout"? I would like to make it show the current percentage of something without having to have a new line for each percentage. Is this possible? This works for me: std::cout << "1111"; std::cout << "\r"; std::cout << "2222"; \r is a carriage return symbol. Puts the "cursor" back to the beginning of the line. Alternatively you can use \b character. This is backspace. When printed it goes one character back. In general it is not possible. (imagine that the output from cout is fed directly to a printer. How would you instruct it to "unprint" the last

函数使用

夙愿已清 提交于 2019-11-29 12:31:53
       函数使用 为什么要使用函数 ? 一个较为复杂的系统往往需要若干个子系统,然后分别对这些子系统进行开发。通常将相对独立的,经常使用的功能抽象为函数,函数编写好以后可以被重复利用,这样有利于代码重用,提升开发效率,增强程序可靠性,也便于分工合作和修改。 ---c++语言程序设计函数部分     从上面这段话中我们可以提炼出使用函数的几个优点! 代码重复利用 代码分区分块,便于修改和维护 增强程序的可靠性 下面我就用一个简单的打印程序来说明函数的方便性。 #include <iostream> using namespace std; void print(int a) { cout << a << endl; } int main() { int a, b,sum; a = 3, b = 4; sum = a + b; print(sum); sum = b - a; print(sum); return 0; } 这段代码就体现了函数的分块性和代码重复利用性,每当我看一下代码计算过程的值,我就调用一下 print 函数,就不需要在反复写cout语句了,其次打印函数和主函数是分开的,维护起来只用看有问题的部分,互不影响。   为什么要用函数重载 ? 两个以上的函数,具有相同的函数名,但是形参的个数或者类型不同,编译器根据实参和形参类型及个数的最佳匹配

c++类的构造函数

£可爱£侵袭症+ 提交于 2019-11-29 11:26:20
类的构造函数 类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。 构造函数可用于为某些成员变量设置初始值。 下面为实例: 1 #include<iostream> 2 using namespace std; 3 class Line 4 { 5 public: 6 void setLength(double len); 7 double getLength(void); 8 Line();//为构造函数 9 10 private: 11 double length; 12 }; 13 14 //成员函数定义,包括构造函数 15 Line::Line(void) 16 { 17 cout << "Object is being created" << endl; 18 } 19 void Line::setLength(double len) 20 { 21 length = len; 22 } 23 double Line::getLength(void) 24 { 25 return length; 26 } 27 28 //程序主函数 29 int main() 30 { 31 Line line; 32 //设置长度 33 line.setLength(6.0); 34

C++ : &(std::cout) as template argument

白昼怎懂夜的黑 提交于 2019-11-29 11:05:56
Why isn't it possible to pass std::cout 's address as template argument? Or if it is possible then how? Here is what I tried: #include <iostream> template<std::ostream* stream> class MyClass { public: void disp(void) { (*stream) << "hello"; } }; int main(void) { MyClass<&(std::cout)> MyObj; MyObj.disp(); return 0; } And the error message I got from clang++ -std=c++11 : main.cpp:15:11: error: non-type template argument does not refer to any declaration MyClass<&(std::cout)> MyObj; ^~~~~~~~~~~ main.cpp:6:24: note: template parameter is declared here template<std::ostream* stream> ^ 1 error