C++语法基础及使用案例
1. 第一个C++程序
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
}
2. 输出不同类型(十进制、八进制、十六进制、浮点型、字符型)
#include <iostream>
using namespace std;
int main() {
cout << "十进制的数字: "
<< dec << 15 << endl;
cout << "八进制数字: " << oct << 15 << endl;
cout << "十六进制数字: " << hex << 15 << endl;
cout << "浮点型数字: "
<< 3.14159 << endl;
cout << "字符: "
<< char(188) << endl;
}
3. 控制台输入
#include <iostream>
using namespace std;
int main() {
int number;
cout << "输入一个数字: ";
cin >> number;
cout << "八进制 = 0"
<< oct << number << endl;
cout << "十六进制 = 0x"
<< hex << number << endl;
}
4. 调用其他程序
#include <cstdlib>
using namespace std;
int main() {
system("Hello");
}
5. 初始化字符串
#include <string>
#include <iostream>
using namespace std;
int main() {
string s1, s2;
string s3 = "Hello, World.";
string s4("I am");
s2 = "Today";
s1 = s3 + " " + s4;
s1 += " 8 ";
cout << s1 + s2 + "!" << endl;
}
结果:Hello, World. I am 8 Today!
6. 文件的读写
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream in("Scopy.cpp");
ofstream out("Scopy2.cpp");
string s;
while(getline(in, s))
out << s << "\n";
}
7. Vector的简单使用
vector底层本质就是一个顺序表,它是一个可变长的数组,采用连续存储的空间来存储数据,它的元素类型也可以是任意的内置类型或者自定义类型。
和数组类似,vector采用的连续存储空间来存储元素。可以采用下标对vector的元素进行访问。与数组不同的是,它的大小是可以动态改变的,而且它的大小会被容器自动处理。
vector会分配一些额外的空间以适应可能的增长,因为存储空间比实际需要的存储空间更大。
与其它动态序列容器相比,vector在访问元素的时候更加高效,在末尾添加和删除元素相对高效。对于其它不在末尾的删除和插入操作,效率更低。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
for(int i = 0; i < v.size(); i++)
cout << v[i] << ", ";
cout << endl;
for(int i = 0; i < v.size(); i++)
v[i] = v[i] * 10;
for(int i = 0; i < v.size(); i++)
cout << v[i] << ", ";
cout << endl;
}
8. 控制语句
#include <iostream>
using namespace std;
int main() {
int i;
cout << "输入一个数字:" << endl;
cin >> i;
if(i > 5)
cout << "这个数字大于5" << endl;
else if(i < 5)
cout << "这个数字小于5" << endl;
else
cout << "这个数字等于5" << endl;
cout << "在输入一个数字:" << endl;
cin >> i;
if(i < 10)
if(i > 5)
cout << "5 < i < 10" << endl;
else
cout << "i <= 5" << endl;
else // Matches "if(i < 10)"
cout << "i >= 10" << endl;
}
9. while语句
#include <iostream>
using namespace std;
int main() {
int secret = 15;
int guess = 0;
while(guess != secret) {
cout << "你猜个数: ";
cin >> guess;
if (guess > secret) {
cout << "你猜大了。" << endl;
} else if (guess < secret) {
cout << "你猜小了。" << endl;
} else {
cout << "你猜对啦!" << endl;
}
}
}
10. do-while语句
#include <iostream>
using namespace std;
int main() {
int secret = 15;
int guess;
do {
cout << "猜一个数字呀: ";
cin >> guess;
if (guess > secret) {
cout << "你猜大了。" << endl;
} else if (guess < secret) {
cout << "你猜小了。" << endl;
}
} while(guess != secret);
cout << "你猜对啦!" << endl;
}
11. for语句
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 128; i++)
if (i != 26)
cout << " value: " << i
<< " character: "
<< char(i)
<< endl;
}
12. break和continue关键字
#include <iostream>
using namespace std;
int main() {
char c;
while(true) {
cout << "主菜单:" << endl;
cout << "l: 左, r: 右, q: 退出 -> ";
cin >> c;
if(c == 'q')
break;
if(c == 'l') {
cout << "左侧菜单:" << endl;
cout << "选择 a or b: ";
cin >> c;
if(c == 'a') {
cout << "你选择的是 'a'" << endl;
continue;
}
if(c == 'b') {
cout << "y你选择的是 'b'" << endl;
continue;
}
else {
cout << "你选择的非 a or b!"
<< endl;
continue;
}
}
if(c == 'r') {
cout << "右侧菜单:" << endl;
cout << "选择 c or d: ";
cin >> c;
if(c == 'c') {
cout << "你选择的是 'c'" << endl;
continue;
}
if(c == 'd') {
cout << "你选择的是 'd'" << endl;
continue;
}
else {
cout << "你选择的非 c or d!"
<< endl;
continue;
}
}
cout << "你必须选择 l or r or q!" << endl;
}
cout << "已退出..." << endl;
}
13. switch语句
#include <iostream>
using namespace std;
int main() {
bool quit = false; // Flag for quitting
while(quit == false) {
cout << "选择 a, b, c or q to quit: ";
char response;
cin >> response;
switch(response) {
case 'a' : cout << "你选择的是 'a'" << endl;
break;
case 'b' : cout << "你选择的是 'b'" << endl;
break;
case 'c' : cout << "你选择的是 'c'" << endl;
break;
case 'q' : cout << "已退出" << endl;
quit = true;
break;
default : cout << "请你选择 a,b,c or q!"
<< endl;
}
}
}
14. 递归
#include <iostream>
using namespace std;
void removeHat(char cat) {
for(char c = 'A'; c < cat; c++)
cout << " ";
if(cat <= 'Z') {
cout << "cat " << cat << endl;
removeHat(cat + 1);
} else
cout << "VOOM!!!" << endl;
}
int main() {
removeHat('A');
}
输出:
cat A
cat B
cat C
cat D
cat E
cat F
cat G
cat H
cat I
cat J
cat K
cat L
cat M
cat N
cat O
cat P
cat Q
cat R
cat S
cat T
cat U
cat V
cat W
cat X
cat Y
cat Z
VOOM!!!