cout

Check if ostream object is cout or ofstream, c++

丶灬走出姿态 提交于 2019-11-26 21:37:30
问题 Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that I want to overload the << operator to do two different things depending on what type of stream it is used with. Is it possible to just overload the << operator twice each time with a different type of

C++ not showing cout in Xcode console but runs perfectly in Terminal

随声附和 提交于 2019-11-26 21:30:33
问题 Basically i am running a simple program in Xcode Version 8.3 (8E162) #include <iostream> using namespace std; int main() { int a; cout << "What is your age: "; cin >> a; cout << "My age is " << a << endl; return 0; } I have seen different questions about cout need to be flushed and all std::cout won't print and Xcode debugger not showing C++ cout output. The Xcode debugger does not print cout until i put \n or endl . But, it works perfectly fine on terminal. What if i had to use What is your

Is std::cout buffered?

狂风中的少年 提交于 2019-11-26 21:14:53
问题 Just reading an old but interesting article by "Scott Meyers" http://aristeia.com/Papers/C++ReportColumns/novdec95.pdf Basically it is about preferring to use '\n' over std::endl (which I agree with and have used the same augment for years). BUT the last section indicates this was not included in his book because the whole thing was rendered moot because of two points: std::cout was not buffered. The state of ios::unitbuf on std::cout is not explicitly defined (thus implementation dependent).

temp

核能气质少年 提交于 2019-11-26 20:10:16
QueryResult.h、TextQuery.h、TextQuery.cpp 和 make_plural.h 同 练习 12.27 。 Query.h #ifndef TEST_QUERY_H #define TEST_QUERY_H #include "TextQuery.h" #include <string> #include <set> #include <iostream> #include <fstream> #include <sstream> #include <memory> // abstract class acts as a base class for concrete query types; all members are private class Query_base { friend class Query; protected: typedef TextQuery::line_no line_no; // used in the eval functions virtual ~Query_base() {} private: // eval returns the QueryResult that matches this Query virtual QueryResult eval(const TextQuery &) const = 0;

vector insert()

随声附和 提交于 2019-11-26 19:48:18
1. 语法: vector_name.insert (position, val) - position:插入的位置; - val:插入的值; Return value:返回一个迭代器Iterator,指向新插入的元素。 // program below illustrates the // vector::insert() function #include <bits/stdc++.h> using namespace std; int main() { // initialising the vector vector<int> vec = { 10, 20, 30, 40 }; // inserts 3 at front auto it = vec.insert(vec.begin(), 3); // inserts 2 at front vec.insert(it, 2); cout << "The vector elements are: "; for (auto it = vec.begin(); it != vec.end(); ++it) cout << *it << " "; return 0; } Output: The vector elements are: 2 3 10 20 30 40 2. 语法: vector_name.insert

三十分钟学习STL【转载】

≯℡__Kan透↙ 提交于 2019-11-26 18:36:52
原文: http://net.pku.edu.cn/~yhf/UsingSTL.htm 太长30分钟是看不完的,不过花一个小时把他看完能对STL有个大致的了解,g++下需要加iterator头文件 以下为原文 这是本小人书。原名是《using stl》,不知道是谁写的。不过我倒觉得很有趣,所以化了两个晚上把它翻译出来。我没有对翻译出来的内容校验过。如果你没法在三十分钟内觉得有所收获,那么赶紧扔了它。文中我省略了很多东西。心疼那,浪费我两个晚上。 译者:kary contact:karymay@163.net STL概述 STL的一个重要特点是数据结构和算法的分离。尽管这是个简单的概念,但这种分离确实使得STL变得非常通用。例如,由于STL的sort()函数是完全通用的,你可以用它来操作几乎任何数据集合,包括链表,容器和数组。 要点 STL算法作为模板函数提供。为了和其他组件相区别,在本书中STL算法以后接一对圆括弧的方式表示,例如sort()。 STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。你在STL中找不到任何明显的类继承关系。这好像是一种倒退,但这正好是使得STL的组件具有广泛通用性的底层特征。另外,由于STL是基于模板,内联函数的使用使得生成的代码短小高效。 提示

C++ 获取Windows服务列表

柔情痞子 提交于 2019-11-26 18:31:34
#include <windows.h> #include <iostream> #define MAX_SERVICE_SIZE 1024 * 64 #define MAX_QUERY_SIZE 1024 * 8 int main( void ) { do { SC_HANDLE SCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (SCMan == NULL) { std::cout << " OpenSCManager failed. " << std::endl; break ; } LPENUM_SERVICE_STATUS service_status; DWORD cbBytesNeeded = NULL; DWORD ServicesReturned = NULL; DWORD ResumeHandle = NULL; service_status = (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, MAX_SERVICE_SIZE); BOOL ESS = EnumServicesStatus(SCMan, // 句柄 SERVICE_WIN32, // 服务类型 SERVICE_STATE_ALL, // 服务的状态 (LPENUM_SERVICE_STATUS

Poco Exception例子

社会主义新天地 提交于 2019-11-26 18:31:07
Poco Exception提供的宏挺方便的。。 #include " Poco/Exception.h " #include <iostream> POCO_DECLARE_EXCEPTION( , MyException, Poco::Exception ) POCO_DECLARE_EXCEPTION( , MyFatalException, Poco::Exception ) POCO_IMPLEMENT_EXCEPTION( MyException, Poco::Exception, " Something really bad happened... " ) POCO_IMPLEMENT_EXCEPTION( MyFatalException, Poco::Exception, " Something really really bad happened... " ) void reallyBad () { throw MyException(); } void reallyReallyBad () { throw MyFatalException(); } int main( void ) { try { reallyBad(); } catch ( MyException& ex ) { std::cout << ex.displayText() << std:

Educational Codeforces Round 70 题解

风流意气都作罢 提交于 2019-11-26 17:36:56
比赛链接 : https://codeforc.es/contest/1202   A. You Are Given Two Binary Strings... 题意 :给出两个二进制数 \(f(x)\) 和 \(f(y)\) ,定义一个二进制数 \(s_k=f(x)+2^k\cdot f(y)\) ,询问 \(k\) 取何值时 \(s_k\) 的反向字符串(将二进制数看作01串)字典序最小。 分析 :首先,我们需要知道对于一个二进制数乘2,相当于将该数整体左移一位,因此我们只需要找到 \(f(y)\) 最右侧的1的位置 \(pos\) ,令它去和 \(f(x)\) 在 \(pos\) 左侧最近的 \(y\) 匹配,这样得到的反向字符串字典序必然最小。 AC代码 : #include <bits/stdc++.h> #define SIZE 200007 #define rep(i, a, b) for(int i = a; i <= b; ++i) using namespace std; typedef long long ll; void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } ll n, m, t; string s1, s2; int main() { io();

Float formatting in C++

大憨熊 提交于 2019-11-26 16:50:13
问题 How do you format a float in C++ to output to two decimal places rounded up? I'm having no luck with setw and setprecision as my compiler just tells me they are not defined . cout << "Total : " << setw(2) << total << endl; total outputs: Total : 12.3961 I'd like it to be: 12.40 or 12.39 if it's too much work to round up. 回答1: You need to include <iomanip> and provide namespace scope to setw and setprecision #include <iomanip> std::setw(2) std::setprecision(5) try: cout.precision(5); cout <<