【ACM—蓝桥杯】蓝桥杯中C++的一些基础用法

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));

猜你喜欢

转载自www.cnblogs.com/yznnnn/p/10520007.html