C++语法基础及入门使用案例

本文详细介绍了C++编程的基础,包括输出不同类型的数据、控制台输入、文件读写、Vector容器使用、控制语句(如if、else、while、do-while、for)、break和continue关键字、switch语句以及递归的概念和应用实例。
摘要由CSDN通过智能技术生成

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!!!
  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的布欧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值