1. vector
#include<cstdio>
#include<vector>
using namespace std;
int main() {
vector<int> vi;
for (int i = 0; i <= 5; i++) {
//TODO
vi.push_back(i);
}
vector<int>::iterator it = vi.begin();
for (int i = 0; i <= 5; i++) {
//下标访问
printf("%d ", vi[i]);
printf("\n");
}
for (int i = 0; i <= 5; i++) {
printf("迭代器访问:");
printf("%d \n", *(it + i)); // 输出vi[i]
}
return 0;
}
所有的容器中,只有vector和string中,才允许使用vi.begin() + 3这种迭代器加上整数的写法
vector常用的函数:
(1) push_back(x); // 在vector后面添加一个元素
(2) pop_back() ;// 在vector中删除容器的尾元素
(3) size(); // 获得vector元素的个数
(4) clear(); // 清空向量中所有元素
(5) insert(it, x); // 在vector中的任意迭代器it处插入一个元素x,时间复杂度为O(N);
(6) erase(it); //删除单个元素
(7) erase(firt, last); // 删除[first,last)内的所有元素,左闭右开,firs类似于指针
2. set
集合
#include<cstdio>
#include<set>
using namespace std;
/*
int main(){
set<int> st; // set集合容器的特点就是内部自动排序,且不含重复元素
st.insert(3);
st.insert(4);
st.insert(5);
st.insert(0);
st.insert(4);
// set集合的元素,访问时只能用迭代器来遍历, 插入元素时就只能用insert()函数
// begin()函数和end()函数
for(set<int>::iterator it =st.begin();it !=st.end();it++){
//TODO
printf("%d ",*it);
}
return 0;
}
*/
int main(){
set<int> st;
for(int i=1;i<=3;i++){
//TODO
st.insert(i);
}
set<int>::iterator it=st.find(2);
printf("%d \n",*it);
printf("%d",*(st.find(2)));
return 0;
}
erase()函数可以 加上 find()函数返回迭代器使用
常用函数:
(1) insert();
(2) find(value); // 返回set中对应值为value的迭代器it
(3) erase(it); //删除单个元素,it 是迭代器
(4) erase(firt, last); // 删除[first,last)内的所有元素,左闭右开,firs类似于指针
(5) size();
(6) clear();
3. string容器
字符串
#include<iostream>
#include<string>
using namespace std;
//int main(){
// string str;
// // 要读入和输出 整个字符串只能使用cin 和cout
// cin>> str;
// cout<< str;
// return 0;
//
// return 0;
//}
//int main(){
//
// string str1,str2,str3;
// cin>> str1>>str2;
// str3=str1+str2;
// str1+=str2;
// cout<<str1<<endl;
// cout<<str3<<endl;
//
//
//
// return 0;
}
//int main(){
// string str1="aa",str2="aaaa",str3="abc",str4="xyaf";
// if(str1<str2){
// printf("ok1\n"); //
// }
//
//
// return 0;
//}
int main(){
string str="Maybe you will turn around";
string str2="will not";
string str3 ="surely";
cout<<str.replace(10,4,str2)<<endl;
cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;
return 0;
}
常用函数:
(1) operator + ; 将两个string拼接起来
(2) compare operator == 、 != 、<、 <= 、> 、>= ; 比较规则是字典序
(3) length()/size();
(4) insert(pos,string);
(5) insert(pos,it2,it3);
(6) erase();
(7) erase(first,last);
(8) erase(pos,length);
(9) clear();
(10) substr(pos,len);
(11) find();
(12)replace(pos,len,str2);
(13)replace(it1,it2,str2);
4.map常用解法
映射
map中的键是唯一的
#include<stdio.h>
#include<map>
using namespace std;
/*
int main(){
map<char,int> mp;
mp['c']=20;
mp['c']=30;
printf("%d\n",mp['c']);
return 0;
}*/
int main() {
map<char, int> mp;
mp['m'] = 20;
mp['r'] = 30;
mp['a'] = 40;
// for(map <char,int>:: iterator it=mp.begin();it!=mp.end();it++){
// //TODO
// printf("%c %d\n",it->first,it->second);// 可以使用 it->first访问键,it->second访问值
// }
map<char, int >::iterator it = mp.find('m');
printf("%c %d", it->first, it->second);
return 0;
}
map 会以键(字典序)从小到大顺序自动排序,即按a<m<r的顺序排列这三对映射,因为map内部是由红黑树实现的(set集合也是),在建立映射的过程中会自动实现从小到大的排序
常用函数:
(1) find(key); // 返回key映射的迭代器
(2) erase(it); // it为需要删除的元素的迭代器
(3) erase(first,last); // 删除一个区间的元素
(4) size();
(5) clear();
5.queue容器
队列
#include<stdio.h>
#include<queue>
using namespace std;
int main(){
queue<int> q;
for(int i=0;i<=5;i++){
//TODO
q.push(i); // 依次压入栈
}
printf("%d %d",q.front(),q.back());
return 0;
}
在使用front()和pop()函数之前,必须要用empty()判断队列是否为空,否则可能因为队空而出现错误
常用函数:
(1) push(x); //将x入队
(2) front() 、 back() ; // 分别获取队首元素和队尾元素
(3) empty(); // 检查是否为空,返回true则为空,返回false不为空
(4) size();
(5) pop(); // 令队首元素出栈
6.优先队列
略
7.stack
栈,是STL容器中实现后进先出的容器
#include<cstdio>
#include<stack>
using namespace std;
int main() {
stack<int> st;
for (int i = 1; i <= 5; i++) {
//TODO
st.push(i); // push(i)用以把i压入栈
}
printf("%d", st.top());
return 0;
}
常用函数:
(1) push(x); // 将x入栈
(2) pop(); // 弹出栈顶元素
(3) top(); // 获得栈顶元素,只有这一种方法来访问栈顶元素
(4) empty(); // 判断是否为空
(5) size();
8.pair
是一个很实用的 小玩意
当想要将两个元素绑定在一起作为一个合成元素、又不想因此定义结构体时,可以使用pair.
pair实际上是一个内部有两个元素的结构体
struct pair{
typeName1 first;
typeName2 second;
}
头文件
#include <utility>
加上using namespace std
#include<iostream>
#include<utility>
#include<map>
#include<string>
using namespace std;
int main(){
pair<string, int> p;
p.first="haha";
p.second=4;
cout<<"初始元素:"<<p.first<< ""<<p.second<<endl;;
p=make_pair("xixi",55); // 构建pair的方法一
cout<<"方法一"<<p.first<<" "<<p.second<<endl;
p=pair<string,int>("heihei",555); // 构建pair的方法二
cout<<"方二一"<<p.first<<" "<<p.second<<endl;
return 0;
}
/*
int main() {
pair<int, int > p1(5, 10);
pair<int, int > p2(5, 15);
pair<int, int > p3(15, 5);
if(p1<p3){
//TODO
printf("p1<p3\n");
}
if(p1<=p3){
//TODO
printf("p1<=p3\n");
}
if(p1<p2){
//TODO
printf("p1<p2\n");
}
}
*/
常用函数:
(1) 比较操作数 == ! = < <= > >= ;比较规则是先以first的大小作为标准,只有当first相等时才判别second的大小
``
pair常见用途:
(1)用来代替二元结构体及其构造函数,可以节省编码时间
(2)作为map的键值对来进行插入
int main() {
map<string, int> mp;
mp.insert(make_pair("heihie", 5)); // pair作为map的键值对来进行插入
mp.insert(pair<string, int>("haha", 123));
for (map<string, int> :: iterator it = mp.begin(); it != mp.end(); it++) {
cout << it->first << " " << it->second << endl;
//TODO
}
return 0;
}
9.algorithm
略