cout

Assigning cout to a variable name

↘锁芯ラ 提交于 2019-11-28 21:29:22
In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile because outFile does not have an // assignment operator outFile << "whatever" << endl; I tried doing this as a Macro function as well: #define OUTPUT outFileRequested?outFile:cout OUTPUT << "whatever" << endl; But that gave me a compiler error as well. I supposed I could

C++访问sqlite3的初体验

情到浓时终转凉″ 提交于 2019-11-28 20:33:24
Sqlite确实是一个比较好的本地数据库,从接触它的时候就喜欢上了它,它可以在很多情况下简化应用。不过以前都是在Java里面使用,或者Linux C下使用的,现在有个项目(C++)可能我会用到sqlite做数据持久化,所以先热热身。 第一步:下载相关文件 首先到这里下载sqlite-source-3_6_12.zip、sqlite-3_6_12.zip、 sqlitedll-3_6_12.zip三个包,并分别解压。 第二步:生成SQLite的lib文件 cmd进入命令行后输入: LIB /DEF:SQLITE3.DEF /MACHINE:IX86 如果找不到命令LIB,则需要将Microsoft Visual Studio\VC98\Bin这个目录添加到环境变量里。这样就生成了sqlite3.lib文件,我们在后面需要用到这个库,用于链接win32程序 第三步:编写测试工程 新建项目,将sqlite3.h(在源码包里)、sqlite3.dll、sqlite3.lib设置到工程环境里,或者直接拷贝到工程目录下。 然后我们将cmd切换到sqlite3的目录下,里面有个sqlite3.exe。执行命令: > sqlite3 D:\sql.db ;生成sql.db的数据库文件 sqlite3 > create table test_tab (f1 int, f2 long);

Codeforces Round #585 (Div. 2)

拈花ヽ惹草 提交于 2019-11-28 20:22:44
https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的,,,上次学这玩意是在今年的3、4月份把,,,早忘得差不多了,,, A. Yellow Cards A题较简单,,贪心就行了,, #include <bits/stdc++.h> #define aaa cout<<233<<endl; #define endl '\n' using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; // mt19937 rnd(time(0)); const int inf = 0x3f3f3f3f;//1061109567 > 1e9 const ll linf = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-6; const double pi = 3.14159265358979; const int maxn = 1e6 + 5; const int maxm = 2e5 + 233; const int mod = 1e9 + 7; int a[maxn], n;

[c++11]多线程编程(三)——竞争条件与互斥锁

泄露秘密 提交于 2019-11-28 19:27:04
文章目录 竞争条件 使用互斥元保护共享数据 为保护共享数据精心组织代码 接口设计中也存在竞争条件 竞争条件 并发代码中最常见的错误之一就是竞争条件(race condition)。而其中最常见的就是数据竞争(data race),从整体上来看,所有线程之间共享数据的问题,都是修改数据导致的,如果所有的共享数据都是只读的,就不会发生问题。但是这是不可能的,大部分共享数据都是要被修改的。 而 c++ 中常见的 cout 就是一个共享资源,如果在多个线程同时执行cout,你会发发现很奇怪的问题: # include <iostream> # include <thread> # include <string> using namespace std ; // 普通函数 无参 void function_1 ( ) { for ( int i = 0 ; i > - 100 ; i -- ) cout << "From t1: " << i << endl ; } int main ( ) { std : : thread t1 ( function_1 ) ; for ( int i = 0 ; i < 100 ; i ++ ) cout << "From main: " << i << endl ; t1 . join ( ) ; return 0 ; }

what does cout << “\\n”[a==N]; do?

假如想象 提交于 2019-11-28 19:04:05
In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout , but it does not print a newline when the value of a is equal to N . I have no clue about what the [] option does in cout This is actually not a cout option, what is happening is that "\n" is a string literal . A string literal has the type array of n const char , the [] is simply an index into an array of characters which in this case contains: \n\0 note \0 is appended to all string literals. The == operator results in either true or false , so the index will be: 0 if false, if a does not equal N

Vector使用

前提是你 提交于 2019-11-28 18:18:14
https://www.cnblogs.com/linuxAndMcu/p/10259630.html 一、vector介绍: vector(向量): 是一种序列式容器,事实上和数组差不多,但它比数组更优越。一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界。而vector正好弥补了这个缺陷,它的特征是相当于可分配拓展的数组(动态数组),它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。 回到顶部 二、用法 1、头文件 #include <vector> //vector属于std命名域的,因此需要通过命名限定,例如using std::vector; 2、定义及初始化 vector<int> a; //定义一个int类型的向量a vector<int> a(10); //定义一个int类型的向量a,并设置初始大小为10 vector<int> a(10, 1); //定义一个int类型的向量a,并设置初始大小为10且初始值都为1 vector<int> b(a); //定义并用向量a初始化向量b vector<int> b(a.begin(), a.begin()+3); //将a向量中从第0个到第2个(共3个)作为向量b的初始值 除此之外,还可以直接使用数组来初始化向量: int n[] = {1, 2, 3, 4, 5} ; vector<int

C++ cout cin string manipulation

落花浮王杯 提交于 2019-11-28 14:20:54
I'm trying to get a line as input from the command line. My problem is that I'm not getting the whole line, but it's being tokenized by space. So if I entered something such as "I like Math a lot" instead of getting "you enterend: I like Math a lot" I get the follwoing: EDITING MODE: Enter a command i like Math a lot you entered i EDITING MODE: Enter a command you entered like EDITING MODE: Enter a command you entered Math EDITING MODE: Enter a command you entered a EDITING MODE: Enter a command you entered lot void enterEditingMode(){ editingMode = TRUE; static string CMD = "\nEDITING MODE:

How to print array of LPCTSTR in c++?

梦想的初衷 提交于 2019-11-28 13:52:26
问题 How to print array of LPCTSTR example LPCTSTR pszValueNames[] = { L"partnerID", L"partnerSubID", L"transactionID" }; for (int i = 0; i < (sizeof(pszValueNames) / sizeof(LPCWSTR)); i++) { cout << (*pszValueNames[i]) << endl; } Above give some numbers which is not real lpctstr values. When i use wchar_t* and all other basic types it spit good values. 回答1: The reason why you get the address printed is that std::cout works with std::string , which is char based. An LPCWSTR is a wide string

what is the best way to avoid negative zero in output?

不羁岁月 提交于 2019-11-28 13:24:55
As in this question is said, there is some differences between negative and positive zero in floating point numbers. I know it's because of some important reasons. what I want to know is a short code to avoid negative zero in output. for example in the following code: cout << fixed << setprecision(3); cout << (-0.0001) << endl; "-0.000" is printed. but I want "0.000". Note all other negative numbers (e.g. -0.001) should still be printed with the minus sign preceding them, so simply * -1 will not work. Try depending on your precision. cout << ((abs(ans) < 0.0005)? 0.000: ans) << endl; How about

unexpected output in cout and printf [duplicate]

泄露秘密 提交于 2019-11-28 13:05:30
问题 Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) For the code below: main() { int i = 1 ; cout << i << ++i << i++ ; } Why do I get the output as 331 instead of what was expected i.e 122 ? ( Same is the case even if I use printf instead of cout ?) 回答1: << is a function, namely something like ostream& operator<<(ostream& lhs, RhsType rhs) . cout << a; is equivalent to operator<<(cout, a); The function returns lhs, that is return lhs , - so in