cout

increment and decrement with cout in C++ [duplicate]

与世无争的帅哥 提交于 2020-02-26 10:29:05
问题 This question already has answers here : Unexpected order of evaluation (compiler bug?) [duplicate] (3 answers) Closed 4 years ago . I'm new to C++ and study the increment and decrement operators. So I tried this example: int x = 4; cout << ++x << " " << x++ << " " << x++ << endl << endl; cout << x << endl; It returns this weird output on C++ .NET and QtCreator and 5 online C++ compilers: 7 5 4 7 The weird thing is that I expect something like this: 5 5 6 7 Can you explain what happens? 回答1:

优先队列

风格不统一 提交于 2020-02-24 15:50:54
许多应用程序都需要处理有序的元素,但不一定要求全部有序。一个合适的数据结构应该支持两种操作:删除最大元素和插入元素。 1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 5 6 struct node 7 { 8 int priority; 9 int value; 10 friend bool operator<(node n1, node n2) 11 { 12 return n1.priority < n2.priority; 13 } 14 }; 15 16 17 struct cmp 18 { 19 bool operator() (const int &a, const int &b) 20 { 21 return a > b; 22 } 23 }; 24 25 int main() 26 { 27 // Use 1 28 cout << "Use 1" << endl; 29 priority_queue<int> qi; 30 qi.push(10); 31 qi.push(78); 32 qi.push(13); 33 qi.push(12); 34 qi.push(50); 35 36 // expect output is 78 50 13 12 10 37 while(!qi

cout小数点输出格式

て烟熏妆下的殇ゞ 提交于 2020-02-24 13:44:39
首先要调用头文件iomanip 假设val = 12.345678 cout << setprecision(4) << val; 结果为:12.34也就是控制位数 cout <<fixed << setprecision(4) << val; 结果为:12.3456也就是控制小数点后有几位 来源: CSDN 作者: 矢月 链接: https://blog.csdn.net/weixin_44397852/article/details/104467056

001:Set

天涯浪子 提交于 2020-02-24 04:26:26
总时间限制: 5000ms 内存限制: 100000kB 描述 现有一整数集(允许有重复元素),初始为空。我们定义如下操作: add x 把x加入集合 del x 把集合中所有与x相等的元素删除 ask x 对集合中元素x的情况询问 对每种操作,我们要求进行如下输出。 add 输出操作后集合中x的个数 del 输出操作前集合中x的个数 ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。 输入 第一行是一个整数n,表示命令数。0<=n<=100000。 后面n行命令,如Description中所述。 输出 共n行,每行按要求输出。 样例输入 7 add 1 add 1 ask 1 ask 2 del 2 del 1 ask 1 样例输出 1 2 1 2 0 0 0 2 1 0 # include <iostream> # include <set> # include <string> # include <cstring> # include <algorithm> using namespace std ; int main ( ) { int n ; multiset < int > s ; set < int > s1 ; //还要在新建一个set用来存所有存过的,来判断他是不是出现过 string st ; /

Ten C++11 Features Every C++ Developer Should Use

南笙酒味 提交于 2020-02-23 23:23:57
原版:http://www.codeproject.com/Articles/570638/Ten-Cplusplus-Features-Every-Cplusplus-Developer 译版:http://blogs.ejb.cc/archives/7190/top-10-new-features-you-should-know-about-c-11 This article discusses a series of features new to C++11 that all developers should learn and use. There are lots of new additions to the language and the standard library, and this article barely scratches the surface. However, I believe some of these new features should become routine for all C++ developers. You could probably find many similar articles evangelizing different C++11 features. This is my attempt to

STL中_Rb_tree的探索

风格不统一 提交于 2020-02-20 06:33:36
我们知道STL中我们常用的 set 与 multiset 和 map 与 multimap 都是基于红黑树。本文介绍了它们的在STL中的底层数据结构 _Rb_tree 的直接用法与部分函数。难点主要是 _Rb_tree 的各个参数的确定。 特别注意在如下代码的 Selector 类用于从 Node 中选出用于排序的key值,这个仿函数必须返回 const int& 而不能是 int ,否则 less<int>::operator(const int&, const int&) 会抛出 segmentation fault 。由于源码中逻辑比较复杂,但是可以观察到内部涉及这方面的地方经常使用到指针。所以可以推测是因为引用了已经释放的局部变量所以才抛出的 segmentation fault 。一开始写成 int ,看了很多源码才发现是这个原因,一定要注意。 接下来是样例代码,里面都有注释了。 #include <iostream> #include <iomanip> // 原则上不要直接引用这个头文件,这里只是为了测试 #include <bits/stl_tree.h> using namespace std; struct Node { int first, second; Node(int _first, int _second) : first(_first),

C++ 命名空间

馋奶兔 提交于 2020-02-20 05:26:07
C++ 命名空间 假设这样一种情况,当一个班上有两个名叫 Zara 的学生时,为了明确区分它们,我们在使用名字之外,不得不使用一些额外的信息,比如他们的家庭住址,或者他们父母的名字等等。 同样的情况也出现在 C++ 应用程序中。例如,您可能会写一个名为 xyz() 的函数,在另一个可用的库中也存在一个相同的函数 xyz()。这样,编译器就无法判断您所使用的是哪一个 xyz() 函数。 因此,引入了 命名空间 这个概念,专门用于解决上面的问题,它可作为附加信息来区分不同库中相同名称的函数、类、变量等。使用了命名空间即定义了上下文。本质上,命名空间就是定义了一个范围。 定义命名空间 命名空间的定义使用关键字 namespace ,后跟命名空间的名称,如下所示: 1 namespace namespace_name { 2 // 代码声明 3 } 为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称,如下所示: 1 name::code; // code 可以是变量或函数 让我们来看看命名空间如何为变量或函数等实体定义范围: 1 #include <iostream> 2 using namespace std; 3 4 // 第一个命名空间 5 namespace first_space{ 6 void func(){ 7 cout << "Inside first

C++ cout 按2、8、10、16进制输出

a 夏天 提交于 2020-02-19 19:23:09
#include <iostream> #include <bitset> using std::cout; using std::endl; int main(int argc,char *argv[],char *envp[]) { cout<<"binary: "<<std::bitset<8>(16)<<endl;//输出八位二进制数16, 00010000 cout<<std::showbase<<std::oct<<"octal: "<<16<<endl;// 020 cout<<std::showbase<<std::dec<<"decimal: "<<16<<endl;// 16 cout<<std::showbase<<std::hex<<"hexdecimal: "<<16<<endl;// 0x10 return 0; } 来源: CSDN 作者: zing2000 链接: https://blog.csdn.net/zing2008/article/details/104393432

ABC155E - Payment

懵懂的女人 提交于 2020-02-17 21:52:33
简述题意,给你一个大数,你可以选择10的次幂进行加减运算,问如何用最少的次数从0到达这个大数 考虑从这个大数到0,从最低位开始,每次都将这个位置取完,2种策略,贪心的话不好处理进位的情况,可以想到是DP 设dp[i][0]为取到第i位,将第i位直接拿完的最小次数,dp[i][1]为取到第i位,进位后拿完的最小次数,可以得到状态转移,num表示第i位的数字 dp[i][0] = min(dp[i-1][0], dp[i-1][1]+1) + num,dp[i-1][1]-1表示进了一位,所以第i位就要+1 dp[i][1] = 10 - num + min(dp[i-1][0], dp[i-1][1]-1) 同理,dp[i-1][1]进了一位,num相当于(num+1), 10-(num+1) = 10 - num - 1 注意初始化状态,最终取答案的时候要在最高位的下一位统计,因为最高位可能也进位了,相当于放了一个虚0 #include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; const int maxm = 1e6+5; int dp[maxm][2]; void run_case() { string str; cin >> str; dp

第二章 开始学习c++

点点圈 提交于 2020-02-17 15:10:16
第二章 开始学习c++ 2.1 进入c++ 2.1.1 基本知识 1.c++对大小写敏感,区分大小写 2.c语言使用printf(),scanf()等输入输出,c++可以使用,但需包含stdio.h头文件。 3. #include:预处理器编译指令 int main():函数头 using namespace:编译指令 2.1.2 main() 函数 1.c++句法要求main() 函数的定义以函数头int main() 开始 c++函数可被其他函数激活和调用,函数头描述了函数与调用它的函数之间的接口。 位于函数名前面的部分叫做函数返回类型,它描述的是从函数返回给调用它的函数的信息,函数名后括号中的部分叫做形参列表;他描述的是从调用函数传递给被调用的函数的信息。 通常,main()被启动代码调用,而启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁。事实上,该函数头描述的是main()和操作系统之间的接口 main()函数可以给调用它的函数返回一个整数值,且不从调用它的函数哪里获得任何信息。 2.main() 函数若无返回语句,则默认为return 0; c++程序必须包含一个名为main() 的函数 2.1.3 c++预处理器和iostream文件 c++程序使用输入输出工具,要使用这样两行代码 #include using namespace std; c+