c转c++

C转C++

以下内容从bilibili博主别喷我id提炼学习

基本

输入输出

#include<iostream>//如同<stdio.h>,皆为输入输出库

using namespace std;//命名空间以防多库有同名函数

int main()
{
    int n;
    cin >> n;//输入
    cout << "输出:" << n << endl;
    //endl为换行
    return 0;
}

变量声明

bool变量:非0为true和0为false

const常变量:永不可改变

string类

#include<iostream>

using namespace std;

int main()
{
    bool a=true;
    const int MAX=400;
    string s="word";
    string s1="My ";
    string s2=s1+s;
    getline(cin,s);//接受一行,不是cin单纯遇空格停止
    string s_sub=s.substr(1,2);//从第1个字符取2个
    string s_sub1=s.substr(2);//从第2个字符取完
    cout << a << endl;//输出1
    cout << s.length() << endl;
    //s.length() 求s长度
    return 0;
}

结构体

struct stu
{
    sting s;
    int n;
};
int main()
{
    stu a;//可不用struct
}

引用&和传值

int main()
{
    int a=9;
    int b=&a;
    b=100;
    cout << a << endl;
    //输出100;
}

STL篇

vector可变数组

#include<vector>
int main()
{
    vector <int> v(10,2);//10个空间,值都是2
    vector <int> v1(9);//9个空间,值默认为0
    vector <int> v3;//无,待开辟
    v3.resize(5)//重新开辟5个空间给v3
    v.push_back(3)//在v数组后面插人3,数组长度+1
    for(auto p=v.begin();p!=v.end();p++)
        pass;
    //这种称为迭代器
    //auto报错代表编译器版本低于c++ 11;
    //顾名思义,无论v长度这样变,p都可从头走到未
}

set集合

set集合,里面数据各不相同并从小到大排序

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> s;
    // 插入一个元素5
    s.insert(5);
    // 查找元素3,如果不存在则返回s.end()
    cout << (s.find(3) == s.end()) << endl; 
    // 输出1,因为3不在set中
    s.erase(5);// 删除元素5
    // 遍历set
    for (auto it = s.begin(); it != s.end(); ++it) {
        // 这里可以对迭代器指向的元素进行操作
        cout << *it << " "; // 输出set中的元素
    }
    cout << endl;
    s.size();//s的长度
    return 0;
}

map键值对(字典)

map键值对,它自动按照键从小到大排序

#include<map>
#include<iostream>
using namespace std;

int main()
{
    map <string,int> m; 
    m["one"]=1;
    m["four"]=4;
    //[]里面的就是键,按照这个排序的
    cout << m["four"] << ',' << m["five"] << endl;
    //输出4,0
    for(auto p=m.begin();p!=m.end();p++)
       cout << p->first << ":" << p->second << "-";
    //输出:one:1-four:4
    m.sise();//m长度
    return 0;
}

stack栈

stack栈(先进后出,如:弹夹)

#include<stack>
#include<iostream>
using namespace std;

int main()
{
    stack <int> s;
    s.push(3)//将3进栈
    s.pop();//栈顶出栈
    s.top();//访问栈顶
    s.size();//获取长度
    //不可迭代器
    return 0;
}

queue队列

queue队列(先进先出,如:排队打饭)

#include<queue>
#include<iostream>
using namespace std;

int main()
{
    queue <int> s;
    s.push(3)//将3入列
    s.pop();//第一个入列的出列
    s.front();//访问对首(第一个入列的)
    s.back();//访问对尾(最后一个入列的)
    s.size();//获取长度
    //不可迭代器
    return 0;
}

unordered_map 和 unordered_set

unordered_map就是乱序的map(键对值)

unordered_set就是乱序的集合(集合)

为节省时间

位运算bitset

#include<bitset>

sort函数

#include<sort>

cctype头文件


c++11新特性

auto声明

基于范围的for循环

to_string

stoi stod

面向对象

基础

class和struct的区别

  • class默认访问权限是私有

  • struct默认访问权限是公共

public、protected、private访问权限的区别

public protected private
类内 可访问 可访问 可访问
类外 可访问 不可访问 不可访问
父类 可访问 可访问 不可访问

构造函数和析构函数的

构造函数 共同 析构函数
进行初始化
函数名和类名相同
有参数,可以发生重载
创建对象时,会自动调用,且只调用一次
没有返回值,不用写void 进行清理
函数名等于~类名
没有参数,不能发生重载
对象销毁前,会自动调用,且只调用一次
posted @ 2024-12-22 14:25  悟屈  阅读(18)  评论(0)    收藏  举报