cout

Strange beep when using cout

我与影子孤独终老i 提交于 2019-12-04 03:40:53
问题 today when I was working on some code of mine I came across a beeping sound when printing a buffer to the screen. Here's the mysterious character that produces the beep: '' I don't know if you can see it, but my computer beeps when I try to print it like this: cout<<(char)7<<endl; Another point of interest is that the 'beep' doesn't originate from my on board beeper, but from my headphone/speaker Is this just my computer or there something wrong with the cout function? EDIT: But then why does

C++ unicode characters printing

老子叫甜甜 提交于 2019-12-04 03:19:41
I need to print some unicode characters on the Linux terminal using iostream . Strange things happen though. When I write: cout << "\u2780"; I get: ➀ , which is almost exactly what I want. However if I write: cout << '\u2780'; I get: 14851712 . The problem is, I don't know the exact character to be printed at compile-time. Therefore I'd like to do something like: int x; // some calculations... cout << (char)('\u2780' + x); Which prints: � . Using wcout or wchar_t instead don't work either. How do I get correct printing? From what I found around on the Internet it seems important that I use g++

C++ cout overwriting itself while in for loop

冷暖自知 提交于 2019-12-04 03:17:54
问题 The cout statement in this for loop: for (vector<Student>::iterator qw = students.begin(); qw != students.end(); ++qw){ Student a = *qw; name = a.getName(); regno = a.getRegNo(); std::cout << "Name: "<< name << " Reg Number: " << regno << endl; } Is creating some odd behavior, what the cout should print is something like this: Name: Mike Sanderson Reg Number: 10101 However which it actually prints out it: Reg Number: 10101on It would seem to me that after the second part of the cout statement

Why does this output of the same expression from printf differ from cout?

只谈情不闲聊 提交于 2019-12-04 02:47:17
问题 I'm using Visual C++ 2012 and compiling from the command line the following files: #include <stdio.h> int main() { printf("%.5f", 18/4+18%4); return 0; } Linking with MSVCRT.LIB rather than LIBCMT to avoid runtime error R6002. The value that is output is 0.00000 for this program. However, if I perform the exact same thing in C++ #include <iostream> using namespace std; int main() { cout << 18/4+18%4 << endl; return 0; } Now, it prints out 6, like it should. What's the difference? Is it to do

为什么“使用命名空间标准”被认为是不好的做法?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 00:36:15
其他人告诉我, using namespace std; 编写 using namespace std; 在代码中是错误的,我应该直接使用 std::cout 和 std::cin 代替。 为什么 using namespace std; 认为是不好的做法? 是效率低下还是冒着声明模棱两可的变量(与 std 名称空间中的函数具有相同名称的变量)的风险? 它会影响性能吗? #1楼 请勿在全球范围内使用 仅在 全局使用 时才被视为“不良”。 因为: 您使正在编程的名称空间混乱。 当您使用 using namespace xyz 使用许多标识符时,读者将很难看到特定标识符的来源。 对于您的源代码的 其他 阅读者而言,正确的是对最常使用它的读者:您自己。 一两年后再来看看... 如果仅谈论 using namespace std 那么您可能不知道要获取的所有内容-当添加另一个 #include 或移动到新的C ++修订版时,可能会遇到您不认识的名称冲突。 您可以在本地使用 继续免费在本地(几乎)使用它。 当然,这可以防止您重复 std:: -重复也是不好的。 在本地使用它的习惯用法 在C ++ 03中,有一个习惯用法-样板代码-用于为您的类实现 swap 功能。 建议您实际上使用一个 using namespace std 的本地-或至少 using std::swap : class

print double with precision 4 using cout [duplicate]

谁说我不能喝 提交于 2019-12-04 00:31:09
This question already has answers here : Closed 6 years ago . Possible Duplicate: Convert a double to fixed decimal point in C++ Suppose , I have double a = 0 and I want to print it as 0.0000 . I've tried this : cout.precision(4) ; cout<<a<<endl ; but it gaves 0 as the output. Just try: #include <iomanip> ... cout << fixed << setprecision(4); cout << a << endl; See here . #include <iomanip> #include <iostream.h> int main() { double a = 0.00; // print a double, 2 places of precision cout << setprecision(4) << a << endl; } 来源: https://stackoverflow.com/questions/13728425/print-double-with

为什么“使用命名空间标准”被认为是不好的做法?

十年热恋 提交于 2019-12-04 00:29:22
其他人告诉我, using namespace std; 编写 using namespace std; 在代码中是错误的,我应该直接使用 std::cout 和 std::cin 代替。 为什么 using namespace std; 认为是不好的做法? 是效率低下还是冒着声明模棱两可的变量(与 std 名称空间中的函数具有相同名称的变量)的风险? 它会影响性能吗? #1楼 请勿在全球范围内使用 仅在 全局使用 时才被视为“不良”。 因为: 您使正在编程的名称空间混乱。 当您使用 using namespace xyz 使用许多标识符时,读者将很难看到特定标识符的来源。 对于您的源代码的 其他 阅读者而言,正确的是对最常使用它的读者:您自己。 一两年后再来看看... 如果仅谈论 using namespace std 那么您可能不知道要获取的所有内容-当添加另一个 #include 或移动到新的C ++修订版时,可能会遇到您不认识的名称冲突。 您可以在本地使用 继续免费在本地(几乎)使用它。 当然,这可以防止您重复 std:: -重复也是不好的。 在本地使用它的习惯用法 在C ++ 03中,有一个习惯用法-样板代码-用于为您的类实现 swap 功能。 建议您实际上使用一个 using namespace std 的本地-或至少 using std::swap : class

Correctly pad negative integers with zeros with std::cout

怎甘沉沦 提交于 2019-12-04 00:26:56
I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or to force cout to always print at least 5 digits (which would result in -00005) as we can do with printf? std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n'; // ^^^^^^^^ Output: -0005 std::internal Edit: For those those that care about such things, N3337 ( ~c++11 ), 22.4.2.2.2 : The location of any padding is determined

C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>

主宰稳场 提交于 2019-12-03 23:57:53
一、概念介绍   unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了 独占式拥有 的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有,一旦拥有者被销毁或变成空或开始拥有另一个对象,那么先前拥有的那个对象就会被销毁,其任何相应资源亦会被释放。   Class unique_pt继承自class auto_ptr(由于不安全已被弃用),但它提供了更简明的接口,更不易出错。 1.1 出现的目的性   为了避免异常时的资源泄露,通常函数会捕获所有的异常,例如: 上图中多个catch时会造成代码臃肿且累赘,使用unique_ptr能很好解决这个问题,改写如下: 1.2 unique_ptr的使用   unique_ptr有着和寻常指针非常相似的接口,*操作符用来提取指向的对象,如果被指向的对象是class或struct,->操作符还可以用来访问成员,然而由于它是独占式的指针,所以不提供++算术符 1 //必须直接初始化,不允许用赋值的语法将一个寻常的指针当作初值 2 //std::unique_ptr<std::string> = new int;//error 3 std::unique_ptr<std::string> up(new std::string("nico"));//OK 4 (*up)[0] = 'X

【洛谷P4589】[TJOI2018]智力竞赛(二分+最小链覆盖)

你说的曾经没有我的故事 提交于 2019-12-03 22:34:48
洛谷 题意: 给出一个 \(DAG\) ,现在要选出 \(n+1\) 条可相交的链来覆盖,最终使得未被覆盖的点集中,权值最小的点的权值最大。 思路: 显然最终的答案具有单调性,故直接二分答案来判断; 直接将小于二分权值的点加入图中,求出最小链覆盖即可。 这个题貌似有点卡常。。二分上界设为INF直接T飞了。。 /* * Author: heyuhhh * Created Time: 2019/11/6 10:20:13 */ #include <bits/stdc++.h> #define MP make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define INF 0x3f3f3f3f #define Local #ifdef Local #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0) void err() { std::cout << '\n'; } template<typename T, typename...Args> void err(T a, Args...args) { std::cout <<