cout

Cout and Cin in Linux - can't see the console

北慕城南 提交于 2020-01-03 18:51:27
问题 I just moved from Windows to Linux and I'm try to create a simple application that opens a console, displays a message and wait for key press to close. I have created it on Windows and it works, then I just moved the files to Linux. Didn't make any change, just compiled it with g++ and I get no errors. The problem is that on Linux (Ubuntu 12.04) I can't see the console and some message asking me to press any key before closing. My code is as simple as this: #include <iostream> #include

How can I redirect a std::ofstream to std::cout?

假如想象 提交于 2020-01-03 14:18:11
问题 I'm working with some code that uses a global debug logger that is of type std::ofstream* . I would like to redirect this to std::cout since I'm using the code in realtime, as opposed to a batch method for which it was designed. Is it possible to redirect the global std::ofstream* pointer it uses to std::cout ? I know std::ofstream inherits from std::ios , which allows one to change the stream buffer using the rdbuf() method, but unfortunately it appears std::ofstream redefines the rdbuf()

Is there a simple way to get the number of characters printed in C++?

我的未来我决定 提交于 2020-01-03 08:37:12
问题 printf(...) returns the number of characters output to the console, which I find very helpful in designing certain programs. So, I was wondering if there is a similar feature in C++, since the cout<< is an operator without a return type (at least from what I understand of it). 回答1: You can associate your own streambuf to cout to count the characters. This is the class that wraps it all: class CCountChars { public: CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m

Is there a simple way to get the number of characters printed in C++?

我的未来我决定 提交于 2020-01-03 08:37:08
问题 printf(...) returns the number of characters output to the console, which I find very helpful in designing certain programs. So, I was wondering if there is a similar feature in C++, since the cout<< is an operator without a return type (at least from what I understand of it). 回答1: You can associate your own streambuf to cout to count the characters. This is the class that wraps it all: class CCountChars { public: CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m

Type of ternary expression

泄露秘密 提交于 2020-01-03 06:44:06
问题 Can anyone explain the output of the following program: #include <iostream> using namespace std; int main() { int test = 0; cout << "First character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } Output: First character 1 Second character 49 But both the printf statements should print the same line. 回答1: The type of the expression '1' is char . The type of the expression (test ? 3 : '1') is at least int (or an unsigned version thereof; portably it is

print address of array of char

假如想象 提交于 2020-01-02 07:50:49
问题 int *i = new int(1); cout << i << endl; Will print the address of the integer. char *c="cstring"; cout << c << endl; cout << &(*c) << endl; Will both print "cstring". I guess this behavior can simply be explained with the implementation of ostream& operator<< (ostream& out, const char* s ); in the IOstream Library. But what to do if you actually want to print the address of the data c refers to? 回答1: cout << reinterpret_cast<void*>(c) << endl; or just cout << (void*)c << endl; 回答2: Try

C++ print value of a pointer

十年热恋 提交于 2020-01-02 06:21:51
问题 I have an array of double pointers, but every time I try do print one of the values the address gets printed. How do I print the actual value? cout << arr[i] ? cout << &arr[i] ? they both print the address Does anyone know? 回答1: If it's really an array of (initialized) double pointers, i.e.: double *arr[] = ... // Initialize individual values all you need is: cout << *arr[i]; 回答2: cout << *(arr[i]) will print the value. 回答3: cout << *(arr[i]); 回答4: If "arr" is declared as double* arr[..];

C++11正则表达式 ECMAScript文法

不羁岁月 提交于 2020-01-01 12:48:05
突然想写个爬虫,然后发现,如果有正则表达式,会方便些。 C++11提供了Regex类.可以用来完成: 1.Match: 将整个输入拿来比对(匹配)某个正则表达式。 2.Search:查找“与正则表达式吻合”的子序列。 3.Tokenize:正则表达式作为分割器,得到分割器之前的字符串。 4.Replace:将与正则表达式吻合之的子序列替换掉 主要函数有: regex_match(),regex_search(),regex_replace(); 主要对象:sregex_iterator,sregex_token_iterator,regex,smatch 例子: [_[:alpha:]][_[:alnum:]]* 表示,以_或字母开头,后面接着任意个_或字母的组合 [123]?[0-9]\.1?[0-9]\.20[0-9]{2} 表示german format,如 24.12.2010 C++11默认使用 ECMAScript 文法,告诉你怎么构造正则表达式 表示式 意义 . newline以外的任何字符 [...] ...字符中的任何一个 [^...] ...字符之外的任何一个 [ [:charclass:]] 指定字符串类charclass中的一个(见下表) \n,\t,\f,\r,\v 一个newline,tabulator,form feed,carriage return

C++ unicode characters printing

血红的双手。 提交于 2020-01-01 09:10:09
问题 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.

iostream thread safety, must cout and cerr be locked separately?

流过昼夜 提交于 2020-01-01 04:46:05
问题 I understand that to avoid output intermixing access to cout and cerr by multiple threads must be synchronized. In a program that uses both cout and cerr, is it sufficient to lock them separately? or is it still unsafe to write to cout and cerr simultaneously? Edit clarification: I understand that cout and cerr are "Thread Safe" in C++11. My question is whether or not a write to cout and a write to cerr by different threads simultaneously can interfere with each other (resulting in