cout

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

六眼飞鱼酱① 提交于 2019-11-30 09:20:41
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 } 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 values exist such that an invocation of the function or constructor could be an evaluated subexpression of a

P2830 写程序

谁说我不能喝 提交于 2019-11-30 08:31:20
题目背景 zrz在写程序,他叫你帮他看看他的程序有没有问题。 题目描述 有一个若干行的程序,每一行只有一个命令,可能出现的命令有一下几种 int a[maxn] 声明一个数组,开头一定是int,不会是别的什么longlong之类的,a是指一个数组的名称(不一定是a,也有可能是别的字母或者多个字母,总之长度不超过10),后面是一个中括号和一个数字或一个变量,表示数组大小(从0到maxn-1,maxn<=100),数组声明之后里面的数均为0。 a[i] h 把h赋给a[i](也就是a[i]=h),同样h可能是一个数字或者是一个变量,i代表一个数字或者是一个变量。 cout h 输出h,h一定是个变量。 输入格式 若干行:每行一个命令 输出格式 对于每一个输出的命令(即cout),输出一行。如果在某一行发现有数组下标越界(切记,只可能出现这种错误,不会出现别的比如重定义之类的问题),不管是哪个命令,都要立即停止,无论下面有多少行命令都忽略,并输出-1。 输入输出样例 输入 #1 复制 int a[10] a[a[0]] 2 cout a[0] 输出 #1 复制 2 输入 #2 复制 int a[10] a[0] 10 cout a[0] a[a[0]] 1 cout a[0] 输出 #2 复制 10 -1 说明/提示 行数不会太多的,变量可能嵌套,如 a[a[b[0]]]等等

C++模板

余生长醉 提交于 2019-11-30 07:39:33
一、函数模板 我们可能会遇到功能相同,但参数不同,而不得不写大量重载函数,如求绝对值,根据参数是整数还是浮点数而重载函数。 这不仅会导致冗余,如果修改算法时,没有各个函数体中同步修改,会造成在同一个系统中,处理同类型的问题,用的算法不一致。 解决:使用模板 求绝对值问题 语法: template <模板参数表> 函数定义 模板参数表的内容 类型参数:class(或typename) 标识符 常量参数:类型说明符 标识符 模板参数:template <参数表> class标识符 //9_1.cpp #include <iostream> using namespace std; template <class T> //定义函数模板 void outputArray(const T *array, int count) { for (int i = 0; i < count; i++) cout << array[i] << " "; //如果数组元素是类的对象,需要该对象所属类重载了流插入运算符“<<” cout << endl; } int main() { const int A_COUNT = 8, B_COUNT = 8, C_COUNT = 20; int a [A_COUNT] = { 1, 2, 3, 4, 5, 6, 7, 8 }; double b[B_COUNT

Difference between “endl” and “\\n” [duplicate]

一笑奈何 提交于 2019-11-30 07:22:52
Possible Duplicate: C++: “std::endl” vs “\n” I'm wondering if there is any significant difference between these two ways to print newline : cout << endl; //approach1 cout << "\n"; //approach2 Is there any practical difference? Yes, they're different. "\n" is just a string of length 1 that gets appended to stdout. std::endl , instead, is an object that will cause to append the newline character ( "\n" ) AND to flush stdout buffer. For this reason it will take more processing. 来源: https://stackoverflow.com/questions/4512631/difference-between-endl-and-n

What can explain std::cout not to display anything?

别来无恙 提交于 2019-11-30 06:44:18
For whatever reason, std::cout does not display anything with my application. The description of my development environment follows. I am working on a Qt application using Qt Creator. Since Qt Creator can't be launched from my station (XP64), i am currently developping it with Visual Studio 2008 and the Qt plugin (by importing the .pro project file). Everything seems fine and the application works. In some cases (depending on command line arguments), i don't want to launch the HIM, just to display a few sentences in the CLI (command line required arguments, for instance). I don't get any error

what does cout << “\n”[a==N]; do?

假如想象 提交于 2019-11-30 06:29:05
问题 In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout , but it does not print a newline when the value of a is equal to N . 回答1: I have no clue about what the [] option does in cout This is actually not a cout option, what is happening is that "\n" is a string literal. A string literal has the type array of n const char , the [] is simply an index into an array of characters which in this case contains: \n\0 note \0 is appended to all string literals

std::cout to print character N times

有些话、适合烂在心里 提交于 2019-11-30 04:17:20
How can I print a character N number of times using std::cout without looping? Is there a way to move the text cursor back to nullify the effect of std::cout << std::endl; ? i.e. to move up a line (say we never printed anything after doing the std::cout << std::endl; operation). std::cout << std::string(100, '*') << std::endl; To move a line up, you have to resort to terminal escapes (assuming that isatty() indicates that you are running on one). std::cout << std::setfill(the_char) << std::setw(100) << ""; is there a way to back our way to nullify the effect of cout << endl; i.e. to move up a

C++ - STL - map的基础操作

别来无恙 提交于 2019-11-30 03:25:17
STL - map常用方法 map简述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,其作用类似于python之中的字典,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。 map的基本操作函数: | 方法 | 作用 | | | | ---------------- | ------------------------------- | | | | begin() | 返回指向map头部的迭代器 | | | | clear() | 删除所有元素 | | | | count() | 返回指定元素出现的次数 | | | | empty() | 如果map为空则返回true | | | | end() | 返回指向map末尾的迭代器 | | | | equal_range() | 返回特殊条目的迭代器对 | | | | erase() | 删除一个元素 | | | | find() | 查找一个元素 | | | | get_allocator() | 返回map的配置器 | |

C++输入

☆樱花仙子☆ 提交于 2019-11-30 02:19:27
整行读取 std::getline string s; getline(cin, s); cout << s << endl; 利用 scanf 的正则特性 char s[100]; scanf("%[^\n]%*c", s); printf("%s\n", s); std::gets (deprecated) char s[100]; gets(s); printf("%s\n", s); cin.get (std::basic_istream::get) char s[100]; cin.get(s,100); printf("%s\n", s); cin.getline (std::basic_istream::getline) char s[100]; cin.getline(s, 100); printf("%s\n", s); 比较常用的自然是方法1,简单省事儿。 比较一下上述方法4和方法5: cin.get 可以有1个、2个、3个…参数,这里用的是2个参数的形式。 此时它和cin.getline的两个形参是相同格式的: basic_istream& get(char_type* s, streamsize count) 不同点在于: 1) 输入字符串不超过指定值时:(包含直接按回车的情况) get(char*, size) 遇到 '\n' 停止,但不会从输入流中删去

How to remove last character put to std::cout?

别等时光非礼了梦想. 提交于 2019-11-30 02:00:07
Is it possible on Windows without using WinAPI? You may not remove last character. But you can get the similar effect by overwriting the last character. For that, you need to move the console cursor backwards by outputting a '\b' (backspace) character like shown below. #include<iostream> using namespace std; int main() { cout<<"Hi"; cout<<'\b'; //Cursor moves 1 position backwards cout<<" "; //Overwrites letter 'i' with space } So the output would be H No. You can't without accessing the console's api that is never standard. This code does exactely that std::cout<<"\b \b"; 来源: https:/