C++入门第一课

  1. c++优点
  • 硬件执行速度快
  • 软件规模大、复杂度高的时候功能强大
  • 深入的图像处理需/深度学习需要c++,虽然Python也行
  1. 一般组成部分
  • 预编译指令
  • main函数

    《C++入门第一课》 结构组成部分

如图, “#” 代表预编译指令, “include”代表需要包含库,”<>”代表先在标准库里寻找当前库,如果找不到就在当前文件夹找
如果换成引号,代表先在当前文件夹寻找,找不到再去标准库里寻找

《C++入门第一课》 结构组成部分

看最简单的一段代码

 #include <iostream>

  int main() 
 {
      std::cout << "Hello world!";
      return 0;
  }
  • main函数必须返回0
  • cout表示在控制台打印输出
  • std:: 表示后面的cout是在这个命名空间里的变量。 为了简便,命名空间也可以写在最上面,这样就不用每次在变量名前写
#include <iostream>

 using namespace std;

 int main() 
{
     cout << "Hello world!";
     return 0;
 }
  • 向控制台打印输出
 int a = 3
 std::cout << "hello word";   // 输出字符串需要加引号
 std:cout<< 3; // 输出数字不加引号
 std::cout << a;  // 输出变量
 std::cout << "我想输出:" << a << "可以吗?" << std::endl; // 连续输出,endl表示换行
  1. 注释
    分两类:
  • 单行注释 //
  • 多行注释 /* */
  1. 代码风格
  1. 编译
  • IDE内置编译器,eg: MAC的XCode
  • g++ 《C++入门第一课》 c++编译与执行

  1. 打印一下各个数据类型的默认长度,做个笔记

    《C++入门第一课》 数据类型默认长度(字节)

  2. 定义常量,即不可改变的值, 用const

  • const int a = 1;
  1. 可以自定义有限值的枚举类型
    //定义 MONTHS变量为枚举数据类型,有后面12个值
    enum MONTHS {Jan, Feb, Mar, Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
    
    //定义变量 bestMonth为MONTHS这个枚举类型
    MONTHS bestMonth;
    
    //此变量只能赋值为枚举类型的值其中的一个
    bestMonth = Jan;

如果你赋的枚举类型的值不是其中之一,那就会报错

《C++入门第一课》 报错

  1. 格式化

      • -n/std::endls 是换行
      • -t 代表tab键

        《C++入门第一课》 格式化

    1. 可以引入一个标准库<iomanip>, 用setw(int n)来输出空格宽度,n代表字符宽度

      《C++入门第一课》 setw

  2. 文件读写

  • 文件读写需要引入 <stream>标准库
  • 定义一个文件流对象,将对象与文件关联起来,每个文件流类都定义了一个名为open的函数,完成系统相关的操作,来定位给定的文件,并视情况打开为读或写模式
  • 首先定义输入流ifstream(读)或者输出流ofstream(写),如果提供文件名,则open会自动被调用
  • 样例代码:
    # include <iostream>
    # include <fstream>
    
    using namespace std;
    
    int main() {
    
      string line;
      ofstream myfileI ("another.txt");
    
      if(myfileI.is_open())
      {
        myfileI << "向末尾添加一行\n";
        myfileI << "向末尾添再加一行\n";
        myfileI.close();
      }
      else cout << "无法打开写入权限的文件test.txt";
    
      ifstream myfileO ("another.txt");
      if(myfileO.is_open())
      {
          while (getline(myfileO, line)) {
            cout << line << endl;
          }
          myfileO.close();
      }
      else cout << "无法打开读取权限的test.txt";
      return 0;
    
    }
    
  • 输出结果
    向末尾添加一行
    向末尾添再加一行
    Program ended with exit code: 0
    
  1. 头文件
  • 一般来讲,c++程序可以包含自己写库,一般后缀为.hpp,成为头文件
  • 头文件包含如何去做一个任务的计划
  • 主程序包含将要去做的的信息
  • 举例

    《C++入门第一课》 原来
    《C++入门第一课》 更改

《C++入门第一课》 更改

  1. 控制台输入用std::cin
  • 先定义好你要在控制台输入的变量的类型
  • 样例代码
 using namespace std;
 int main(){
    int year = 0;
    int age = 0;
    string name = " ";
    //print a message to the user
    std::cout<<"What year is your favorite? ";
    
    //get the user response and assign it to the variable year
    std::cin >> year;
    
    //output response to user
    std::cout<<"How interesting, your favorite year is "<<year<<"!\n";
    
    //print a message to the user
    std::cout<<"At what age did you learn to ride a bike? ";
    
    //get the user response and assign it to the variable age
    std::cin >> age;
    
    //output response to user
    std::cout<<"How interesting you learned to ride at "<<age<<"!\n";
    
    std::cout<<"What is your name? ";
    std::cin>>name;
    std::cout<<"Hello "<<name<<" !\n";
    return 0;
}

    原文作者:筑梦师刘渊
    原文地址: https://www.jianshu.com/p/b39df01f90fa
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞