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秒。

 

8.添加背景音乐

#include <mmsystem.h>        //多媒体接口头文件
#pragma comment(lib,"winmm.lib")


//背景音乐	
mciSendString("open 亡灵序曲.mp3 alias bk", 0, 0, 0);
mciSendString("Play bk repeat", 0, 0, 0);

 

9. 使用unicode编码,把汉字操作为字符

#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
	wchar_t c = L'爱';
	setlocale(LC_ALL, "Chs");	
	wcout << c << endl;
	system("pause");
	return 0;
}

  

 10.

cin >> M >> N;

vector<vector<int>> m(M,vector<int>(N));

初始化二维数组,可像一般二位数组一样通过 ‘=’ 进行复值。