ET-Campus算法竞赛_0_基本语法

文章介绍了如何在C++中使用`ios::sync_with_stdio(false)`、`cin.tie(0)`和`cout.tie(0)`进行IO优化,以提高程序效率,特别是在处理大量输入时。同时讨论了`flush`、`ends`和`endl`的区别,以及避免使用`endl`以减少额外操作。
摘要由CSDN通过智能技术生成
  1. ios::sync_with_stdio(false);

    这段语句可以在IO之前将stdio解除绑定,取消iostream的输入输出缓存,可以节省许多时间,使效率与 scanf 与 printf 相差无几。
    这样做了之后要注意不要同时混用 cout/cin 和 printf/scanf 之类

  2. cin.tie(0),cout.tie(0);

    tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。
    在默认的情况 cin 下绑定的是 cout ,每次执行 << 操作符的时候都要调用 flush ,这样会增加IO负担。可以通过 tie(0表示NULL)来解除 cin 与 cout 的绑定,进一步加快执行效率。

    应用:

    #include <iostream>
    #include <fstream>
    ///SubMain//
    int main()
    {
    	std::ostream *prevstr;
    	std::ofstream ofs;
    	ofs.open("test.txt");
     
    	std::cout << "tie example:\n";	// 直接输出到屏幕
     
    	*std::cin.tie() << "This is inserted into cout\n";	// 空参数调用返回默认的output stream,也就是cout
    	prevstr = std::cin.tie(&ofs);						// cin绑定ofs,返回原来的output stream
    	*std::cin.tie() << "This is inserted into the file\n";	// ofs,输出到文件
    	std::cin.tie(prevstr);									// 恢复
     
    	ofs.close();
    	system("pause");
    	return 0;
    }
    ///End Sub//
    
    
    输出:
    
    tie example:
    This is inserted into cout
    请按任意键继续. . .
    
    
    同时当前目录下的test.txt输出:
    
    This is inserted into the file
  3. ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    该操作取消同步流,该条件下 cin 速度一般会比 scanf 快。

  4. int main()
    {
        using namespace std;
        cout << "a" ;
        cout << "b" <<ends;
        cout << "c" <<endl;
        cout << "e" << flush;
        cout << "f" << flush;
        cout << "g" ;
        cout << "h" <<ends;
        cout << "i" << flush;
        cout << "j" <<endl;
        return 0;
    }
    
    结果
    ab c
    efgh ij

    ends函数:终止字符串,即在末尾加入“\n”。
    flush函数:刷新缓冲区。
    endl函数:终止一行并刷新缓冲区。

  5. 不要使用 cout << endl; 该操作包含 换行 + 清空缓存区 ,等于 cout << "\n" << flush;

  6. 简化 long long 的方法
    #include<bits/stdc++.h>
    using namespace std;
    
    #define int long long    // 1_利用define将int转化为long long    
    
    typedef long long ll;    // 2_利用typedef函数为long long类型赋予新的名称ll
    
    using ll = long long;    // 3_c++11以上的写法
     
    signed main()    //因为main函数需要返回一个int值,而int已被定义为long long,          
                      //故而利用int = signed int = signed(有符号整型)声明int,
                      //与#define int long long配套
    {
        
    }
    
  7.  常用程序题格式
    #inclued <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    
    //1.确定数组大小,不要用define
    const int N = 1e5 + 9;
    
    //2.开数组,全局数组自动初始化为0;
    int A[N], prefix[N];
    
    int main()
    {
        //3.取消同步流
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        
    
        int n;cin << n;
    
        //4.单独输入(仅调用i单元,可加快运行速度)
        for(int i = 1; i <= n; ++ i)cin >> A[i];
        for(int i = 1; i <= n; ++ i)prefix[i] = prefix[i - 1] + A[i];
        
        return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值