首页 > 其他分享 >常用STL

常用STL

时间:2023-09-22 21:34:12浏览次数:36  
标签:std 常用 string STL int include com size

参考链接:

[C++ STL] vector使用详解 - fengMisaka - 博客园 (cnblogs.com)

[C++ STL] deque使用详解 - fengMisaka - 博客园 (cnblogs.com)

[C++ STL] list使用详解 - fengMisaka - 博客园 (cnblogs.com)

[C++ STL] set使用详解 - fengMisaka - 博客园 (cnblogs.com)

[C++ STL] map使用详解 - fengMisaka - 博客园 (cnblogs.com)

[C++ STL] 常用算法总结 - fengMisaka - 博客园 (cnblogs.com)

Acwing算法基础课

std::string

cplusplus.com/reference/string/string/substr/

string substr (size_t pos = 0, size_t len = npos) const;

cplusplus.com/reference/string/string/find/

size_t find (const string& str, size_t pos = 0) const noexcept;

cplusplus.com/reference/string/string/erase/

string& erase (size_t pos = 0, size_t len = npos);

cplusplus.com/reference/string/to_string/

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

cplusplus.com/reference/string/stoi/

// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi

int main () {
  std::string str_dec = "2001, A Space Odyssey";
  std::string str_hex = "40c3";
  std::string str_bin = "-10010110001";
  std::string str_auto = "0x7f";

  std::string::size_type sz;   // alias of size_t

  int i_dec = std::stoi (str_dec,&sz);
  int i_hex = std::stoi (str_hex,nullptr,16);
  int i_bin = std::stoi (str_bin,nullptr,2);
  int i_auto = std::stoi (str_auto,nullptr,0);

  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
  std::cout << str_hex << ": " << i_hex << '\n';
  std::cout << str_bin << ": " << i_bin << '\n';
  std::cout << str_auto << ": " << i_auto << '\n';

  return 0;
}

cplusplus.com/reference/string/stod/

// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main () {
  std::string orbits ("365.24 29.53");
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
  return 0;
}

vector

变长数组,倍增的思想

  • size() 返回元素个数(所有容器都有,时间复杂度为O(1))
  • empty() 返回是否为空(所有容器都有,时间复杂度为O(1))
  • clear() 清空
  • front()/back() 返回第一个数a[0]/最后一个数a[a.size()-1]
  • push_back()/pop_back() 插入/弹出最后一个数
  • begin()/end() 迭代器第一个数a[0]/最后一个数的后面一个数a[a.size()]
  • [] 支持随机访问(下标)
  • 支持比较运算,按字典序比较
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>

using namespace std;

int main() {
    // vector 初始化
    vector<int> a;
    vector<int> a(10); // 定义一个长度为10的vector
    vector<int> a(10, 3); // 定义一个长度为10的vector,每个元素都初始化为3

    // vector 添加元素
    for (int i = 0; i < 10; i++) {
        a.push_back(i);
    }
    // vector遍历
    // 下标访问vector
    for (int i = 0; i < a.size(); i++) {
        cout << a[i] << ' ';
    }

    // 迭代器访问vector
    for (vector<int>::iterator i = a.begin(); i != a.end(); i++) {
        cout << *i << ' ';
    }

    // 范围遍历
    for (auto x: a) {
        cout << x << ' ';
    }
    
    // vector 支持比较运算,按字典序比较
    vector<int> a(4, 3), b(3, 4);
    if (a < b) {
        puts("a < b");
    }
    
    return 0;
}

pair

存储一个二元组,前后两个变量的类型可以是任意的。

某个东西有两种不同的属性,有一个属性需要排序,可以将需要排序的属性放到first中,将不需排序的关键字放到second中。

嵌套的pair可以用于存储一个三元组。

  • first 第一个元素
  • second 第二个元素
  • 支持比较运算,按字典序,以first为第一关键字,以second为第二关键字
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main() {
    // pair 初始化
    pair<int, int> p1;
    
    pair<int, string> p2;
    p2 = make_pair(10, "yxc");
    p2 = {20, "abc"};
    
    // 嵌套的pair可以用于存储三元组
    pair<int, pair<int, int>> p3;

    return 0;
}

string

字符串,substr(),c_str()

  • size()

  • length()

  • empty()

  • clear()

  • 支持加法运算

  • substr() 第一个参数是位置,第二个参数是长度

    如果长度大于string则一直输出到最后一个字母

    第二个参数可省略,省略时也是从pos开始一直输出到最后一个字母

  • c_str() 返回字符数组的起始地址

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main() {
    // 初始化
    string a = "yxc";

    // 支持加法运算
    a += "def";
    a += "c";

    cout << a.substr(1, 2) << endl; // 输入字符串从1开始的两个字符
    cout << a.substr(1, 10) << endl; // 输入字符串从1开始到最后一个字符
    cout << a.substr(1) << endl; // 输入字符串从1开始到最后一个字符

    printf("%s\n", a.c_str());

    return 0;
}

queue

队列

  • size()

  • empty()

  • push() 向队尾插入一个元素

  • front() 返回队头元素

  • back() 返回队尾元素

  • pop() 弹出队头元素

  • 没有clear() 函数,如果想清空队列,可以重新构造一个队列

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

int main() {
    // 初始化
    queue<int> q;
    // 没有clear()函数,如果想清空,可以重新构造一个队列
    q = queue<int>();

    return 0;
}

priority_queue

优先队列,默认是大根堆

  • size()

  • empty()

  • 没有clear()函数

  • push() 插入一个元素

  • top() 返回堆顶元素

  • pop() 返回堆顶元素

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;

int main() {
    // 初始化,默认为大根堆
    priority_queue<int> heap;
    int x;
    cin >> x;

    // 定义小根堆的两种方式
    // 方式1:直接插入一个负数
    heap.push(-x);

    // 方式2:多加两个参数
    priority_queue<int, vector<int>, greater<int>> heap;

    return 0;
}

stack

  • size()

  • empty()

  • 没有clear()函数

  • push() 向栈顶插入一个元素

  • top() 返回栈顶元素

  • pop() 弹出栈顶元素

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>

using namespace std;

int main() {
    // 初始化
    stack<int> s;

    // 没有clear()函数
    while (!s.empty()) {
        s.pop();
    }
    

    return 0;
}

deque

双端队列,两端都能插入删除,支持随机访问,缺点是速度慢

  • size()
  • empty()
  • clear()
  • front()/back()
  • push_back()/pop_back()
  • push_front()/pop_front()
  • begin()/end()
  • [] 支持随机访问

set, map, multiset, multimap

set, map, multiset, multimap, 基于平衡二叉树(红黑树), 动态维护有序序列

  • size()
  • empty()
  • clear()
  • begin()/end() ++, -- 返回前驱和后继,时间复杂度O(logn)

set 无重复元素,插入重复元素会被忽略

multiset 有重复元素

  • insert() 插入一个数

  • find() 查找一个数

  • count() 返回某一个数的个数

  • erase()

    对multiset,如果输入一个数x,则删除所有x,时间复杂度O(k + logn),k是x的个数

    如果输入一个迭代器,则删除这个迭代器

  • lower_bound() 返回大于等于x的最小的数的迭代器,如果不存在返回end()

  • upper_bound() 返回大于x的最小的数的迭代器

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>

using namespace std;

int main() {
    set<int> s;
    multiset<int> ms;
    
    return 0;
}

map

multimap

  • insert() 插入的数是一个pair
  • erase() 输入的参数是pair或者迭代器
  • find()
  • [] 时间复杂度O(logn)
  • lower_bound()/upper_bound() 返回大于x的最小的数的迭代器
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;

int main() {
    map<string, int> a;
    
    a["yxc"] = 1;
    cout << a["yxc"] << endl;
    
    return 0;
}

unordered_set, unordered_map, unordered_multiset, unordered_multimap

哈希表,和上面类似,增删改查的时间复杂度是O(1)

不支持lower_bound()/upper_bound(),迭代器的++,--

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>

using namespace std;

int main() {
    unordered_map<string, int> a;
    
    a["yxc"] = 1;
    cout << a["yxc"] << endl;

    unordered_multimap<string, int> b;

    return 0;
}

bitset

压位,省8倍空间

  • bitset<10000>s;

  • ~, &, |, ^

  • >>, <<

  • ==, !=

  • []

  • count() 返回有多少个1

  • any() 返回是否至少有一个1

  • none() 判断是否全为0

  • set() 把所有位置成1

  • set(k, v) 将第k位变成v

  • reset() 把所有位变成0

  • flip() 等价于~

  • flip(k) 把第k位取反

标签:std,常用,string,STL,int,include,com,size
From: https://www.cnblogs.com/catting123/p/17723432.html

相关文章

  • C++ STL 容器简单讲解
    STL简单讲解网上有很多很好的资料可以参考而直接看标准是最准确清晰的vectorstackqueue/priority_queuedequearraymap/multimapset/multisetunordered_mapunordered_set关于指针和迭代器!!!pbds……本文默认认为读者会基本的STL应用。一切STL......
  • math 库中常用的数学运算和常量【GO 基础】
    〇、关于mathGO语言的math库是一个内置的标准库,其中包含了许多数学函数和常量,用于计算各种数学运算和统计学计算。日常开发中,计算当然是少不了的,那么今天来梳理下备查。一、测试示例1.1小数位的:Round-四舍五入、RoundToEven-四舍/五至偶数funcRound(xfloat64)float6......
  • Python functools模块:提升函数式编程的5个常用函数
    Python的functools模块是标准库中的一个强大工具,提供了一系列函数,用于优化和增强函数式编程的能力。这些函数可以帮助我们处理函数、操作装饰器、缓存结果等。介绍functools模块中的五个常用函数,包括 partial、wraps、lru_cache、reduce和compose,并提供相关的代码示例,帮助更好......
  • 常用的校验算法
     CRC校验码publicstaticstringCRCCheck(stringval){val=val.TrimEnd('');string[]spva=val.Split('');byte[]bufData=newbyte[spva.Length+2];bufData=ToBytes......
  • gin常用操作
    介绍官方介绍​ Gin是一个用Go(Golang)编写的Web框架。它具有类似martini的API,性能要好得多,多亏了httprouter,速度提高了40倍。如果您需要性能和良好的生产力,您一定会喜欢Gin中文官网使用简单例子packagemainimport( "net/http" "github.com/gin-gonic......
  • 常用正则表达式
    一、校验数字的表达式数字:[1]*$n位的数字:^\d{n}$至少n位的数字:^\d{n,}$m-n位的数字:^\d{m,n}$零和非零开头的数字:^(0|[1-9][0-9]*)$非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$带1-2位小数的正数或负数:^(-)?\d+(.\d{1,2})$正数、负数、和小数:^(-|+)?\d......
  • EL表达式和JSTL标签库
    什么是EL表达式以及他的作用EL表达式和jsp表达式脚本输出对比a.jsp<%--CreatedbyIntelliJIDEA.User:SWTDate:2023/9/14Time:22:59TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8......
  • 记录常用设计模式
    策略模式+工厂模式1.四种玩法,玩法出入参类型一致,根据类型区分具体玩法。/***玩法枚举类**@authorjiangym*@version1.0*@date2022/9/2820:10*/publicenumTricksEnum{/***vip身份用户*/VIP_USER(1,"vip身份用户"),/**......
  • MFC-GUI常用API
    替换m_comboSNCodeRandom.ResetContent();m_comboSNCodeRandom.AddString("数字0-9");m_comboSNCodeRandom.AddString("大写字母A-Z");m_comboSNCodeRandom.AddString("小写字母a-z");m_comboSNCodeRandom.InsertString(0,"数字0-9");m_comb......
  • 常用的内置模块
    时间模块这个模块我们就比较熟悉了,之前学的时候调用过内部的几个功能三种时间表现形式1.时间戳(timestamp)返回秒数。通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。2.结构化时间(struct_time)主要是给......