首页 > 编程语言 > 详细

c++ 笔记

时间:2019-04-04 21:58:09      阅读:125      评论:0      收藏:0      [点我收藏+]

1. cerr   可输出出错信息,用法和cout一样,在vc6.0中和cout输出在同一个界面,gcc中输出到出错信息窗口

2.setiosflags(ios::fixed) 设置定点格式         setprecision() 设置精度

//实现输出时上下行小数点对齐
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
    double a = 3.14;
    double b = 12.56;
    cout << setiosflags(ios::fixed) << setprecision(2);//设置定点格式和精度
    cout << setw(10) << a << endl << setw(10) << b << endl;
    system("pause");
    return 0;
}

技术分享图片

 

3.可输入空格    getline();

例:string str;

getline(cin,str);

 

4.文件的输入输出

头文件 #include <fstream>

ofstream fout("文件名 ");

ifstream fin("文件名");

if(!fout)

{

 cout << "文件打开不成功“;

return 0;

}

f(!fin)

{

 cout << "文件打开不成功“;

return 0;

}

fout.close();

fin.close();

实例:

技术分享图片
//实现输出时上下行小数点对齐
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
//fun1函数数据输入到文件fout1.txt 和fout2.txt中
void fun1()
{
    int a[3];
    ofstream fout1("fout1.txt"), fout2("fout2.txt");
    if (!fout1)
    {
        cout << "fout1.txt 打开不成功。" << endl;
    }
    if (!fout2)
    {
        cout << "fout2.txt 打开不成功。" << endl;
    }

    for (int i = 0; i < 3; i++) //写入fout1.txt
    {
        cin >> a[i];
        fout1 << a[i] << " ";
    }

    for (int i = 0; i < 3; i++) //写入fout2.txt
    {
        cin >> a[i];
        fout2 << a[i] << " ";
    }

    fout1.close();
    fout2.close();
}

//fun2函数将fout1.txt文件中的内容输入到文件fout2.txt中
void fun2()
{
    ifstream fin("fout1.txt");
    ofstream fout("fout2.txt",ios::app); //ios::app使得文件指针移动到最后,文件指针默认在开头
    if (!fin)
    {
        cout << "文件fout1.txt 打开不成功。" << endl;
    }
    if (!fout)
    {
        cout << "文件fout2.txt 打开不成功。" << endl;
    }
    int a;
    for (int i = 0; i < 3; i++)
    {
        fin >> a;
        fout << a << " ";
    }

    fin.close();
    fout.close();
}

//fun3函数将fout1.txt文件中数据由小到大排序后放入fout2.txt中
void fun3()
{
    ifstream fin("fout1.txt");
    ofstream fout("fout2.txt");
    if (!fin)
    {
        cout << "文件fout1.txt 打开不成功。" << endl;
    }
    if (!fout)
    {
        cout << "文件fout2.txt 打开不成功。" << endl;
    }

    int a[3];
    int temp;
    for (int i = 0; i < 3; i++)
    {
        fin >> a[i];
    }

    for (int i = 0; i < 2; i++)     //冒泡排序
    {
        for (int j = 0; j < 2 - i; j++)
        {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < 3; i++)
    {
        fout << a[i] << " ";
    }

    fin.close();
    fout.close();
}

int main()
{
    fun1();
    //fun2();
    fun3();
    system("pause");
    return 0;
}
文件操作

 注意 同一个文件流不能冲撞,不能紧接着输入,又紧接着输出。可放在两个函数中。

 5.system("pause") ;         --------------------暂停

   system("cls")  ;               --------------------清屏

两个搭配起来用有换屏的效果。

6.改变光标位置

#include <Windows.h> //头文件

    // 标准输出句柄,获取输出的的那个黑边框
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    // 光标位置
    COORD  cursorPos;
    cursorPos.X = 10;        //设置坐标(10,10)
    cursorPos.Y = 10;
    SetConsoleCursorPosition(hStdout, cursorPos); //启动光标定位
    
    CloseHandle(hStdout);//释放获取的黑边框
    

7.Windows.h 中的Sleep()函数可延时

例 Sleep(7000)可延时7秒。

 

c++ 笔记

原文:https://www.cnblogs.com/jianghuxiao/p/10636602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!