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
<<
- 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);
- 无法处理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);