常用STL

vector

#include <iostream>
#include <vector>

using namespace std;

int main(){
	vector<int> whq = {1, 2, 3};
	cout << whq[1] << endl; // 2  支持随机访问
	whq.push_back(4);
	cout << whq.size() << endl; // 1 2 3 4 共4个
	whq.pop_back();
	cout << whq.size() << endl; // 1 2 3 共3个
	
	cout << whq.empty() << endl; //false-> 0
	whq.clear();
	cout << whq.empty() << endl << endl; // true->1
	
	// vector遍历
	whq = {1, 2, 3};
	for (int i = 0; i < whq.size(); i ++)
        cout << whq[i] << endl; // 1 2 3
    // 采用迭代器
    for (vector<int>::iterator it = whq.begin(); it != whq.end(); it ++)
        cout << *it << endl; // 1 2 3
	
	cout << whq.front() << endl; // 1
	cout << whq.back() << endl; // 3
	

	
	return 0;
}
一维vector
// 3种等价写法:赋具体值
vector<int> a = {1, 2, 3};
vector<int> a({1, 2, 3});
vector<int> a{1, 2, 3};
// 定义shape与初始值
vector<int> a(10, 0);
定义二维vector, 如n*m的vector。
// n*m 的二维vector
vector<vector<int>> a(n, vector<int>(m));
// 初始化全0的vector:
vector<vector<int>> a(n, vector<int>(m, 0));

bool:
vector<vector<bool>> st(n, vector<bool>(m));
// 定义一些变量,,例如全局变量,但此时未知n等大小
vector<vector<bool>> st;
// ............................
// 赋值
st = vector<vector<bool>>(n, vector<bool>(n, false));

queue

#include <iostream>
#include <queue> // 包括queue与priority_queue

using namespace std;

int main(){
	struct Rec{
		int a, b, c;
		bool operator> (const Rec& t) const {// 有sort则必须运算符重载,具体:小根堆重载>,大根堆重载<
			return a > t.a;
		}
	};
	
	queue<int> q;
	q.push(1);
	q.pop();
	cout << q.front() << endl; // 0
	cout << q.back() << endl; // 1
	cout << "   " << endl;
	
	
	priority_queue<int> a; // 默认为大根堆 
	// 小根堆
	priority_queue<int, vector<int>, greater<int>> b; 
	// 此处为结构体小根堆,需要手动重载>;若为大根堆则重载< 
	priority_queue<Rec, vector<Rec>, greater<Rec>> d; 
	d.push({3, 2}); // 3 < 100
	d.push({100, 2});
	
	cout << d.top().a << endl; // 3 查看堆顶元素
	cout << d.top().b << endl; // 2
	cout << d.top().c << endl; // 0 未赋值则默认为0
	
	cout << d.empty() << endl; // false->0
	cout << d.size() << endl; // 2
	cout << "   " << endl;
	
	priority_queue<pair<int, int>> e;
	e.push({10, 2});
	e.push({3, 2});
	cout << e.top().first << endl; // 10
	
	return 0;
}

map

内部实现为红黑树

#include <iostream>
#include <map>

using namespace std;

int main(){
		
	map<string, vector<int>> m;
	// 两种方法
	m.insert({"whq", vector<int>()}); 
	
	m["whq"] = vector<int>({1, 2, 3, 4, 5});
	
	cout << m["whq"][2] << endl; // 3
	cout << (m.find("whq") == m.end()) << endl << endl; // false->0
	
		
	return 0;
}

bitset

#include <iostream>
#include <bitset>

using namespace std;

int main(){
	bitset<1000> bb;
	bitset<1000> cc;
	bitset<1000> dd;
	bb[0] = 1;
	bb[2] = 1; // bb.set(2); reset则设为0
	cc[1] = 1;
	cout << (bb | cc).count() << endl; // 3 返回1的个数为3
	cout << bb[0] << " " << bb[1] << endl; // 1 0
	cout << bb.count() << endl; // 2
	cout << sizeof(bb) << endl; // 128
	
	return 0;
}

pair

#include <iostream>

using namespace std;

int main(){
	pair<int, string> a;
	a = {2018, "whq"};
	cout << a.first << " " << a.second << endl; // 2018 whq
	
	a = make_pair(1, "hello");
	cout << a.first << ' ' << a.second << endl; // 1 hello
	
	return 0;
}
C++竞赛中,常用STL(Standard Template Library)组件有vector、stack以及算法函数next_permutation。vector是一个动态数组容器,可以方便地进行元素的插入、删除和访问。stack是一个后进先出(LIFO)的容器,常用于实现递归、深度优先搜索等算法。next_permutation是一个算法函数,用于按照字典序生成某个序列的所有排列。 在竞赛中,我们可以利用vector来存储元素,使用push_back函数添加元素,使用[]操作符或迭代器进行元素的访问。可以使用stack来模拟递归过程,实现一些深度优先搜索的问题。而使用next_permutation函数可以方便地生成某个序列的所有排列。 举个例子,如果我们想要按照字典序输出1到n的全排列,可以使用next_permutation函数结合vector来实现。首先,我们可以使用一个for循环将1到n的元素添加到vector中。然后,使用do-while循环来不断调用next_permutation函数,每次生成下一个排列并输出。最后,当next_permutation函数返回false时,表示已经生成了所有的排列,循环结束。 另外,如果我们需要使用栈来解决一些问题,可以使用stack容器。可以使用push函数将元素入栈,使用pop函数将元素出栈。栈的特点是后进先出,所以可以用来模拟递归过程或实现一些需要回溯的算法。 总结起来,在C++竞赛中,常用STL组件有vector、stack和算法函数next_permutation。vector可以方便地进行元素的插入、删除和访问,stack可以模拟递归过程,而next_permutation函数可以生成某个序列的所有排列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值