C++程序设计比赛常用函数总结

目录

String

set

map 

全排列函数

queue

priority_queue

vector

​编辑stack 

类型转换


String

#include 
#include
#include
#include
using namespace std;

int main()
{
    string str = "abcde";
    cout << "翻转前:" << str << endl;
    reverse(str.begin(), str.end());//字符串翻转
    cout << "翻转后:" << str << endl << endl;

    str = "abcde";
    cout << "当前字符串:" << str<

C++程序设计比赛常用函数总结_第1张图片 

set

#include 
#include
#include
using namespace std;

int main()
{
    set st;
    st.insert(3);
    st.insert(5);
    st.insert(2);
    st.insert(3);
    st.insert(4);//自动排序

    cout << "set容器的大小:" << st.size() << endl << endl;
    //查找某个值,如果找到了则返回指向该元素的迭代器;
    //时间复杂度为O(logN)
    //如果没有找到则返回指向容器末尾的迭代器,即st.end().
    //这是一个表示无效位置的迭代器,指示元素不存在于集合中。
    set::iterator ite = st.find(6);
    if (ite == st.end()) cout << "没有找到" << endl;
    else cout << "找到了,值为:" << *ite << endl;

    ite = st.find(2);
    if (ite == st.end()) cout << "没有找到" << endl;
    else cout << "找到了,值为:" << *ite << endl;
    cout << endl;

    cout << "迭代器的方式遍历结果为:";
    for (ite = st.begin(); ite != st.end(); ite++)
    {
        cout << *ite << " ";
    }
    cout << endl;

    st.erase(2);//按值删除
    cout << "迭代器的方式遍历结果为:";
    for (ite = st.begin(); ite != st.end(); ite++)
    {
        cout << *ite << " ";
    }
    cout << endl;
    //按区间删除
    st.erase(st.find(4), st.end());
    cout << "迭代器的方式遍历结果为:";
    for (ite = st.begin(); ite != st.end(); ite++)
    {
        cout << *ite << " ";
    }
    cout << endl;
    st.clear();//清空所有元素
    cout << st.size() << endl;
    return 0;
}

C++程序设计比赛常用函数总结_第2张图片

map 

# include 
# include 
#include
using namespace std;
int main()
{
	map mp;
	mp[1] = "zdy";
	mp[2] = "wt";
	mp[3] = "lhb";
	mp[4] = "wzy";

	map::iterator  ite;
	ite = mp.find(1);
	if (ite != mp.end())
		cout << "find!" << ite->second << endl;
	else
		cout << "not find!"<second << " ";
	cout << endl;
	ite = mp.find(1);
	mp.erase(ite);
	for (ite = mp.begin(); ite != mp.end(); ite++) cout << ite->second << " ";
	cout << endl;
	mp.erase(mp.find(2), mp.find(4));
	for (ite = mp.begin(); ite != mp.end(); ite++) cout << ite->second << " ";
	cout << endl;
	if (!mp.empty()) cout << mp.size() << endl;
	else cout << "为空!" << endl;
	return 0;
}

C++程序设计比赛常用函数总结_第3张图片

全排列函数

做题时,有时候会碰到需要列举一些数字所有排列方式的情况。这时候大家恐怕会无从下手,但如果使用next_permutation函数,就简单了。next_permutation函数在头文件中,作用是是生成给定序列的下一个较大排序,直到序列按降序排列为止。到这里还需要强调的一点是,如果你希望生成所有的排列方式,一定要先将序列按升序排列,这里可以与sort函数结合起来使用,先用sort升序排列,再调用next_permutation函数。

# include 
# include 
using namespace std;
int main()
{
	int a[] = { 3,1,2 };
	sort(a, a + 3); //默认升序排列
	do {
		for (int i = 0; i < 3; i++) {
			cout << a[i] << " ";
		}
		cout << endl;
	} while (next_permutation(a, a + 3)); //第一个参数对应数组首,第二个参数对应数组尾
	return 0;
}

C++程序设计比赛常用函数总结_第4张图片

queue

# include 
# include 
#include
using namespace std;
int main()
{
	queue que;
	que.push(1);//尾插
	que.push(2);
	que.push(3);
	cout << "队头元素:" << que.front() << endl;
	cout << "队尾元素:" << que.back() << endl;

	que.pop();
	cout << "删除队头后的队头:" << que.front() << endl;

	cout << "队列的大小:" << que.size() << endl;

	if (!que.empty()) cout << "不为空!" << endl;
	else cout << "为空!" << endl;
	//队列不提供迭代器遍历
	while (!que.empty())
	{
		cout << que.front() << " ";
		que.pop();
	}
	return 0;
}

C++程序设计比赛常用函数总结_第5张图片

priority_queue

# include 
# include 
#include
using namespace std;
int main()
{
	priority_queue, greater >que;
	que.push(3);
	que.push(1);
	que.push(2);
	while (!que.empty())
	{
		cout <, less >que1;
	que1.push(3);
	que1.push(1);
	que1.push(2);
	while (!que1.empty())
	{
		cout << que1.top() << " ";
		que1.pop();
	}
	return 0;
}

C++程序设计比赛常用函数总结_第6张图片

vector

# include 
# include 
#include
using namespace std;
int main()
{
	vector vec = {5,6};
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);
	vec.push_back(0);
	//迭代器遍历
	cout << "迭代器遍历结果:";
	for (vector::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " " ;
	cout<< endl;
	//下标遍历
	cout << "下标遍历结果:" ;
	for (int i = 0;i::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
	cout << endl;
	reverse(vec.begin(), vec.end());
	cout << "翻转结果:";
	for (vector::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
	cout << endl;

	vec.insert(vec.begin(), 9);
	vec.insert(vec.begin()+3,2, 9);
	cout << "插入结果为:";
	for (vector::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
	cout << endl;

	vec.erase(vec.begin(), vec.begin() + 3);
	cout << "删除结果为:";
	for (vector::iterator ite = vec.begin(); ite != vec.end(); ite++) cout << *ite << " ";
	cout << endl;
	return 0;
}

C++程序设计比赛常用函数总结_第7张图片stack 

# include 
# include 
#include
using namespace std;
int main()
{
	stack a;
	a.push(1);
	a.push(2);
	a.push(3);
	cout << a.size() << endl;
	while (!a.empty()) {
		cout << a.top() << " ";
		a.pop();
	}
	return 0;
}

C++程序设计比赛常用函数总结_第8张图片

类型转换

# include 
# include 
#include
using namespace std;
int main()
{
	int a = 1;
	cout << typeid(a).name() << endl;
	string str = to_string(a);
	cout << str << endl;
	cout << typeid(stoi("2121")).name() << endl;
	cout << typeid(stol("2121")).name() << endl;
	cout << typeid(stoll("2121")).name() << endl;
	cout << typeid(stof("2121.2")).name() << endl;
	cout << typeid(stod("2121.2")).name() << endl;
	return 0;
}

C++程序设计比赛常用函数总结_第9张图片 

你可能感兴趣的:(我的随笔,c++,开发语言,算法)