C++
语法
基础
方法1
调用库:
调用函数:
1
| std::cout << "Hello!\n";
|
方法2
命名空间,省略 std。
1 2
| #include <iostream> using namespace std;
|
调用函数:
组合方式
组合方式1
1
| std::cout << "Hello World!\n";
|
组合方式2
1
| std::cout << "Hello" << " World!\n";
|
组合方式3
1 2
| std::cout << "Hello"; std::cout << " World!\n";
|
特殊符号
回车
回车表示将光标移动到行的开头。这应该和键盘上的回车有所区分。
换行
换行表示将光标移动到下一行的开头。
或者:
变量调用
1 2 3 4
| int num = 100; std::cout << "数字是:" << num << std::endl; std::string st = "你好,世界!"; std::cout << st << std::endl;
|
演示
演示效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <iostream>
int main() { std::cout << "Print\n"; std::cout << "|_" << "语法\n"; std::cout << "| |_" << "基础\n"; std::cout << "| | |_" << "Hello!\n"; std::cout << "| |" << "\n"; std::cout << "| |_" << "组合方式\n"; std::cout << "| | |_" << "组合方式1\n"; std::cout << "| | | |_" << "Hello World!\n"; std::cout << "| | |" << "\n"; std::cout << "| | |_" << "组合方式2\n"; std::cout << "| | | |_" << "Hello" << " World!\n"; std::cout << "| | |" << "\n"; std::cout << "| | |_" << "组合方式3\n"; std::cout << "| | |_" << "Hello"; std::cout << " World!\n"; std::cout << "| |" << "\n"; std::cout << "| |_" << "特殊符号\n"; std::cout << "| | |_" << "回车\n"; std::cout << "| | |_" << "\r"; std::cout << "*\n"; std::cout << "| | |_" << "换行\n"; std::cout << "| | |_" << "\n"; std::cout << "*\n"; std::cout << "| | |_" << "或者\n"; std::cout << "| | |_" << std::endl; std::cout << "*\n"; std::cout << "| |" << "\n"; std::cout << "| |_" << "变量调用\n"; int num = 100; std::cout << "| | |_" << "数字是:" << num << std::endl; std::string st = "你好,世界!"; std::cout << "| | |_" << st << std::endl; }
|
说明:换行和回车有明显区别。
1 2 3 4 5 6 7 8 9
| #include <iostream> using namespace std;
int main() { string st = "你好,世界!"; cout << st << endl; }
|