STL 【 C ++ 】

STL中的基本容器:

vector :  变长数组 , 倍增的思想(由于系统为某一程序分配空间时,所需时间与空间大小无关,与申请次数有关,所以每次增加数组长度时会增加一倍(空间换时间))

pair : 存储一个二元组

string : 字符串 , substr() , c_str()

queue : 队列 , push() , front() , pop()

priority_queue : 优先队列 ,默认是大根堆, push() , top() , pop()

stack : 栈 , push() , top() , pop()

deque :  双端队列(不常用,效率比一般的数组慢好几倍)

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

unordered_set , unordered_map , unordered_multiset , unordered_multimap : 哈希表

bitset :  压位

next_permutation:排列


用到的头文件: 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>

基本用法:

一、vector

#include <vector>

int main()
{
    vector<int> a , a(10) , a[10] , a(10,3);
    a.size(); //返回元素个数 ,O(1)
    a.empty(); // 返回是否为空 , O(1)
    a.clear(); // 清空
    vector<int>::iterator it; //迭代器
    类似指针 *it 表示 a[0] , *(it+2) 表示 a[2]
    a.front()/a.back(); // 返回第一个/最后一个数
    a.push_back()/a.pop_back(); // 在最后插入一个数/删掉最后一个数
    a.begin()/a.end(); // 返回第0个数/最后一个数后面的一个数
    *a.begin() = a[0]
    a[8] , a[84]; // 随机取址

    遍历:
    一:
    for(int i = 0 ; i < 10 ; i ++) a.push_back(i);
    for(int i = 0 ; i < a.size() ; i ++) cout << a[i] << ' ' ;
    cout << endl;
    二:迭代器:可以理解为指针,所以需要用*来取出值
    for(vector<int>::iterator i = a.begin() ; i != a.end() ; i ++ ) cout << *i << ' ';
    cout << endl;
    三:
    for(auto x : a) cout << x << ' ';
    cout << endl;

    支持比较运算(按字典序):
    vector<int> a(4,3) , b(3,4);
    if(a < b) puts("a < b");

    return 0;
}

二、pair

int main()
{
    //创建和初始化:
    pair<string , pair<int , int> > P;  //<>里面可以是任意类型的两个变量
    pair<int , string> p(10,"abc");
    pair<int , string> p1,p2; 
    p1 = make_pair(10 , "abc");
    p2 = p1;

    p2.first; // 第一个元素
    p2.second; // 第二个元素
    cout << p2.first << p2.second << endl;

    支持比较运算,以first为第一关键字,second为第二关键字(按字典序)

    return 0;
}

三、string

int main()
{
    string s;
    s.size(); //返回字符个数
    s.empty(); //返回字符串是否为空
    s.clear(); // 清空字符串

    支持加法运算(每个加法运算都有一个运算符是string):
    s += "abc";
    s += 'c';

    s.substr(1,2); //返回某段字符串,第一个参数表示从这一位置(1)开始,第二个参数表示返回字符串长度(2)
    s.substr(1); // 省略第二个参数,返回从1(第一个参数)开始到末尾的字符串

    输出:
     cout << s << endl;
     printf("%s\n",s.c_str());
    
    return 0;
}

四、queue

#include <queue>

int main()
{
    queue<int> q;
    q.size() , q.empty();
    q.push(5); // 向队尾插入一个元素
    q.front(); // 返回队头元素
    q.back(); // 返回队尾元素
    q.pop(); // 弹出队头元素

    q = queue<int>(); // 清空只需要重新定义一下

    return 0;
}

五、priority_queue

#include <queue>
#include <vector>

int main()
{
    priority_queue<int> h; //默认大根堆
    定义成小根堆的方式:
    priority_queue<int , vector<int> , greater<int> > q;
    
    如果定义一个结构体类型的优先队列大根堆必须要重载一个小于号,小根堆重载大于号
    struct rec
    {
        int a, b;
        bool operator< (const rec& t) const
        {
            return a < t,a;
        }
    };
    priority_queue<rec> d;

    struct Rec
    {
        int a, b;
        bool operator> (const rec& t) const
        {
            return a > t,a;
        }
    };
    priority_queue<Rec , vector<int> , greater<int> > q;

    h.push(8);//插入一个元素
    h.top();//返回堆顶元素
    h.pop();//弹出堆顶元素
 

    return 0;
}

六、stack

#include <stack>

int main()
{
    stack<int> s;
    s.size(),s.empty();
    s.push(4); // 向栈顶插入一个元素
    s.top(); //返回栈顶元素
    s.pop(); //弹出栈顶元素

    return 0;
}

七、deque(不常用,效率低)

#include <deque>

int main()
{
    deque<int> q;
    q.size(),q.empty();
    q.front()/q.back(); //返回第一个/最后一个元素
    q.push_back()/q.pop_back(); //向最后插入一个元素/弹出最后一个元素
    q.push_front()/q.pop_front(); //从队首插入/弹出队首元素
    q.begin()/q.end(); //迭代器
    q.clear();
    q[5],q[8];//随机取址

    return 0;
}

八、set、multiset、map、multimap(自动排序)

#include <set>
#include <map>

int main()
{
    增删改查的操作时间复杂度都是O(logn)
    int x;
    public:
        size();
        empty();
        clear();
        begin()/end(); //支持++,--操作,返回前驱和后继,O(logn)
        set<int>::iterator it = a.begin();
        it ++ , it --;
        ++ it , -- it;

    set<int> s/multiset<int> ms; 
    //set里面不允许有重复的数
    //set定义结构体类型必须重载小于号

        insert(x); //插入一个数(6)
        find(x); //查找一个数(4),不存在返回s.end()
        count(x); //返回某个数(5)的个数
        erase();//(1) 输入是一个数x,删除所有x  O(k+logn),k是x的个数
                //(2)输入是一个迭代器,删除迭代器
        lower_bound(x); //返回大于等于x的最小的数的迭代器
        upper_bound(x); //返回大于x的最小的数的迭代器

    map<string,int>a/multimap<string,int>ua;
        insert(x); //插入的数是一个pair
        erase(); //输入的参数是pair或者迭代器
        find(x);
        a["abc"] = 1; //可以像数组一样用,O(logn)
        lower_bound(x)/upper_bound(x);

    return 0;
}

九、unordered_set , unordered_map , unordered_multiset , unordered_multimap(不会自动排序)

#include <unordered_set>
#include <unordered_map>

int main()
{
    增删改查的时间复杂度都是O(1)
    int x;
    public:
        size();
        empty();
        clear();

    unordered_set<int> s/unordered_multiset<int> ms; //set里面不允许有重复的数

        insert(x); //插入一个数(6)
        find(x); //查找一个数(4),不存在返回s.end()
        count(x); //返回某个数(5)的个数
        erase();//(1) 输入是一个数x,删除所有x 
                //(2)输入是一个迭代器,删除迭代器

    unordered_map<string,int>a/unordered_multimap<string,int>ua;
        insert(x); //插入的数是一个pair
        erase(); //输入的参数是pair或者迭代器
        find(x);
        a["abc"] = 1; //可以像数组一样用

    return 0;
}

十、bitset

#include <bitset>

int main()
{
   bitset<10000> s;
   支持所有位运算:
    ~ , & , | , ^ , >> , << , == , != , []
    
    count(); //返回有多少个1
    any(); //判断是否至少有一个1
    none(); // 判断是否全为0

    set(); // 把所有位置成1、
    set(k,v); // 将第k为变成v
    reset(); // 把所有位变成0
    flip(); // 等价于~
    flip(k) ; // 把第k位取反

    return 0;
}

十一、next_permutation 

next_permutation返回传入数组的下一个排列(按字典序从小到大),返回的是最大序列时返回false。时间复杂度O(n)。

求数字数组全排列

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int> > permutation(vector<int>& nums)
{
    sort(nums.begin() , nums.end());
    vector<vector<int> > res;
    do res.push_back(nums); while(next_permutation(nums.begin() , nums.end()));
         
    return res;
}

int main()
{
    vector<int> a ({1,2,3});
    vector<vector<int> > ans =  permutation(a);

    for(vector<int> x : ans)
    {
        for(int y : x) cout << y << ' ';
        cout << endl;
    }

    return 0;
}

输出: 

1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
STL(C++标准模板库)可以用来实现图书管理系统,其中可以使用vector来存储图书的信息,使用map来实现关键字搜索,使用algorithm来实现排序等功能。 以下是一个简单的图书管理系统的示例代码: ```c++ #include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> using namespace std; struct Book { string name; string author; int num; }; // 用vector存储图书信息 vector<Book> books; // 用map存储关键字 map<string, vector<int>> index; // 添加图书 void addBook(string name, string author, int num) { Book book; book.name = name; book.author = author; book.num = num; books.push_back(book); // 更新关键字索引 int index = books.size() - 1; string keyword = name + author; for (int i = 0; i < keyword.length(); i++) { string sub = keyword.substr(i, 1); if (index.find(sub) == index.end()) { index[sub] = vector<int>(); } index[sub].push_back(index); } } // 根据名称和作者搜索图书 void searchBook(string name, string author) { string keyword = name + author; vector<int> result; for (int i = 0; i < keyword.length(); i++) { string sub = keyword.substr(i, 1); if (index.find(sub) != index.end()) { for (int j = 0; j < index[sub].size(); j++) { int idx = index[sub][j]; if (books[idx].name == name && books[idx].author == author) { result.push_back(idx); } } } } if (result.empty()) { cout << "没有找到相关图书" << endl; } else { cout << "找到了" << result.size() << "本相关图书" << endl; for (int i = 0; i < result.size(); i++) { cout << "名称:" << books[result[i]].name << endl; cout << "作者:" << books[result[i]].author << endl; cout << "数量:" << books[result[i]].num << endl; cout << endl; } } } // 根据名称排序图书 bool cmpByName(Book a, Book b) { return a.name < b.name; } // 根据作者排序图书 bool cmpByAuthor(Book a, Book b) { return a.author < b.author; } // 显示所有图书 void showBooks() { cout << "共有" << books.size() << "本图书" << endl; for (int i = 0; i < books.size(); i++) { cout << "名称:" << books[i].name << endl; cout << "作者:" << books[i].author << endl; cout << "数量:" << books[i].num << endl; cout << endl; } } int main() { // 添加几本图书 addBook("C++程序设计", "谭浩强", 10); addBook("C++高级编程", "侯捷", 5); addBook("Java核心技术", "Cay S. Horstmann, Gary Cornell", 3); // 按名称排序 sort(books.begin(), books.end(), cmpByName); // 显示所有图书 showBooks(); // 根据名称和作者搜索图书 searchBook("C++程序设计", "谭浩强"); return 0; } ``` 该示例代码可以实现添加图书、按名称排序、按作者排序、搜索图书等功能。其中用vector存储图书信息,用map存储关键字索引。 当添加图书时,将图书信息存储到vector中,并且更新关键字索引。当搜索图书时,根据名称和作者的关键字搜索图书,并且输出搜索结果。当按名称排序或按作者排序时,使用algorithm库中的sort函数来实现排序功能。当显示所有图书时,遍历vector中的所有图书信息,并且输出每本图书的名称、作者和数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

沐川҉ ღ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值