acm实用拓展知识总结

IO

long double %Lf
unsigned long long %llu
unsigned int %u 

char s[MAX];
scanf(" %[^\n]", s); //遇到回车结束(读一整行)
scanf(" %[^ ]", s); //遇到空格结束

ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
sscanf(s, "%d", &a);

namespace fastIO{
    const int BUF_SIZE = 1000005;
    bool IOerror = 0;
    inline char nc(){
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend){
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1){
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';}
    inline void read(int &x){
        char ch;
        while(blank(ch = nc()));
        if(IOerror) return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    inline void read(char &x){
        char ch;
        while(blank(ch = nc()));
        if(IOerror) return;
        x = ch;
    }
    inline int readstr(char *x){
        char ch;
        while(blank(ch = nc()));
        if(IOerror) return -1;
        int i = 0;
        for(i; ch >= '0' && ch <= '9'; i++){
            x[i] = ch;
            ch = nc();
        }
        x[i] = '\0';
        return i;
    }
}
using namespace fastIO;

random

srand(time(0));
rand();0-32767

vector<int> a(n);
random_shuffle(a.begin(), a.end());
//更随机

Sequence Operation

//sort
sort(a, a + n);
sort(a, a + n, greater<int>());
sort(a, a + n, cmp);
sort(vec.begin(), vec.end(), [=](pii a, pii b) -> bool {
        return a.first < b.first;
});  //匿名函数 

//unique
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());

//random_suffle
random_shuffle(a.begin(), a.end());

next_pumutation

string str = "1234";
while (next_permutation(str.begin(), str.end()))
    cout << str << endl;

int a[] = { 1,2,3 };
sort(a, a + 3);
do {
    cout << a[0] << ' ' << a[1] << ' ' << a[2] << '\n';
} 
while (next_permutation(a, a + 3));

string

可以直接 < == > = sort
 string s;
cin >> s; // s = “12345”
s += “12345”; // s = “1234512345”
s.erase(5, 3); // s = “1234545”
int a = s.find(“45”); // a = 3 kmp
int b = s.find(“6”); // b = -1
int c = s.length(); // c = 7 size()
string t = s.substr(4, 2); // t = “54”

priority_queue

priority_queue<int> pq;

priority_queue<int, vector<int>, greater<int> >pq2;//小根堆

struct cmp1 { bool operator ()(int &a, int &b) { return a > b; } };
priority_queue<int, vector<int>, cmp1>pq3;/

set/multiset

find logn
insert logn
erase logn
size
empty

s.erase(2);//把2全删
s.erase(s.find(2));//删1个2
lower_bound(s.begin(), s.end(), 5);
s.lower_bound(5);

iterator

set<int> s;
for (set<int>::iterator i = v.begin(); i != v.end(); i++)   cout << *i << endl;
for(auto i:s) cout << i << endl;  //auto的自动类型推断发生在编译期,所以使用auto并不会造成程序运行时效率的降低

c++ 11实用特性

//auto
vector<pair<int, int>> v;
vector<int> a = {1, 2, 3, 4, 5};
for (int &i : a) {i++;}
for (int i : {2, 3, 7, 61, 24251}) {prime_test(i);}

//container improvements
vector<pair<string, int>> vp;
string s;int i;
while (cin >> s >> i) {   
     vp.push_back(pair<string,int>{s, i});    
     vp.emplace_back(s, i);
}

//initializer list
list<pair<string,string>> languages = {{"Richards","BCPL"}, {"Ritchie","C"}};
map<vector<string>,vector<int>> years = {{ {"Maurice","Vincent"},{1945, 1951, 1967, 2000} },{ {"Martin", "Ritchards"}, {1982, 2003, 2007} }, { {"David", "John", "Wheeler"}, {1947, 1951, 2004} }}; 

//max & min
int x = max({ 1, 2, 3, 4 });

//Unordered containers O(1)
unordered_set<Elem,Hash,Cmp> //一个无序set,使用Hash作为hash函数,使用Cmp进行比较
unordered_multiset<Elem,Hash,Cmp> //一个无序multiset,使用Hash作为hash函数,使用Cmp进行比较
unordered_map<Key,T,Hash,Cmp> //一个无序map,使用Hash作为hash函数,使用Cmp进行比较
unordered_multimap<Key,T,Hash,Cmp> //一个无序multimap,使用Hash作为hash函数,使用Cmp进行比较
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值