#include
using namespace std;
int main() {
cout << "Hello World" << endl;
}
#include
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;
}
#include
using namespace std;
int main() {
int number;
cout << "输入一个数字: ";
cin >> number;
cout << "八进制 = 0"
<< oct << number << endl;
cout << "十六进制 = 0x"
<< hex << number << endl;
}
#include
using namespace std;
int main() {
system("Hello");
}
#include
#include
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!
#include
#include
using namespace std;
int main() {
ifstream in("Scopy.cpp");
ofstream out("Scopy2.cpp");
string s;
while(getline(in, s))
out << s << "\n";
}
vector底层本质就是一个顺序表,它是一个可变长的数组,采用连续存储的空间来存储数据,它的元素类型也可以是任意的内置类型或者自定义类型。
和数组类似,vector采用的连续存储空间来存储元素。可以采用下标对vector的元素进行访问。与数组不同的是,它的大小是可以动态改变的,而且它的大小会被容器自动处理。
vector会分配一些额外的空间以适应可能的增长,因为存储空间比实际需要的存储空间更大。
与其它动态序列容器相比,vector在访问元素的时候更加高效,在末尾添加和删除元素相对高效。对于其它不在末尾的删除和插入操作,效率更低。
#include
#include
using namespace std;
int main() {
vector 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;
}
#include
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;
}
#include
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;
}
}
}
#include
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;
}
#include
using namespace std;
int main() {
for(int i = 0; i < 128; i++)
if (i != 26)
cout << " value: " << i
<< " character: "
<< char(i)
<< endl;
}
#include
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;
}
#include
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;
}
}
}
#include
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!!!