输入输出流

C++输入输出

 

#include <iostream>
#include <string>

using namespace std;

int main() {
    string url;
    cin >> url;
    cout << "cout: " << url << endl;
    cerr << "cerr: " << url << endl;
    clog << "clog: " << url << endl;
    return 0;
}

 

运算符重载

格式:

friend istream& operator>>(istream& is, className& a);

cin >> a; <==> operator(cin, a);

// 参数输入流对象的引用和输入流有什么联系?

 

#include <iostream>

using namespace std;

class A {
   private:
    float x, y;

   public:
    A(float a = 0, float b = 0) {
        this->x = a;
        this->y = b;
    }
    void set(int a, int b) {
        this->x = a;
        this->y = b;
    }
    void show() { cout << this->x << "\t" << this->y << endl; }

    friend istream& operator>>(istream& is, A& a);
    friend ostream& operator<<(ostream& os, const A& a);
};

istream& operator>>(istream& is, A& a) {
    is >> a.x >> a.y;
    return is;
}

ostream& operator<<(ostream& os, const A& a) {
    os << a.x << "\t" << a.y << endl;
    return os;
}

int main() {
    A a;
    cin >> a;
    cout << a;
    return 0;
}

 

输入输出流的重定向

方法一:

#include <stdio.h>

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name, url;
    // 将标准输入重定向到io3.txt中
    freopen("io3.txt", "r", stdin);
    cin >> name >> url;
    // 将标准输出重定向到out3.txt中
    freopen("out3.txt", "w", stdout);
    cout << name << "\n" << url;
    return 0;
}

方法二:

#include <stdio.h>

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name, url;
    cin >> name >> url;
    cout << name << "\n" << url;
    return 0;
}

 在命令行中也可以对文件进行重定向操作。

 

输出格式

头文件:iomanip.h

 

(图片来源:http://c.biancheng.net/view/275.html)

#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    int a;
    cout << "Please input a:";
    cin >> a;
    cout << "dex:" << dec << a << endl;
    cout << "hex:" << hex << a << endl;
    cout << "oct:" << setbase(8) << a << endl;
    char pt[10] = "China";
    cout << setw(10) << pt << endl;
    cout << setfill('*') << setw(10) << pt << endl;
    double pi = 22.0 / 7.0;
    cout << setiosflags(ios::scientific) << setprecision(8);
    cout << "pi = " << pi << endl;
    cout << "pi = " << setprecision(4) << pi << endl;
    cout << "pi = " << setiosflags(ios::fixed) << pi << endl;

    return 0;
}

输出结果:

 

 注:

cout << hex << a << endl;
// 等同于
cout.setf(hex);
cout << a << endl;

 

常用成员函数

cout.put():用于向缓冲区添加单个字符

格式:

ostream&put(char c);

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    freopen("io3.txt", "r", stdin);
    cin >> str;
    freopen("out3.txt", "w", stdout);
    for (int i = str.length() - 1; i >= 0; --i) {
        cout.put(str[i]);
    }
    return 0;
}

 

 cout.write():用于向输出缓冲区中添加指定字符串

格式:

ostream&write(const char * s,streamsize n);

因为返回值是一个ostream对象,所以可以像下面的方式使用write方法

cout.write(c1, 1).write(c2, 2).write(c3, 3);

 

#include <iostream>

using namespace std;

int main() {
    char str[100] = "www.xxxx.xxx.xx";
    cout.write(str, 5).write(str, 3);
    return 0;
}

注:连写的时候每次都是从第一个字符开始计数。

 

posted @ 2021-05-18 15:53  Veritas_des_Liberty  阅读(62)  评论(0编辑  收藏  举报