OL test C++随记

这是一篇在网测刷题时遇到的一些小知识,记录下来以备查询。

 

1. 包括了所有标准头文件的include语句

include <bits/stdc++.h>

2. set中没有重复的元素

所以求日活跃用户数(DAU)的时候,只需要:

#include <bits/stdc++.h>
using namespace std;//统计不重复的数字, 利用set就可以了
int main() {
    long long x;
    set<long long> st;
    while (cin >> x) {
        if (x == 0) break;
        st.insert(x);
    }
    cout << st.size() << endl;;
    return 0;
}

3. 如何从OJ中读取整行数据,两种方法,分别是cin和scanf:

//cin的方法
vector<int> input(n,0);
for(int i=0; i<n; i++)
{
    cin>>input[i];
}


//当遇到大数的时候,scanf效率更高
vector<int> r(n, 0);
for(int i = 0; i < n; i++) scanf("%d", &x[i]);

//同理,输出的时候,用printf效率也会更高
printf("%d\n", max);

4. 注意int的上下限,分别是正负21亿四千多;

5. 运用sort对vector进行排序

Remark:vector内部的sort采用的是快速排序,在占用内存不大时候,比multiset(multimap实现基于multiset)快,后者基于红黑树。

此部分转自:关于C++中vector和set使用sort方法进行排序

第二种情形:用自定义的结构体进行sort算法,

这时候需要自己定义个比较函数,因为sort算法是基于容器中的元素是可以两两比较的,然后从小到大排序,所以要自定义怎么样才是小于('<')

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{
    char name[10];
    int score;
};
//自定义“小于”
bool comp(const student &a, const student &b){
    return a.score < b.score;
}
int main(){
    vector<student> vectorStudents;
    int n = 5;
    while (n--){
        student oneStudent;
        string name;
        int score;
        cin >> name >> score;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        vectorStudents.push_back(oneStudent);
    }
    cout << "===========排序前================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    sort(vectorStudents.begin(),vectorStudents.end(),comp);
    cout << "===========排序后================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    return 0;
}

运行结果:

不过有时候一个排序条件不够,比如要求学生按分数从高到低排序,如果分数相同,则按照年龄从大到小排序

就需要在comp自定义函数里面修改一下判断了,原来是直接return a.score < b.score

现在就需要判断:

if (a.score > b.score)
   return true;
else if (a.score == b.score  && a.age > b.age)
   return true;
else                
   return false;
这里一定要记得else return false!!!

完整代码如下:

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{
    char name[10];
    int score;
    int age;
};
//自定义“小于”
bool comp(const student &a, const student &b){
    if (a.score > b.score)
        return true;
    else if (a.score == b.score  && a.age > b.age)
        return true;
    else                ///这里的else return false非常重要!!!!!
        return false;
}
int main(){
    vector<student> vectorStudents;
    /*set<student> setStudents;*/
    //int n = 5;
    int n = 6;
    while (n--){
        student oneStudent;
        string name;
        int score;
        int age;
        cin >> name >> score>>age;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        oneStudent.age = age;
        vectorStudents.push_back(oneStudent);
    }
    cout << "===========排序前================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << " age: "<<it->age<<endl;
    }
    sort(vectorStudents.begin(), vectorStudents.end(), comp);
    //sort(setStudents.begin(), setStudents.end());
    cout << "===========排序后================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << " age: " << it->age << endl;
    }
    return 0;
}

运行结果:

6. 运用sort对set进行排序

此部分转自:关于C++中vector和set使用sort方法进行排序

set是一个集合,内部的元素不会重复,同时它会自动进行排序,也是从小到大

而且set的insert方法没有insert(a,cmp)这种重载,所以如果要把结构体插入set中,我们就要重载'<'运算符。

set方法在插入的时候也是从小到大的,那么我们重载一下<运算符让它从大到小排序

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{
    char name[10];
    int score;
};
//自定义“小于”
bool comp(const student &a, const student &b){
    return a.score < b.score;
}
bool operator < (const student & stu1,const student &stu2){
    return stu1.score > stu2.score;
}
int main(){
    //vector<student> vectorStudents;
    set<student> setStudents;
    //int n = 5;
    int n = 6;
    while (n--){
        student oneStudent;
        string name;
        int score;
        cin >> name >> score;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        setStudents.insert(oneStudent);
    }
    cout << "===========排序前================" << endl;
    for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    //sort(setStudents.begin(), setStudents.end(), comp);
    //cout << "===========排序后================" << endl;
    //for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++){
    //    cout << "name: " << it->name << " score: " << it->score << endl;
    //}
    return 0;
}

运行结果:

我们可以看到,set内元素不会重复,而且它按照它所认为的“从小到大”进行了排序

 

7. vector 的遍历方法

1)标准c++方法

vector<int>res = {0,1,2,3,4,5,6,7,8,9};
int size = res.size();
for(int i =0;i<size;i++)
{
    cout<<res[i]<<endl;
}

 

2)c++11的方法

vector<int> res= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for(int i:res)cout<<i<<endl;

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值