C++基础知识及题目

100 阅读5分钟

vector基本知识

  1. 头文件:#include <vector>
  2. 声明方式:
    • vector<int> myVector; // 创建一个空vector, 元素是int类型的
    • vector<int> myVector = {1, 2, 3, 4, 5}; // 创建一个包含整数元素的容器并初始化元素
    • vector<int> myVector(10); // 创建一个包含10个元素的容器,元素为int类型(值被系统默认初始化为0)
    • vector<int> myVector(10, -1); // 创建一个包含10个重复元素的容器,每个元素的值都是-1
  3. 常用方法:
    • myVector.push_back(val)// 插入元素val
    • myVector.pop_back(); // 删除vector末尾的元素
    • myVector.clear(); // 清空vector中的所有元素
    • myVector.empty(); // 判断vector是否不含有任何元素,如果长度为0,则返回真,否则,返回假

输出保留X位小数的数字

使用printf 函数,其中格式字符串"%.2f"表示输出一个浮点数并保留两位小数, 不过想要使用printf函数需要引入头文件<stdio.h>或者<cstdio>:

  • printf("%.2f\n",sum/count);

自定义链表结构体

struct ListNode{
    // 存储节点的数据
    int val;
    // 下一个节点也是链表节点,所以也是ListNode类型,*表示指针(地址),next是名称
    ListNode* next;
    // 构造函数
    ListNode(int x) : val(x),next(nullptr){}
}; //!!!注意 

set集合

  1. 头文件:
    • #include <unordered_set>// 引入<unordered_set>头文件
    • #include <set>// 引入set头文件
  2. 区别: image.png
  3. 声明方式:
    • unordered_set<int> mySet;// 创建一个存储整数的无序集合
    • set<int> mySet;// 创建一个存储整数的set
    • multiset<int> myMultiSet;// 创建一个存储整数的 multiset
  4. 常用方法:
    • mySet.insert(val);// 向集合中插入元素
    • mySet.erase(val);// 往集合中删除元素
    • if (mySet.find(i) != mySet.end())//判断元素是否在集合中, 只要不等于end(), 说明元素在集合中

map集合

  1. 头文件:

    • #include <unordered_map>// 引入unordered_map头文件,包含unordered_map类型
    • #include <map>// 引入map头文件,包含map类型和multimap类型
  2. 区别: image.png

  3. 声明方式:

    • unordered_map<int, int> uMap;// 声明一个整数类型映射到整数类型的 无序映射
    • map<string, int> myMap;//声明一个将字符串映射到整数的map
  4. 插入数据:使用[]操作符来插入

    • uMap[0] = 10;
    • uMap[10] = 0;
    • myMap["math"] = 100;
    • myMap["english"] = 80;
  5. 使用find函数来检查某个键是否存在于map中:

    • if (myMap.find("math") != myMap.end())
  6. 使用范围for循环来遍历map中的所有键值对:

    • 声明一个变量kv来存储每个键值对。这个变量的类型通常是pair类型。
    • 使用时通过first 和 second 成员来访问 pair 中的第一个和第二个元素, 它的 first 成员存储键,而 second 成员存储值。
    •   int x;
        cin>>x;
        //遍历map
        for(const pair<int,int>& kv : uMap){
            // 找到了
            if(kv.second == x){
                std::cout << kv.first << std::endl;
                flag = true;
                break;
            }
        }
      

栈stack

  1. 头文件:#include <stack> // 引入stack头文件
  2. 声明:stack<int> st; // 创建一个int类型的栈
  3. 常用操作:
    • empty(): 判断栈是否为空栈,如果为空栈返回true, 否则或者false
    • push(): 进栈操作,将新的元素放入到栈中,新的元素成为栈顶元素。
    • pop():出栈操作,栈顶元素从栈中离开
    • top(): 获取栈顶元素,但是不会移除它
    • size(): 获取栈的长度,即栈中元素的数量
  4. 举例:
    st.push(1); st.push(10); st.push(100); // 往栈中添加元素,现在栈底元素是1,栈顶元素是100 
    st.pop(); // 移除栈顶元素100,新的栈顶元素是10 
    int topNumber = st.top(); // 获取栈顶元素10 
    bool isEmpty = st.empty(); // 如果栈为空,返回true;否则返回false 
    int stackSize = st.size(); // 获取栈的长度(元素数量)
    

队列queue

  1. 头文件:#include <queue>
  2. 声明:queue<string> q; // 创建一个字符串类型的队列
  3. 常用操作:
    • empty(): 判断队列是否为空,如果队列为空返回true, 否则返回false
    • push(): 入队操作,将新的元素添加到队列的尾部。
    • pop(): 出队操作,移除队列的头部元素。
    • front(): 访问队列的头部元素,但不会将其移除。
    • size(): 获取队列的长度,即队列中元素的数量。
  4. 举例:
    q.push("Jack"); q.push("Mike"); // 入队了两个名称字符串 
    q.pop(); // 移除队列头部的元素 
    string name = q.front(); // 获取队列头部的元素但是不会将其移除 
    bool isEmpty = q.empty(); // 如果队列为空,返回true;否则返回false 
    int queueSize = q.size(); // 获取队列中元素的数量
    

面向对象编程:继承/封装/多态

image.png

#include<iostream>
#include<math.h>
#include<vector>
#include <string>
// 引入iomanip库文件,用于控制输出格式
#include <iomanip>

using namespace std;

// 基类
class Shape{
public:
    // ♥定义虚函数virtual,允许不同的对象使用相同的接口进行操作,但在运行时表现出不同的行为。
    // ♥const = 0 表示纯虚函数,该类不能被实例化,要求派生类必须实现这两个函数。
    virtual double CalculateArea() const = 0;
    virtual string GetType() const = 0;
};

class Rectangle : public Shape{
public:
    //构造函数,初始化参数列表写法
    Rectangle(int width, int height) : width(width), height(height){}
    
    // 计算面积
    // ♥方法的重写需要override关键字,其意思是子类重写父类的方法,并提供自己的实现
    // ♥使用const用来修饰函数,表示该函数不会修改对象的状态,能保证对对象的访问是安全的
    double CalculateArea() const override{
        // ♥static_cast<double>  隐式类型转换,将字符型转换为double类型
        return static_cast<double>(width * height);
    }
    
    // 获取类型
    string GetType() const override{
        return "Rectangle";
    }
private:
    int width;
    int height;
};

class Circle : public Shape{
public:
    Circle(int r):r(r){}
    
    double CalculateArea() const override{
        //隐式类型转换,将字符型转换为double类型
        return (3.14 * pow(r,2));
    }
    
    string GetType() const override{
        return "Circle";
    }
private:
    int r;
};

int main(){
    // 输入的类型
    std::vector<Shape*> shapes;
    while(true){
        // 获取输入的类型
        string type;
        cin >> type;
        // 如果输入的是 "end"
        if (type == "end") 
            break;
        if (type == "rectangle"){
            int width,height;
            cin>>width>>height;
            shapes.push_back(new Rectangle(width,height));
        }
        else if(type == "circle"){
            int r;
            cin>>r;
            shapes.push_back(new Circle(r));
        }
    }
    for(const Shape* shape : shapes){
        // ♥fixed << setprecision(2)表示输出小数点后两位
        std::cout << shape->GetType() <<" area: "<< fixed << setprecision(2)<<shape->CalculateArea()<<std::endl;
    }
    return 0;
}