STL使用技巧

Vector

头文件:# include <iostream>

定义:

1.vector <int> a

2.vector <int> a(10) 定义长度为10的 vector

3.vector <int> a(10,3) 定义长度为10的 vector,并且每个数都是3

4.vector <int> a[10] 定义了10个vector

其中对3.有

# include <iostream>
# include <vector>
using namespace std;
int main()
{
	vector<int>a(10, 3);
	for (auto x : a)
		cout << x << " ";
		cout << endl;
	return 0;
}

结果:

支持的函数:

a.size() :求元素个数

a.empty() :判断是否为空

a.clear() :清空 

# include <iostream>
# include <vector>
using namespace std;
int main()
{
	vector<int>a(10, 3);
	auto x = a.size();
	a.clear();
	auto y = a.empty();
	cout << x<<" "<<y<<endl;
	return 0;
}

结果:

 a.front() :返回a的第一个数

a.back() :返回a的最后一个数

push_back :向a的最后插入一个数

pop_back :删掉a的最后一个数

a.begin() : a的第0个数;

a.end() : a的最后一个数后面的数;

# include <iostream>
# include <vector>
using namespace std;
int main()
{
	vector<int>a;
	for (int i = 0; i < 10; i++)
		a.push_back(i);
	for(int i=0;i<10;i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
	for(auto i=a.begin();i!=a.end();i++)
	{
		cout << *i << " ";
	}
	cout << endl;
	for(auto i:a)
	{
		cout << i << " ";
	}
	cout << endl;
	cout << a.front() << endl;
	cout << a.back() << endl;
	a.pop_back();
	for (auto i : a)
	{
		cout << i << " ";
	}
	return 0;
}

结果: 

支持比较运算:(字典序)

# include <iostream>
# include <vector>
using namespace std;
int main()
{
	vector<int> a(4,3);
	vector<int> b(3,4);
	if(a<b)
	{
		cout << "a<b" << endl;
	}
	return 0;
}

结果:

 Pair

pair <int ,string>  p;

初始化:

         p=make_pair(10,"abc")

         p={10,"abc"};

first 第一个元素

second 第二个元素

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

# include <iostream>
# include <string>
using namespace std;
int main()
{
	pair <int ,string>p;
	p ={ 10,"abc" };
	cout << p.first << " " << p.second << endl;
	return 0;
}

结果:

string 

       size() /length():字符个数

       empty() :判断是否为空

       clear() :清空字符串

       substr(k,n) 下标是k后面的n个字符

# include <iostream>
# include <string>
using namespace std;
int main()
{
	string a={"abc"};
	a += "de";
	a += "f";
	cout << a << endl;
	cout << a.substr(1, 3) << endl;//下标是1后面的三个字符
	cout << a.substr(1, 10) << endl;
	cout << a.substr(1) << endl;
	printf("%s\n", a.c_str());
	return 0;
}

queue 

 头文件:# include <queue>

函数:

       size()

       empty()

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

       front() :返回队头元素

       back() :返回队尾元素

       pop() :弹出队头元素

# include <iostream>
# include <queue>
using namespace std;
int main()
{
	queue<int>a;
	a.push(1);
	a.push(2);
	a.push(3);
	cout << a.front() << " ";
	cout << a.back() << " ";
	cout << endl;
	a.pop();
	cout << a.front() << " ";
	cout << a.back() << " ";
	return 0;
}

结果:

 priority_queue 

头文件:include <queue>

 优先队列,默认是大根堆

         push() :插入元素

         top() :返回堆顶元素

         pop() :弹出堆顶元素 

         小根堆定义方法: priority_queue< int , vector<int> , greater<int> >heap;

大根堆: 

# include <iostream>
# include <queue>
using namespace std;
int main()
{
	priority_queue<int>p;
	for(int i=1;i<10;i++)
	{
		p.push(i);
	}
	cout << p.top() << endl;
	cout << p.size() << endl;
	p.pop();
	p.pop();
	cout << p.size() << endl;
	return 0;
}

结果:

小根堆:

# include <iostream>
# include <queue>
# include <vector>
# include <functional>
using namespace std;
int main()
{
	priority_queue< int, vector<int>, greater<int> >p;
	for(int i=1;i<10;i++)
	{
		p.push(i);
	}
	cout << p.top() << endl;
	cout << p.size() << endl;
	p.pop();
	p.pop();
	cout << p.size() << endl;
	return 0;
}

结果:

deque 

头文件: #include <deque>

定义:

 deque <int> p

 deque <int> p(10) 定义长度为10的deque

 deque <int> p(10,3)  定义长度为10的deque,且每个数都为3

# include <iostream>
# include <deque>
using namespace std;
int main()
{
	deque<int> p(10, 3);
	for(auto a:p)
	{
		cout << a << " ";
	}
	return 0;
}

结果:

         size() 

         empty()

         clear()

         front() :返回队首元素

         back() :返回队尾元素

         push_back() :向队尾插入元素

         pop_back() :从队尾弹出元素

         push_front() :向队首插入元素

         pop_front() :从队首弹出元素

# include <iostream>
# include <deque>
using namespace std;
int main()
{
	deque<int> p(4, 3);
	p.push_back(6);
	p.push_front(5);
	for(int i=0;i<p.size();i++)
	{
		cout << p[i]<<" ";
	}
	cout << endl;
	cout << p.front() <<" "<<p.back() << endl;
	p.clear();
	cout << p.empty() << endl;
	return 0;
}

结果:

 

 set, map, multiset, multimap

         size()

         empty()

         clear()

         begin()/end()   ++,--

set/multiset

         insert() :插入一个数

         find () :查找一个数

         count () :返回某个数的个数

erase() :

        (1)输入是一个数x,删除所有x

        (2)输入一个迭代器,删除这个迭代器

lower_bound()/upper_bound()

        lower_bonud(x) :返回大于等于x的最小的数的迭代器

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

map/multimap

        intsert() :插入的数是一个pair

        erase() :输入的参数是pair或迭代器

        find() 

        lower_bound()/upper_bound()

unordered_set,  unordered_map, unordered_multiset, unordered_multimap

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

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

bitset

        bitset<10000> s;

                ~ ,& ,| ,^

                >> , <<

                 == , !=

       count() 返回有多少个1

       any() :判断是否至少有一个1

       none() :判断是否全为0

       set() :把所有位置成1

       set(k,v) :把第k为变成v

       reset() :把所有位变成0

       flip() :等价于取反~

       flip(k) :把第k位取反

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值