跳转至

C++关键字与部分实用STL

关键字

A – C D – P R – Z
[alignas](https://en.cppreference.com/w/cpp/keyword/alignas) (C++11) [alignof](https://en.cppreference.com/w/cpp/keyword/alignof) (C++11) [and](https://en.cppreference.com/w/cpp/keyword/and) [and_eq](https://en.cppreference.com/w/cpp/keyword/and_eq) [asm](https://en.cppreference.com/w/cpp/keyword/asm) [atomic_cancel](https://en.cppreference.com/w/cpp/language/transactional_memory) (TM TS) [atomic_commit](https://en.cppreference.com/w/cpp/language/transactional_memory) (TM TS) [atomic_noexcept](https://en.cppreference.com/w/cpp/language/transactional_memory) (TM TS) [auto](https://en.cppreference.com/w/cpp/keyword/auto) (1) (2) (3) (4) [bitand](https://en.cppreference.com/w/cpp/keyword/bitand) [bitor](https://en.cppreference.com/w/cpp/keyword/bitor) [bool](https://en.cppreference.com/w/cpp/keyword/bool) [break](https://en.cppreference.com/w/cpp/keyword/break) [case](https://en.cppreference.com/w/cpp/keyword/case) [catch](https://en.cppreference.com/w/cpp/keyword/catch) [char](https://en.cppreference.com/w/cpp/keyword/char) [char8_t](https://en.cppreference.com/w/cpp/keyword/char8_t) (C++20) [char16_t](https://en.cppreference.com/w/cpp/keyword/char16_t) (C++11) [char32_t](https://en.cppreference.com/w/cpp/keyword/char32_t) (C++11) [class](https://en.cppreference.com/w/cpp/keyword/class) (1) [compl](https://en.cppreference.com/w/cpp/keyword/compl) [concept](https://en.cppreference.com/w/cpp/keyword/concept) (C++20) [const](https://en.cppreference.com/w/cpp/keyword/const) [consteval](https://en.cppreference.com/w/cpp/keyword/consteval) (C++20) [constexpr](https://en.cppreference.com/w/cpp/keyword/constexpr) (C++11) [constinit](https://en.cppreference.com/w/cpp/keyword/constinit) (C++20) [const_cast](https://en.cppreference.com/w/cpp/keyword/const_cast) [continue](https://en.cppreference.com/w/cpp/keyword/continue) [co_await](https://en.cppreference.com/w/cpp/keyword/co_await) (C++20) [co_return](https://en.cppreference.com/w/cpp/keyword/co_return) (C++20) [co_yield](https://en.cppreference.com/w/cpp/keyword/co_yield) (C++20) [decltype](https://en.cppreference.com/w/cpp/keyword/decltype) (C++11) [default](https://en.cppreference.com/w/cpp/keyword/default) (1) [delete](https://en.cppreference.com/w/cpp/keyword/delete) (1) [do](https://en.cppreference.com/w/cpp/keyword/do) [double](https://en.cppreference.com/w/cpp/keyword/double) [dynamic_cast](https://en.cppreference.com/w/cpp/keyword/dynamic_cast) [else](https://en.cppreference.com/w/cpp/keyword/else) [enum](https://en.cppreference.com/w/cpp/keyword/enum) (1) [explicit](https://en.cppreference.com/w/cpp/keyword/explicit) [export](https://en.cppreference.com/w/cpp/keyword/export) (1) (3) [extern](https://en.cppreference.com/w/cpp/keyword/extern) (1) [false](https://en.cppreference.com/w/cpp/keyword/false) [float](https://en.cppreference.com/w/cpp/keyword/float) [for](https://en.cppreference.com/w/cpp/keyword/for) (1) [friend](https://en.cppreference.com/w/cpp/keyword/friend) [goto](https://en.cppreference.com/w/cpp/keyword/goto) [if](https://en.cppreference.com/w/cpp/keyword/if) (2) (4) [inline](https://en.cppreference.com/w/cpp/keyword/inline) (1) [int](https://en.cppreference.com/w/cpp/keyword/int) [long](https://en.cppreference.com/w/cpp/keyword/long) [mutable](https://en.cppreference.com/w/cpp/keyword/mutable) (1) [namespace](https://en.cppreference.com/w/cpp/keyword/namespace) [new](https://en.cppreference.com/w/cpp/keyword/new) [noexcept](https://en.cppreference.com/w/cpp/keyword/noexcept) (C++11) [not](https://en.cppreference.com/w/cpp/keyword/not) [not_eq](https://en.cppreference.com/w/cpp/keyword/not_eq) [nullptr](https://en.cppreference.com/w/cpp/keyword/nullptr) (C++11) [operator](https://en.cppreference.com/w/cpp/keyword/operator) (4) [or](https://en.cppreference.com/w/cpp/keyword/or) [or_eq](https://en.cppreference.com/w/cpp/keyword/or_eq) [private](https://en.cppreference.com/w/cpp/keyword/private) (3) [protected](https://en.cppreference.com/w/cpp/keyword/protected) [public](https://en.cppreference.com/w/cpp/keyword/public) [reflexpr](https://en.cppreference.com/w/cpp/keyword/reflexpr) (reflection TS) [register](https://en.cppreference.com/w/cpp/keyword/register) (2) [reinterpret_cast](https://en.cppreference.com/w/cpp/keyword/reinterpret_cast) [requires](https://en.cppreference.com/w/cpp/keyword/requires) (C++20) [return](https://en.cppreference.com/w/cpp/keyword/return) [short](https://en.cppreference.com/w/cpp/keyword/short) [signed](https://en.cppreference.com/w/cpp/keyword/signed) [sizeof](https://en.cppreference.com/w/cpp/keyword/sizeof) (1) [static](https://en.cppreference.com/w/cpp/keyword/static) [static_assert](https://en.cppreference.com/w/cpp/keyword/static_assert) (C++11) [static_cast](https://en.cppreference.com/w/cpp/keyword/static_cast) [struct](https://en.cppreference.com/w/cpp/keyword/struct) (1) [switch](https://en.cppreference.com/w/cpp/keyword/switch) [synchronized](https://en.cppreference.com/w/cpp/language/transactional_memory) (TM TS) [template](https://en.cppreference.com/w/cpp/keyword/template) [this](https://en.cppreference.com/w/cpp/keyword/this) (4) [thread_local](https://en.cppreference.com/w/cpp/keyword/thread_local) (C++11) [throw](https://en.cppreference.com/w/cpp/keyword/throw) [true](https://en.cppreference.com/w/cpp/keyword/true) [try](https://en.cppreference.com/w/cpp/keyword/try) [typedef](https://en.cppreference.com/w/cpp/keyword/typedef) [typeid](https://en.cppreference.com/w/cpp/keyword/typeid) [typename](https://en.cppreference.com/w/cpp/keyword/typename) [union](https://en.cppreference.com/w/cpp/keyword/union) [unsigned](https://en.cppreference.com/w/cpp/keyword/unsigned) [using](https://en.cppreference.com/w/cpp/keyword/using) (1) [virtual](https://en.cppreference.com/w/cpp/keyword/virtual) [void](https://en.cppreference.com/w/cpp/keyword/void) [volatile](https://en.cppreference.com/w/cpp/keyword/volatile) [wchar_t](https://en.cppreference.com/w/cpp/keyword/wchar_t) [while](https://en.cppreference.com/w/cpp/keyword/while) [xor](https://en.cppreference.com/w/cpp/keyword/xor) [xor_eq](https://en.cppreference.com/w/cpp/keyword/xor_eq)

STL函数

unique

  • 去除容器中相邻的重复元素。unique(ForwardIterator first, ForwardIterator last),返回值为指向 去重后 容器结尾的迭代器,原容器大小不变(多于的元素放在最后而不会消失)。与 sort 结合使用可以实现完整容器去重。返回值为指针,取地址后为unique后不同值的数量。
int n,m,p,q,T,a[N],ans;
signed main(){
    cin>>n;
    for(int i=2;i<=n+1;i++)cin>>a[i];
    sort(a+2,a+n+2);
    auto val=unique(a+2,a+n+2);

    cout<<"val="<<val<<endl;
    cout<<"*val="<<*val<<endl;
    cout<<"&val="<<&val<<endl;
    for(int i=1;i<=n;i++)cout<<a[i]<<' ';
}
5
1 1 2 3 4
val=0x7ff73a62a0a8
*val=4
&val=0x4318fff788
1 2 3 4 4

lower_bound

  • 在一个有序序列中进行二分查找,返回指向第一个 大于等于 x 的元素的位置的迭代器。如果不存在这样的元素,则返回尾迭代器。`lower_bound(v.begin(),v.end(),x)```。
int n,m,p,q,T,a[N],ans;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    cin>>m;
    auto val=lower_bound(a+1,a+n+1,m);
    cout<<"val="<<val<<endl;
    cout<<"*val="<<*val<<endl;
    cout<<"&val="<<&val<<endl;
    cout<<"val-a="<<val-a<<endl;
}
5
1 3 4 7 8
6
val=0x7ff76767a0a0
*val=7
&val=0x9e41bffb80
val-a=4

nth_element

  • 按指定范围进行分类,即找出序列中第mid 大的元素,使其左边均为小于它的数,右边均为大于它的数。nth_element(v.begin(), v.begin() + mid, v.end(), cmp) 或 nth_element(a + begin, a + begin + mid, a + end, cmp)。
int n,m,p,q,T,a[N],ans;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    cin>>m;
    nth_element(a+1,a+m+1,a+n+1);
    for(int i=1;i<=n;i++)cout<<a[i]<<' ';
}
5
3 7 6 4 1
3
3 1 4 6 7

next_permutation

  • 重新排列范围内的元素[第一,最后一个)返回按照**字典序排列**的下一个值较大的组合。返回值为bool,当有下一个排列可用时返回1,排列穷尽时返回0,如果继续运行则返回字典序最小的情况重新开始。
int n,m,p,q,T,a[N],ans;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    cin>>m;
    while(m--){
        next_permutation(a+1,a+n+1);
        for(int i=1;i<=n;i++)cout<<a[i]<<' ';
        cout<<'\n';
    }
}
5
1 2 3 4 5
6
1 2 3 5 4
1 2 4 3 5
1 2 4 5 3
1 2 5 3 4
1 2 5 4 3
1 3 2 4 5

rand()

STL结构

set

使用Set便捷维护区间重叠问题

www.luogu.com.cn

rope

rope块状链表,用法同vector,删除x个元素erase(pos,x),插入insert(pos,v)

bitset

不仅仅是01数组,还有更优秀的操作。和vector一样属于原生压位的。

好例题

www.luogu.com.cn

union

以下代码

union node{
    int a;
    char b;
}s;

signed main(){
    cin>>s.a>>s.b;
    cout<<s.b<<endl<<s.a;

    return 0;
}
in: 456 c
out: c 99

即改变要求,会把其他的都变成改值对应类型的值。

tuple

取出元素可以使用get(mytuple),但是x必须是一个常量,不可以是变量。总之就是没啥用。

#include<bits/stdc++.h>
using namespace std;

int main(){
    tuple<int,int,string> t; //若干元素的元组
    int a,b;
    string s;
    cin>>a>>b>>s;
    t=make_tuple(a,b,s);

    int a1,a2,a3;
    tie(a1,a2,ignore)=t; //解包,ignore表示取出元素但是不复制给任何变量
    cout<<a1<<' '<<a2;

    return 0;
}