游戏编程笔记1 --- hello world

本文详细介绍C++编程的基础知识,包括如何使用iostream库进行输入输出操作,掌握cout和cin的基本用法,了解预处理指令的作用,以及如何使用setw和setprecision等方法格式化输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hello world

#include <iostream>
using namespace std;

int main()
{
    cout << "hello,world" << endl;
    cout << "hello," << "world";
    return 0;
}

基础知识

  • #include <iostream> using namespace std;
    • 让程序包括 iostream library, 这个库可以在 std 找到
  • <>的是standard库
  • "" 的是自己写的库
preprocessor directive
让 compiler 在运行程序前 load/setup 需要用到的 libraries
在 main.cpp 的第一行
  • cout
    • output to the console
  • <<
    • put to argument (右边的 put into 左边的)
  • endl
    • 断行

cout的相关方法

  • setprecision(int)
    • 详见基本数据类型
  • setw(int)
    • 需要 #include <iomanip>
    • 设置下一个input占多少个字符,如果大于下一个input的长度,则在左侧用空格补全
    • cout << setw(10) << "Prenom"; 输出结果为 (五个空格)Prenom
  • printf()
    • 需要 #include <cstdio>
    • 来自C
    • printf("%f", 3.1415926);
      • f是float的意思
      • 不会打印所有小数位,只会打印float默认显示的位数
    • printf("%10f", 3.1415926);
      • 等同于setw(),多余空格也会左对齐,也只会打印float默认显示的小数位数
    • printf("%10.6f", 3.1415926);
      • 显示6位小数点
    • 无法处理string
      • 可以处理:printf("%s\n", "hello");
      • 不可处理:string hello = "hello"; printf("%s\n", hello);

cin的相关方法

int a, b;
cin >> a >> b;
cout << a << " " << b
  • 每个input被空格分开, 如果想要录入一整行字符串, 需要使用getline()方法
    • string s; getline(cin, s);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值