zoukankan      html  css  js  c++  java
  • cpp(第十七章)

    1.baseic_ostream<charT,traits>& write(const char_type *s,streamsize n),cout.write()第一个参数提供了要显示的字符串的地址,第二个参数

    指出要显示多少个字符

    2.cout<<hex == hex(cout) 控制符实际上是函数。

    3.cout,width()只影响将显示的下一个项目,然后字段宽将恢复为默认值(返回以前的字段宽int)。

    4.cout.fill(‘*’) 用*填充字段中未被使用的部分。

    5.cout.precision(n) 浮点类型保留n位数,而定点和科学保留n位小数

    6.

    #include<iostream>
    #include<exception>
    int mian()
    {
        std::cin.exceptions(std::ios_base::failbit);
        int input;
        try
    {
         while (std::cin>>input)
                    ;
    }catch (std::ios_base::failure &bf)
    {
        std::cout<<bf.what<<"
    ";
    }             
        return 0;
    }

    iostate exceptions() const,返回当前mask for the stream;

    void exceptions(iostate except),sets a new exception mask for the stream and clears the stream's the stream's error state flags(as if member clear() was called).the exception mask is an internal value kept by all stream objects specifying for which state flags an exception of member type failure(or some derived type) is thrown when set.

    cin.exceptions(badbit | eofbit)//setting badbit and eofbit cause exception to be thrown. exceptions()方法返回一个为字段,它包含三位,分别对应eofbit、failbit、badbit。clear()方法将当前的流状态与exceptions()返回的值进行比较。如果在返回值中某一位被设置,而当前状态中的对应为也被设置,则clear()将引发ios_base::failure异常.

    7.istream & cin.get(char&) 和 int cin.get() 提供不跳过空白的单字符输入功能。达到文件尾get(char &)返回false,get()返回eof

    8.istream & cin.get(char *,int,char)、istream & cin.get(char *,int) 和istream & cin.getline(char *,int,char)、istream & getline(char *,int),第一个参数用于放置输入字符串的内存单元地址,第二个参数比要读取的最大字符数大1(额外的一个字符用于存储结尾的空字符),第三个参数指定用作分解符的字符。区别:get()将换行符留在输入流中,getline()抽取并丢弃输入流中的换行符。get(char*,int)如果没用读取任何字符则设置failbit,getline(char *,int)如果读取了最大数目的字符,且行中还有其他字符,则设置failbit。

    9.cin.read(char*,int),读取指定数目的字节,并讲他们存储在指定的位置,read()并不会在输入后面加上空值字符,因此不能将输入转换为字符串(不是专为键盘输入设计的,与write()来完成文件的输入和输出)

    10.cin.peek(),函数返回输入中的下一个字符,但不抽取输入流中的字符

    11.cin.putback(),函数将一个字符插入到输入字符串中,被插入的字符将是下一条输入语句读取的第一个字符.

    12.fstream继承了两个缓冲区一个用于输入,一个用于输出,并能同步化这两个缓冲区的处理。

    13.tellg()和tellp,返回一个表示当前位置的streampos值(以字节为单位,从未见开始算起),fstream对象返回的值相同。

    14.char* tmpnam(char * filename),字符长度不超过L_tmpname。filename if filename was not NULL. Otherwise a pointer to an internal static buffer is returned. If no suitable filename can be generated, NULL is returned,filename如果是NULL则返回一个指向static buffers。

    #include <iostream>
    #include <cstdio>
    #include <string>
     
    int main()
    {
        std::string name1 = std::tmpnam(nullptr);
        std::cout << "temporary file name: " << name1 << '
    ';
     
        char name2[L_tmpnam];
        if (std::tmpnam(name2)) {
            std::cout << "temporary file name: " << name2 << '
    ';
        }
    }

    15.内核格式化

    #include <iostream>
    #include <sstream>
    int main()
    {
        using std::cout;
        using std::cin;
        using std::endl;
        using std::string;
        //std::istringstream in_str;
        std::ostringstream out_str;
        std::stringstream in_out_str;
        out_str<<"message!!!";
        string str= out_str.str();
        cout<<str<<endl;
        out_str<<" ahh!!!";
        cout<<str<<endl;
        cout<<out_str.str()<<endl;
        auto cout_p= cout.rdbuf(out_str.rdbuf());
        cout<<" guess?!";
        cout.rdbuf(cout_p);
        cout<<"auto: "<<out_str.str()<<endl;
        std::istringstream in_str(str);
        string word;
        in_str>>word;
        cout<<"word:"<<word<<endl;
        in_out_str<<str;
        cout<<in_out_str.str()<<endl;
        in_out_str.get();
        if (in_out_str.putback('W'))
        {
            cout<<in_out_str.rdbuf()<<endl;
        }
        else
        {
            cout<<"error putback
    ";
        }
        std::istringstream ins_str(str);
        ins_str.get();
        if (ins_str.putback('W'))
        {
            cout<<ins_str.rdbuf()<<endl;
        }
        else
        {
            cout<<"error putback
    ";
        }
        ins_str.clear();
        if (ins_str.putback('m'))
        {
  • 相关阅读:
    字符串转数字的hash函数-布隆过滤器
    javascript实现字符查询之kmp算法
    毫秒查询9位数qq号码是否存在-BitMap算法应用
    bitMap算法将字符串映射成数字,同时可以将数字映射成字符串-javascript
    js数字格式化为千分位
    浅谈BST(二叉查找树)
    CSP2019 游记
    2019.10.20模拟赛总结
    P2827 蚯蚓
    原生js解决简单轮播图的切换
  • 原文地址:https://www.cnblogs.com/Call-C/p/5922401.html
Copyright © 2011-2022 走看看