1. 程式人生 > >【ACM—藍橋杯】藍橋杯中C++的一些基礎用法

【ACM—藍橋杯】藍橋杯中C++的一些基礎用法

can value amp size NPU include 格式化 cstring queue

C++基礎用法

1、printf()打印函數

printf("Hello,world!\n");//普通打印字符串
printf("%d\n",int_value);//打印int型格式化字符串
printf("%f\n",float_value);//打印float型格式化字符串
printf("%.2f",a); //保留小數點後兩位
printf("%7.2f",a);//整數位保留個數.小數位保留個數


2、scanf()輸入函數

scanf("%d",&int_value);//輸入int型值
scanf("%f",&float_value);//輸入float型值
//在輸入實數時,最好使用 double 類型, %lf


//這裏有一種經典寫法,不會在oj中報錯
while(scanf("%d%d",&A,&B)!=EOF)
{
    ......
    ......
}

//16進制輸入法
int a
scanf("%x",&a);

//getline用法
getline(cin,input_str); 用來讀取一行字符串(包括空格)

3、for循環

// 打印 n 次 "Hello,world!\n"
for(i=0;i<n;i++)
{
    printf("Hello,world!\n");
}

4、while循環

// 打印 無數次 次 "Hello,world!\n"
while(true)
{
    printf("Hello,world!\n");
}

5、if判斷

// 如果條件正確,就打印 "Hello,world!\n"
if(true)
{
    printf("Hello,world!\n");
}

6、struct結構體

struct Date{
    int year;
    int month;
    int day;
};

7、queue隊列vector

8、格式轉換

//int 轉 str
#include <sstream>
#include <string>
string int2str(int input)
{
    string output;
    stringstream ss;
    ss << input;
    output=ss.str();
    return output;
}

9、數組整體賦值

//將src數組的值賦給dest數組
#include <cstring>
memcpy(dest,src,sizeof(src));
//將dest數組全部賦值為0
#include <cstring>
memset( dest, 0, sizeof(dest));

【ACM—藍橋杯】藍橋杯中C++的一些基礎用法