算法竞赛中C++的简单特性(构造函数、IO、new动态内存、重载)学习笔记

c++ 的特性

结构体的构造函数

#include<iostream>
using namespace std;

//构造函数的写法1
struct st{
    int a, b;
    st(int _a, int _b){
        a = _a;
        b = _b;
    }
};

//构造函数的写法2
struct st_{
    int a, b;
    st_(int a_, int b_):a(a_), b(b_){}
    //st_(int a_ = 0, int b_):a(a_), b(b_){} 错误
}

int main(){
    st newSt = st(1, 2);//调用结构体的构造函数
    st newSt_(1, 2);
    //st newSt1;//编译错误:函数的缺省值
    cout << newSt.a << newSt.b;
    return 0;
}

/*
构造函数的写法1
结构体名(参数列表){
    赋值语句
}

构造函数的写法2
结构体名(参数列表):结构体成员(参数),结构体成员(参数){} 
*/
  • 构造函数的返回值是一个结构体
  • 构造函数与结构体重名
  • 结构体的构造函数在创建结构体是自动调用
  • 写了构造函数后,原本的无参构造函数会消失,声明是必须使用构造函数

常见输入输出

  • getline()读入包含空格的整行数据

    • getline(cin, 字符串变量名字);
    • getline(字符串变量名字, 读入字符上线);
    • getline()前如有其他操作,必须加入getchar()来吞掉换行符/n
  • 循环读入直到文件结束(判断EOF结束循环)

    • int a, b;
      while(cin >> a >> b){
      
      }
      

动态内存分配

#incldue<iostream>
using namespace std;

struct node{
    int value;
    node* next;
    node(int _value){
        value = _value;
    }
};
node* head;

int main(){
    head = new node(1);//用new动态开辟head指向node的空间//node变量的地址赋值在head
    head->next = new node(2);//对head内部的next进行操作(用new动态开辟head的next指向node的空间)
    node* second = head->next;//声明一个node型的指针名为second
    
    cout << head->value << second->value << head->next->value;
    return 0;
}
  • 动态内存的分配一般用于新建结构体上
  • 可以不用释放内存

函数的重载

#include<iostream>
using namespace std;

int sum(int a){
    cout<<"���õ��� int sum(int a)"<<endl;
    return a;
}
double sum(double a){
    cout<<"���õ��� double sum(double a)"<<endl;
    return a;
} 
int sum(int a,int b){
    cout<<"���õ��� sum(int a,int b)"<<endl;
    return a+b;
}
int main()
{
    int a = 1;
    int b = 1;
    double c = 1;
    
    cout<<sum(a)<<endl;
    cout<<sum(c)<<endl;
    cout<<sum(a,b)<<endl;
    return 0;
}
  • cpp中区别不同函数不仅靠函数名,对于不同的函数至少满足一下一种情况:

    • 函数名不同

    • 形参类型或数量不同

    • 有缺省值不引起调用时的歧义

      • int sum(int a){}
        double sum(int a){}
        //返回值不能区别函数
        
    函数的缺省值

    在cpp的形参列表中可以允许缺省值的存在,在没有传入对应变量时,使用的就是缺省值

    但使用时要遵循:

    参数列表从左往右,一旦出现缺省值参数后,后面的参数也必须带有缺省值

    int sum(int a,int b = 0){
      cout << a << b;
    }
    int main(){
      sum(1);
          return 0;
    }   
    

运算符的重载

写法一:

#include<iostream>
using namespace std;

struct str{
    int A_score, B_score;
    str(int _A, int _B):A_score(_A), B_score(_B){}
    bool operator < (const str b){
        return this->A_score < b.A_score;
    }    
};
/*
bool operator 运算符(const 结构体名称 b){
    return 什么是时候这个运算符对结构体成立;
}
*/
int main(){
    str a(2, 6), b(7, 1);
    cout << (a < b);    
    return 0;
}

写法二:

#include<bit/stdcpp.h>
using namespace std;

struct node{
    int x, y;
};

bool operator < (node a, node b){
    return a.x < b.x
}

int main(){
    priority_queue<node> pq;
    pq.push({8,3});
    pq.push({4,6});
    pq.push({5,7});
    while(!pq.empty()){
        cout << pq.top().x << pq.top().y;
        pq.pop();
    }
    return 0;
}

优先队列中如果是结构体这种无法直接比较的元素,必须要重载运算符<,使其可以比较。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wzx_1210

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

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

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

打赏作者

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

抵扣说明:

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

余额充值