STL

STL

vector:可变长数组

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int>v1;//定义一维
	vector<vector<int> >v2;//定义二维
	vector<int>v22[100];//一维已定,二维可变
	vector<int>::iterator it;//通过迭代器访问
	//v[i] 0-v.size()-1; 通过下标访问 v[i]=*(v.begin()+i)=*(it+i);
	//push_back()添加
	//pop_back()删除
	//clear()清空
	//insert(it,x)在it处插入元素x
	//erase(it)删除it处的元素,erase[first,last);
}

set:内部自动有序且不含重复元素的容器

#include <iostream>
#include <set>
using namespace std;
int main()
{
	set<int>st;//定义一维
	set<set<int> >st2;//定义二维
	set<int>st22[100];//一维已定,二维可变
	set<int>::iterator it;//只能通过迭代器访问,只有vector和string可用*(it+i)的访问方式
	//for(set<int>::iterator it=st.begin();it!=st.end();it++)
	//printf("%d",*it);
	//insert(x)添加
	//find(x) 返回迭代器
	//clear()清空
	//size()
	//erase(it)删除it处的元素,erase(value),erase[first,last);
}

string:字符串类型

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str = "hello-world";//定义并初始化
	//通过下标访问
	for (int i = 0; i < str.length(); i++)
	{
		printf("%c", str[i]);
	}
	cout << "\n";
	//读入输出只能用cin和cout
	string str1;
	cin >> str1;
	cout << str1<<endl;
	string str2;
	//若在string中读入一整行需要用getline(cin,str2);
	getline(cin, str2);
	cout << str2<<endl;
	printf("%s", str2.c_str());
	char str3[100];
	//若不在string容器中入如下
	cin.getline(str3, 100);
	cout << str3<<endl;
	printf("%s\n", str3);
	//通过迭代器访问
	for (string::iterator it = str.begin(); it != str.end(); it++)
	{
		printf("%c", *it);
	}
	printf("\n");
	//常用函数
	//1.字符串拼接
	string a = "hello";
	string b = "world";
	string c = a + b;
	a += b;
	cout << c << endl;
	cout << a << endl;
	//2.按字典序比较
	//3.length(),size()
	//4.insert(pos,string) 在pos位置插入string
	b.insert(0, a);
	cout << b << endl;
	//insert(it,it2,it3)在原字符串it处插入(it2,it3)处的字符串
	a.insert(a.begin(), b.begin(), b.end());
	cout << a << endl;
	//erase(it),erase(first,last),erase(pos,length)
	a.erase(0, 3);//从0开始删除长度为3的字符串
	cout << a << endl;
	//clear();
	//substr(pos,len);
	cout<<a.substr(0, 3)<<endl;//返回从0开始长度为3的字符串
	//string::npos 是一个常数为find()失败返回的值
	//find();str.find(str1);若str1在str中返回在str1中第一次出现的位置,若不是返回string::npos;
	//str.find(str2,pos) 从str的pos位开始匹配str2
	string aa = "thank you very much!";
	string bb = "you";
	string cc = "weqw";
	if (aa.find(bb) != string::npos)
		cout << bb << endl;
	if (aa.find(bb, 7) != string::npos)
		cout << bb << endl;
	if (aa.find(cc) != string::npos)
		cout << cc << endl;
	else cout << "no position" << endl;
	//str.replace(pos,len,str2) 把str从pos号位开始,长度为len的字串代替为str2;
	//str.replace(it1,it2,str2)
	string aaa = "maybe you will not turn arround.";
	string bbb = "will not";
	string ccc = "Surely";
	cout << aaa.replace(10, 4, bbb) << endl;
	cout << aaa.replace(aaa.begin(), aaa.begin() + 5, ccc);
	system("pause");
	return 0;
}

map:映射

#include <iostream>
#include <map>;
#include <set>
using namespace std;
int main()
{
	//map的定义
	//map<typename1,typename2>mp;
	map<int, char>ic;
	map<string, int>si;
	map<set<int>, string>sit;
	//通过下标访问
	ic[1] = 'c';
	printf("%c", ic[1]);
	//通过迭代器访问.it->first 是当前映射的键,it->second是当前映射的值,map会以键值从小到大的顺序自动排序
	si["nihao"] = 1;
	si["weqwe"] = 2;
	si["erre3"] = 0;
	map<string, int>::iterator it = si.begin();
	for (; it != si.end(); it++)
	{
		printf("%s %d", it->first, it->second);
	}
	//find(key);
	//erase(it);erase(key);erase[first,last)
}

队列

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	queue<int>q;
	//常用函数
	//入队q.push() 出队q.pop();
	//q.front()队首元素
	//q.back()队尾元素
	//q.empty() 若为空返回true,否则返回false
	//q.size()
}

stack

#include <iostream>
#include <stack>
using namespace std;
int main()
{
	stack<int>s;
	//常用函数
	//s.pop();s.push();s.top();s.empty();s.size();
}

algorithm

函数作用
max(a,b)返回最大值
min(a,b)返回最小值
abs(int,int)求绝对值
swap(x,y)交换
reverse(a,a+4)对数组某范围进行反转
reverse(it1,it2)对string字符串进行反转
next_permutation(a,a+3)给出一个序列在全排列中的下一个序列
fill(a,a+5,233)给数组赋值
sort(a,a+3,cmp)排序
lower_bound(a,a+10,-1)在数组[a,a+10)范围内返回第一个大于-1的位置
upper_bound(a,a+10,-1)在数组[a,a+10)范围内返回第一个大于等于-1的位置

next_permutation()

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10]={1,2,3};
do{
printf("%d%d%d\n",a[0],a[1],a[2]);
}while(next_permutation(a,a+3));
return 0;
}

lower_bound()&upper_bound()

int *lowerpos=lower_bound(a,a+10,-1);
int *upperpos=upper_bound(a,a+10,-1);
printf("%d,%d\n",lowerpos-a,upperpos-a);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值