【C/C++学院】0830-兰不达表达式/STL算法-操作数据

兰不达表达式

#include<iostream>
#include<vector>
#include<algorithm>//算法  	lambda表达式,不仅仅适用与array ,也适用于vector

void main1()
{
	std::vector<int> myvector;
	myvector.push_back(11);
	myvector.push_back(22);
	myvector.push_back(33);
	myvector.push_back(3);
	myvector.push_back(4);
	myvector.push_back(5);
	int res=0;//结果
	//&res直接操作一个变量,res等价于返回值,x代表参数,每次充当迭代器指向的元素,大括号就是代码
	std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });
	std::cout << res;
	std::cin.get();
}

void main()
{
	std::vector<int> myvector(5);//分配5个空间,默认初始化为0

	myvector.push_back(1);//增
	myvector.push_back(11);
	myvector.push_back(111);
	myvector.push_back(1111);
	myvector.push_back(2);
	myvector.pop_back();//弹出一个元素,删除最后一个
	myvector.insert(myvector.begin() +1, 999);//插入,
	myvector.erase(myvector.begin()+5);//根据迭代器的位置
	//myvector.clear();//删除所有元素
	for (int i = 0; i < myvector.size(); i++)
	{
		if (1)
		{
			//查询,修改
		}
		std::cout << myvector.at(i) << std::endl;
	}

	system("pause");
}

void main123123()
{
	//可以实现动态无规则数组管理
	std::vector<int> myvetor1;
	myvetor1.push_back(12);
	myvetor1.push_back(13);
	myvetor1.push_back(14);

	std::vector<int> myvetor2;
	myvetor2.push_back(22);

	std::vector<int> myvetor3;
	myvetor3.push_back(32);
	myvetor3.push_back(37);

	std::vector<std::vector<int>> allvecor;
	allvecor.push_back(myvetor1);
	allvecor.push_back(myvetor2);
	allvecor.push_back(myvetor3);
	for (int i = 0; i < allvecor.size(); i++)
	{
		for (int j = 0; j < allvecor[i].size(); j++)
		{
			std::cout <<"  "<< allvecor[i][j];
		}
		std::cout << "\n";
	}
	
	std::cin.get();
}

STL算法-操作数据

Vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <string>

using namespace std;

void main1()
{
	vector<int> myv;
	myv.push_back(1);
	myv.push_back(2);
	myv.push_back(3);
	myv.push_back(11);
	myv.push_back(22);
	myv.push_back(33);

	for_each(myv.begin(), myv.end(), [](int v){ cout << v << endl; });

	auto  i = find(myv.begin(), myv.end(), 23);

	if (i==myv.end())
	{
		std::cout << "23玩失踪";
	} 
	else
	{
		std::cout <<*i;
	}
	
	cin.get();
}

void main2()
{
	vector<int> myv;
	myv.push_back(1);
	myv.push_back(2);
	myv.push_back(3);
	myv.push_back(11);
	myv.push_back(22);
	myv.push_back(33);
	for_each(myv.begin(), myv.end(), [](int v){ cout << v << endl; });
	//auto  i = find_if(myv.begin(), myv.end(), [](int v)->bool{ return (v > 4); });

	auto  i = find_if_not(myv.begin(), myv.end(), [](int v)->bool{ return (v > 4); });
	
	if (i == myv.end())
	{
		std::cout << "玩失踪";
	}
	else
	{
		std::cout << *i;
	}

	cin.get();
}

List

template <class T>
class show
{
public:
	void  operator ()(T &t)
	{
		cout << t << "  ";
	}
};

void main3()
{
	list <int >  list1;
	vector <int > v1;
	list1.push_back(121);
	list1.push_back(12);
	list1.push_back(122);
	list1.push_back(23);

	v1.push_back(121);
	v1.push_back(12);
	v1.push_back(122);
	v1.push_back(23);
	//sort(list1.begin(), list1.end());//排序

	//sort(v1.begin(), v1.end());//排序,简单的排序

	//算法依赖于数据结构(链式,线性),不同的数据结构,算法不一样
	
    fill(v1.begin() + 3, v1.end(), 3);//填充,指定位置数据进行初始化

	for_each(list1.begin(), list1.end(), show<int>());
	cout << "\n";
	for_each(v1.begin(), v1.end(), show<int>());

	cin.get();
}

Multiset 

void main4()
{
	multiset<int > myset;
	myset.insert(3);
	myset.insert(1);
	myset.insert(2);
	myset.insert(1);
	myset.insert(2);
	myset.insert(1);
	myset.insert(2);
	myset.insert(1);

	int i = 0;
	for (auto ib = myset.begin(), ie = myset.end(); ib != ie;ib++,i++)
	{

	}
	cout << i << endl;
	int num = count(myset.begin(), myset.end(), 1);//统计1有多少个节点

	cout << num<< endl;

	cin.get();
}


void  main5()
{
	multiset<int > myset;
	myset.insert(3);
	myset.insert(1);
	myset.insert(2);
	myset.insert(1);
	myset.insert(0);
	myset.insert(1);
	myset.insert(2);
	myset.insert(1);

	for_each(myset.begin(),myset.end(), show< const int>());

    auto  it=	adjacent_find(myset.begin(), myset.end());
	cout << "\n" << *it << endl;
	it++;
	cout << "\n" << *it << endl;
	it++;
	cout << "\n" << *it << endl;
	it++;
	cout << "\n" << *it << endl;
	it = adjacent_find(it, myset.end());//查找相同的数据,可以自己确定位置

	cout << "\n" << *it << endl;
	it++;
	cout << "\n" << *it << endl;	

	cin.get();
}

打乱数据,扑克牌洗牌

template <class T>
class show
{
public:
	void  operator ()(T &t)
	{
		cout << t << "  ";
	}
};
//打乱数据
void  main()
{
	vector<int> v1;
	for (int i = 0; i < 10;i++)
	{
		v1.push_back(i);
	}

	for_each(v1.begin(), v1.end(), show<int>());
	cout << "\n";

	random_shuffle(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), show<int>());
	cout << "\n";
	random_shuffle(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), show<int>());
	cout << "\n";
	random_shuffle(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), show<int>());

	cin.get();
}

旋转数据

bool isok(int num)
{
	return (num == 8);
}
//旋转数据
void  main7()
{
	vector<int> v1;
	for (int i =10; i>0; i--)
	{
		v1.push_back(i);
	}
	for_each(v1.begin(), v1.end(), show<int>());

	//partition(v1.begin(), v1.end(), isok);//服务于快速排序法的分区
	//旋转, 头,尾,中间
	rotate(v1.begin(), v1.begin()+8, v1.end());//v1.begin(), v1.begin()+8之间的数据移动到后面
	cout << "\n";
	for_each(v1.begin(), v1.end(), show<int>());


	/*
	rotate(v1.begin(), v1.begin()+1, v1.end());
	cout << "\n";
	for_each(v1.begin(), v1.end(), show<int>());

	rotate(v1.begin(), v1.begin() + 1, v1.end());
	cout << "\n";
	for_each(v1.begin(), v1.end(), show<int>());

	rotate(v1.begin(), v1.begin() + 1, v1.end());
	cout << "\n";
	for_each(v1.begin(), v1.end(), show<int>());
	*/

	cin.get();
}

排序并显示步骤

void  main8()
{
	int a[4] = { 2, 1, 3 ,10};
	do 
	{
		cout << a[0] << " " << a[1] << " " << a[2] <<a[3]<< "\n";

	} while (prev_permutation(a,a+4));//排序并显示步骤

	cin.get();
}

局部排序

void  main10()
{
	vector<char>one;
	one.push_back('B');
	one.push_back('A');
	one.push_back('C');
	one.push_back('Y');
	one.push_back('Z');
	one.push_back('X');
	for_each(one.begin(), one.end(), show<char>());
	cout << "\n";
	for_each(one.begin(), one.end(), show<char>());

	cin.get();
}

取出分数最低的2个学生

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <string>

using namespace std;

class student
{
public:	 
	string name;
	int score;
public:
	student(string str, int num) :name(str), score(num)
	{

	}
	bool operator <(const student &s1)const
	{
		return this->score < s1.score;
	}
};

//去分数最差的2个人
void  main()
{
	vector<student> ss;
	{
		student s1("银城A", 106);
		ss.push_back(s1);
	}
	{
		student s1("银城B", 101);
		ss.push_back(s1);
	}

	{
		student s1("银城C", 103);
		ss.push_back(s1);
	}

	{
		student s1("银城D", 105);
		ss.push_back(s1);
	}
	partial_sort(ss.begin(), ss.begin() + 2,ss.end());//部分排序

	for (int i = 0; i < 4;i++)
	{
		std::cout << ss[i].name << ss[i].score << "\n";
	}

	cin.get();
}








上一篇:[转帖]30分钟正则表达式指导


下一篇:LeetCode—628. 三个数的最大乘积(Maximum Product of Three Numbers)——分析及代码(Java)