cout

which is faster, and which is more flexible: printf or cout? [duplicate]

做~自己de王妃 提交于 2019-11-28 06:35:31
问题 Possible Duplicates: printf vs cout in C++ cin or printf?? I've always wondered about printf and cout.. which one is ultimately faster, and is it the most flexible as well (ie can print a range of variables, and output can be formatted)? P.S. I know this looks similar to 'printf' vs. 'cout' in C++ ,but i'm not really asking the same thing. 回答1: Short Answer Faster : printf More flexible : cout Long answer When compared to the sprintf family, the C++ streams are supposed to be slower (by a

转:C++ 11 Lambda表达式

ⅰ亾dé卋堺 提交于 2019-11-28 06:01:01
转:https://www.cnblogs.com/DswCnblog/p/5629165.html 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

how does cout << actually work?

。_饼干妹妹 提交于 2019-11-28 05:14:27
I was wondering how std::cout is able to use << as it does. My main puzzlement is with whether std::cout as an instance of something. Basically, how is << defined? If I do this for a custom class, I need an instance of some sort... I could see implementing it as kind of a hack with void pointers or something, but I'd like to see the actual way it's done. Does anyone here know? Thanks Billy ONeal std::cout is an instance of std::ostream . std::cout << "something" calls one of the operator<< overloads as would be done for any instance of std::ostream . It's "special" in that it references the

字符串常量初始化指针

偶尔善良 提交于 2019-11-28 04:17:02
今天写个小文说一说字符串地址和字符串常量。 在C/C++中,一个字符串常量表示的是该字符串第一个元素的地址,就跟char指针名,char数组名表示的是字符串第一个元素的地址一样。 想要打印一个地址,用一个简单的 cout << 地址; 语句就可以搞定; 但是下面这两条语句将打印整个字符串 char a[20] = "1234"; cout << a << endl; char *p = a; cout << p << endl; 这也是字符数组 与其他数组不同的一个地方,那么该如何得到该字符串的地址呢? 下面有两种方法可供参考 cout << (int*)a << endl; cout << &a << endl; 这两种方法都可以正确打印出“字符串的地址”,但是有细微区别之处 在字符数组a中,a表示第一个字符的地址,a+1表示第二个字符的地址; 在第一条打印地址的语句中,(int*)a只是起到了一个强制类型转换的作用,换句话说,a表示第一个字符的地址,但是cout <<a;输出的是整个字符串,这是因为这个地址是char*类型的, cout识别到char*类型的地址将会自动打印从该地址指向的空间开始直到遇到'\0'的字符串内容 ,所以这里我们只需要进行一个强制类型转换(这里举例强制转换为int*,可以转换为其他类型的指针,只要不是char*,哪怕是double*也可以正确打印地址)

Why does scanf appear to skip input?

帅比萌擦擦* 提交于 2019-11-28 04:14:13
问题 I am confused about scanf's behaviour in the following program. scanf appears to input once, and then not input again, until a stream of characters is printed. Below in a C program #include<stdio.h> int main() { int i, j=0; do { ++j; scanf("%d", &i); printf("\n\n%d %d\n\n", i, j); } while((i!=8) && (j<10)); printf("\nJ = %d\n", j); return 0; } here, Till i am inputting any integer program works perfectly fine, but when a character is inputted it goes on printing the last inputed value of i

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

女生的网名这么多〃 提交于 2019-11-28 04:08:57
问题 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; ^~~~~~~~~~~

剑指offer3:从尾到头打印链表每个节点的值

只谈情不闲聊 提交于 2019-11-28 03:49:14
1. 题目描述   输入一个链表,从尾到头打印链表每个节点的值。 2. 思路和方法 2.1 推荐的方法   (1)栈,循环   后进先出,我们可以用栈实现这种顺序。没经过一个结点的时候,把该节点放到一个栈里面,当遍历完整个链表后,再从栈顶开始逐个输出结点的值,此时输出的结点的顺序已经反转过来了。 2.2 不推荐的方法   (1)直接修改输入数据   如果可以修改原来链表的结构,那么把链表中链接结点的指针反转过来,改变链表的方向,然后就可以从头到尾输出了。 但是,打印通常是一个只读操作,我们不希望打印时修改内容,所以就得想别的办法。   (2)递归   递归在本质上就是一个栈结构,于是很自然地又想到了用递归来实现。每访问到一个结点的时候,先递归输出它后面的节点,再输出该节点自身,这样链表的输出结果就反过来了。 3. 核心代码 1 class Solution { 2 public: 3 vector<int> printListFromTailToHead(ListNode* head) { 4 vector<int> result; 5 stack<int> nodes; 6 7 ListNode* pNode = head; 8 while (pNode != NULL){ 9 nodes.push(pNode->val); 10 pNode = pNode->next; 11

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

↘锁芯ラ 提交于 2019-11-28 02:50:48
I tried researching the difference between cout , cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on when to use which one? I visited this site which shows a small program on cerr and clog , but the output obtained over there can also be obtained using cout . So, I'm confused over each one's exact use. riv stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program

文件操作

家住魔仙堡 提交于 2019-11-28 02:35:18
文件分为普通文件和二进制文件 普通文件可以直接打开不需要经过特定的软件 字符流 二进制文件 需要特定的软件 字节流 c++通过fstream头文件进行文件的调用 文本文件一般使用 get put 进行读写 二进制文件利用 write和read 1 #include<iostream> 2 #include<fstream>//文件的头文件用于对文件对象的操纵 3 #include<iomanip>// 作用看控制cout的输出 让文件可以通过<<进行输出 4 using namespace std; 5 class A 6 { 7 public: 8 virtual void showData() = 0;//纯虚函数 9 }; 10 class B :public A//类B继承类A 11 { 12 int data;//类B中设置的新成员 13 public: 14 void setData() 15 { 16 data = 0; 17 }//类B中使用的新函数 18 void showData()//父类指针的调用 19 { 20 setData(); 21 cout << data << endl;//利用虚函数调用子类中的函数和变量 22 } 23 }; 24 class C :public A//类C继承类A 利用类A中的纯虚函数重写showData方法 25 { 26

关于verilog实例化的介绍

走远了吗. 提交于 2019-11-28 02:08:24
概念 当我们完成一个比较完整的系统的时候,通常需要编写一个Testbench来验证自己的设计的功能能否满足设计要求。在这个系统中通常会有一个top模块来连接那些小的模块,verilog通过实例化的方式来完成这些子模块和顶层模块的连接,然后顶层模块可以由此来调用各个子模块。 用法 调用模块的端口一般有两种方式,一种是位置关联,一种是名字关联,顺序关联不容易漏掉端口,名字关联容易理解(两个相同的名字肯定好理解啊) 举例说明 子模块(这里以一个简单的全加器模块说明)描述如下: module adder(a,b,cin,s,cout); innput a,b,cin; output cout,s; assign {cout,s} = a + b + cin; endmodule 下面有一个顶层模块调用全加器模块: module top(A,B,CIN,S,COUT); ... adder ADDER(A,B,CIN,S,COUT);//这里采用位置关联 ... endmodule 如果采用名称关联 adder ADDER( .a(A), .b(B), .cin(CIN), .cout(COUT) ); 上面两种关联方式有一些共同点: 1、例化中一定会有一个例化名,比如上面的ADDER,就代表着对adder模块的调用,这个例化名可以自己定,没有什么特别要求。 值得一讲的是名称关联