cout

C++ reset locale to “C” globally?

时间秒杀一切 提交于 2019-12-22 04:10:48
问题 In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes. Commas have been inserted at every third decimal. Ie. cout << 123456789 << endl used to print out 123456789 and now it prints 123,456,789 . This is horribly annoying, because this behavior is not what I want. This issue is not only apparent in the binary I am compiling, but also shows up in all the couts

c++ how does cout print a char* [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-22 01:43:50
问题 This question already has answers here : cout << with char* argument prints string, not pointer value (6 answers) Closed last year . In the code below, the function getName() returns a char * . I would of thought that it should (it also can) return a string . How does cout correctly print it to the console if it is just a pointer to the first char? #include <iostream> #include <string> using namespace std; class Base { protected: int m_value; public: Base(int value) : m_value(value) { } const

PAT算法笔记

ⅰ亾dé卋堺 提交于 2019-12-21 20:05:42
知识点总结 定义之后一定要初始化 尽量都使用 vector ,速度 vector > set > map 两个关键字排序简洁写法 return (cnt!=a.cnt)?cnt>a.cnt:value 使用 10*1.0 的方式去输出小数,除非必要。否则可以不用定义 double 一定要考虑到为 0 或 1 这些边界条件 传参数尽量使用引用来加快速度 先存进 vector 再排序比直接使用 set 要快 将字母编号时。字母和数字都进行映射,这样方便根据字母取数字,根据数字取字母 堆栈不要用 for 设置无限大统一使用 10亿 尽量使用 c 的输出。必要时输入输出可以 C/C++ 混用 string name(ch) 将字符数组 ch 转化为 string 字符串 printf("%%"); 打印一个 % string.c_str() ,将 string 类型转化为 char* 类型,以便 printf() 输出。 auto - 声明变量时根据初始化表达式自动推断该变量的类型,少了初始化就会出错 getline (cin,name) - 以 '\n' 结束 int - 10^9 long long - 10^18 long long 类型常量需要在后面加上 LL long long bignum=1234456789097LL 碰到浮点都用 double while(get(str)

“cout” and “char address” [duplicate]

☆樱花仙子☆ 提交于 2019-12-21 16:48:58
问题 This question already has answers here : cout << with char* argument prints string, not pointer value (6 answers) Closed 2 years ago . char p; cout << &p; This does not print the address of character p. It prints some characters. Why? char p; char *q; q = &p; cout << q; Even this does not. Why? 回答1: I believe the << operator recognizes it as a string. Casting it to a void* should work: cout << (void*)&p; std::basic_ostream has a specialized operator that takes a std::basic_streambuf (which

STL之list容器用法

落爺英雄遲暮 提交于 2019-12-21 12:26:36
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。 使用list容器之前必须加上<vector>头文件:#include<list>; list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std; 构造函数   list<int> c0; //空链表   list<int> c1(3); //建一个含三个默认值是0的元素的链表   list<int> c2(5,2); //建一个含五个元素的链表,值都是2   list<int> c4(c2); //建一个c2的copy链表   list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。 成员函数 c.begin() 返回指向链表第一个元素的迭代器。 c.end() 返回指向链表最后一个元素之后的迭代器。 1 list<int> a1{1,2,3,4,5}; 2 list<int>::iterator it; 3 for(it = a1.begin();it!=a1.end();it++){ 4 cout << *it << "

Codeforces Round #606 (Div. 2) D. Let's Play the Words?(贪心+map)

旧时模样 提交于 2019-12-21 05:01:44
👾 👾 👾 题意:给你一些序列,要求把这些序列翻转之后能首尾相连(01,10),并且字符串不能相同,询问最小步数; 1.我们只关心这个字符串 首尾位置 ,一共只有四种情况:00,01,10,11;00 和 11 是没必要翻转的,剩下 01,10 只要存在就可以相互抵消(0110,1001这种),剩下多的 01 or 10,就是我们最后需要翻转的字符串,数量为abs(num01-num10)/2; 2.为了满足字符串不能相同, 一开始就记录下哪写字符串不可以翻转 ,例,假如s翻转之后为rev,rev出现过则说明s不可翻转,而同时 rev也不可翻转,因为rev翻转之后为s,而s是一定不会变的 模一模大佬的代码QAQ int n ; string s [ MAXN ] ; int32_t main ( ) { //fast; int t ; cin >> t ; while ( t -- ) { cin >> n ; for ( int i = 0 ; i < n ; ++ i ) cin >> s [ i ] ; set < string > seen , bad ; int zz = 0 , oo = 0 , zo = 0 , oz = 0 ; for ( int i = 0 ; i < n ; ++ i ) { string t = s [ i ] ; if ( t [ 0 ] =

Insight into how things get printed onto the screen (cout,printf) and origin of really complex stuff that I can't seem to find on textbooks

三世轮回 提交于 2019-12-20 10:02:21
问题 I've always wondered this, and still haven't found the answer. Whenever we use "cout" or "printf" how exactly is that printed on the screen?. How does the text come out as it does...(probably quite a vague question here, ill work with whatever you give me.). So basically how are those functions made?..is it assembly?, if so where does that begin?. This brings on more questions like how on earth have they made openGl/directx functions.. break it down people break it down.:) 回答1: Here's one

How can I preserve leading zeros when reading a number and printing it later?

微笑、不失礼 提交于 2019-12-20 07:42:45
问题 I want to cout an integer starting 0. For example, 0123456789. But when I print it out, it only display 123456789 instead of 0123456789 . How can I solve this problem? Below is my sample code: sample.txt 0125961349 01359395930 019349130 I parse 1 of the contact number into an object, eg: 019349130. hence: cout << cp.contactNum << endl; and the final result is 19349130 This is not the result I want. And you can see I have different length for the integer, I cannot use the leading zero solution

Strange behaviour of std::cout in Linux

一笑奈何 提交于 2019-12-20 05:26:20
问题 I am trying to print results in 2 nested for cycles using std::cout . However, the results are not printed to the console immediately, but with delay (after both for cycles or the program have been finished). I do not consider such behavior normal, under Windows printing works OK. The program does not use threads. Where could be the problem? (Ubuntu 10.10 + NetBeans 6.9). 回答1: std::cout is an stream, and it is buffered. You can flush it by several ways: std::cout.flush(); std::cout << std:

Redirecting in C++

╄→尐↘猪︶ㄣ 提交于 2019-12-20 04:52:40
问题 #include <iostream> #include <fstream> using namespace std; void foo(){ streambuf *psbuf; ofstream filestr; filestr.open ("test.txt"); psbuf = filestr.rdbuf(); cout.rdbuf(psbuf); } int main () { foo(); cout << "This is written to the file"; return 0; } Does cout write to the given file? If not, is there a way to do it without sending the variables to foo, like new ? update : I can't use a solution that uses class or uses global so plz can some give me solution that use new. Also passing the