
C++标准模板库(STL)
文章平均质量分 66
C++提供的STL(Standard Template Library),其中封装了很多相当实用的容器
<Running Snail>
奇点将至
展开
-
C++ vector用法
vector翻译为向量,但是这里使用“变长数组”的叫法更容易理解,也即“长度根据需要而自动改变的数组”。在考试题中,有时会碰到只用普通数组会超内存的情况,这种情况使用 vector会让问题的解决便捷许多。另外, vector还可以用来以邻接表的方式储存图,这对无法使用邻接矩阵的题目(结点数太多)又害怕使用指针实现邻接表的读者是非常友好的,写法也非常简洁。定义头文件#include<vector>单独定义一个vectorvector<typename>name;原创 2021-01-13 20:11:07 · 297 阅读 · 0 评论 -
string的find( )函数✅
基本用法string的find()函数用于找出字母在字符串中的位置。find(str,position)str:是要找的元素position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素(不填第二个参数,默认从字符串的开头进行查找)返回值为目标字符的位置,当没有找到目标字符时返回npos...原创 2020-08-26 11:40:01 · 32967 阅读 · 5 评论 -
实验8.3 C++标准模板库(STL)中的双向队列类(deque)
题目使用C++标准模板库(STL)中的双向队列类(deque)重新实现上一小题。C++代码如下:#include <iostream> #include <deque> using namespace std;typedef deque<int>INTDEQUE;int main(){ INTDEQUE A; for (int i = 0; i < 5; i++) { A.push_back(i);} cout << "原创 2020-06-15 21:39:34 · 1160 阅读 · 0 评论 -
二分搜索:lower_bound 与 upper_bound 函数
基本用法:lower_bound 和upper_bound需要用在一个有序数组和容器中。(1)lower_bound (first,last,val)用来寻找在数组和容器[first,last)的范围内,第一个值大于等于val的元素的位置。如果是数组,则返回该位置的指针,如果是容器,则返回该位置的迭代器。(2)upper_bound(first,last,val)用来寻找在数组和容器[fi...原创 2020-04-27 16:27:54 · 414 阅读 · 0 评论 -
C++标准库---逆转元素reverse()&reverse_copy()
逆转元素次序reverse(beg,end)reverse_copy(sourceBeg,sourceEnd,destBeg)reverse()会将区间[beg,end)内的元素全部逆序;reverse_copy()会将源区间[sourceBeg,sourceEnd)内的元素复制到"以destBeg起始的目标区间",并在复制过程中颠倒安置次序;reverse_copy()返回目标区间内最...原创 2020-04-27 12:45:22 · 408 阅读 · 0 评论 -
一篇文章快速搞懂C++生成随机数
使用rand()函数头文件<stdlib.h>如果你只要产生随机数而不需要设定范围的话,你只要用rand()就可以了:rand()会返回一随机数值, 范围在0至RAND_MAX 间。RAND_MAX定义在stdlib.h, 其值为2147483647。#include <iostream>#include <cstdlib>using namespa...原创 2020-03-09 17:28:45 · 955 阅读 · 0 评论 -
algorithm头文件下的sort()
1.简单的sort使用必须加入头文件#include< algorithm >和using namespace std;使用如下:sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(选填));(1)对于整数#include<iostream>#include<algorithm>using namespace std;int...原创 2020-02-07 12:34:25 · 2812 阅读 · 0 评论 -
algorithm头文件下的fill()
fill()可以把数组或容器中的某一段区间赋予相同的值。和memset不同,这里可以是数组类型对应范围中的任意值。示例如下:#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){ int a[5]={1,2,3,4,5}; fil...原创 2020-02-06 17:01:21 · 499 阅读 · 0 评论 -
algorithm头文件下的next_permutation()
next_permutation给出一个序列在全排列中的下一个序列举例#include<iostream>#include<algorithm>using namespace std;int main(){ int a[10]={2,1,3}; do{ cout<<a[0]<<a[1]<<a[2]<<end...原创 2020-02-06 16:41:02 · 678 阅读 · 0 评论 -
algorithm头文件下的reverse()
数组指针在[it,it2)之间的元素反转#include<iostream>#include<algorithm>using namespace std;int main(){ int a[6]={10,11,12,13,14,15}; reverse(a,a+4); for(int i=0;i<6;i++) cout<<a[i]<...原创 2020-02-06 16:37:26 · 701 阅读 · 0 评论 -
algorithm头文件下函数整合
使用algorithm头文件,在头文件下加一行“using namespace std;”才能正常使用1.max(x,y)、min(x,y)、abs(x)分别返回最大值、最小值、绝对值注意:max,min中x,y可以是浮点型abs中的x必须为整数,浮点型用math头文件下的fabs2.swap(x,y)交换x,y的值3.reverse()reverse(it,it2)可以将数组指...原创 2020-02-06 16:33:47 · 537 阅读 · 0 评论 -
pair的访问
#include<iostream>#include<utility>#include<string>using namespace std;int main(){ pair<string,int>p;//字符串处理必须用string p.first="haha"; p.second=5; cout<<p.first&l...原创 2020-02-03 18:45:44 · 486 阅读 · 0 评论 -
map的访问
#include<iostream>#include<map>using namespace std;int main(){ map<char,int>mp; mp['c']=20; cout<<mp['c']; return 0; }原创 2020-02-03 18:34:36 · 191 阅读 · 0 评论 -
string的insert
(1)insert(pose,string)在pose位置插入字符串string#include<iostream>#include<string>using namespace std;int main(){ string str1="abcdef",str2="123"; str1.insert(3,str2);//在str1[3]处插入str2 c...原创 2020-02-02 18:28:37 · 509 阅读 · 0 评论 -
string的compare operator
原创 2020-02-02 18:14:06 · 311 阅读 · 0 评论 -
string的operate+=
不同于C语言,这里可以将两个string直接拼接#include<iostream>#include<string>using namespace std;int main(){ string str1="abc",str2="xyz",str3; str3=str1+str2;//拼接后赋值 str1+=str2;//直接拼接 cout<<...原创 2020-02-02 18:08:28 · 714 阅读 · 0 评论 -
string中内容的访问
1. 通过下标访问(1)输出整个字符串#include<iostream>#include<string>using namespace std;int main(){ string str; cin>>str; cout<<str; return 0;}(2)指定输出内容#include<iostream>...原创 2020-02-02 18:05:36 · 517 阅读 · 0 评论 -
set的erase()函数
1.删除单个元素#include<iostream>#include<set>using namespace std;int main(){ set<int>st; for(int i=1;i<=3;i++) st.insert(i); st.erase(st.find(2));//删除元素2 //此处直接st.erase(2)也可以...原创 2020-02-02 17:56:17 · 7980 阅读 · 0 评论 -
set的find()函数
时间复杂度为O(logN),N为set内元素的个数#include<iostream>#include<set>using namespace std;int main(){ set<int> st; for(int i=0;i<=3;i++) { st.insert(i); } set<int>::iterator i...原创 2020-02-02 17:46:07 · 7630 阅读 · 0 评论 -
set容器内元素的访问
set只能通过迭代器iterator访问#include<iostream>#include<set>using namespace std;int main(){ set<int>st; st.insert(3); st.insert(5); st.insert(5); st.insert(2); for(set<int>::...原创 2020-02-02 17:40:47 · 6131 阅读 · 0 评论