程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 【C/C++學院】0830

【C/C++學院】0830

編輯:關於C++

 

蘭不達表達式

#include
#include
#include//算法  	lambda表達式,不僅僅適用與array ,也適用於vector

void main1()
{
	std::vector 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 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 myvetor1;
	myvetor1.push_back(12);
	myvetor1.push_back(13);
	myvetor1.push_back(14);

	std::vector myvetor2;
	myvetor2.push_back(22);

	std::vector myvetor3;
	myvetor3.push_back(32);
	myvetor3.push_back(37);

	std::vector> 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 
#include 
#include 
#include 
#include 
#include 

using namespace std;

void main1()
{
	vector 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 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 show
{
public:
	void  operator ()(T &t)
	{
		cout << t << "  ";
	}
};

void main3()
{
	list   list1;
	vector  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());
	cout << "\n";
	for_each(v1.begin(), v1.end(), show());

	cin.get();
}

Multiset

void main4()
{
	multiset 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 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 show
{
public:
	void  operator ()(T &t)
	{
		cout << t << "  ";
	}
};
//打亂數據
void  main()
{
	vector v1;
	for (int i = 0; i < 10;i++)
	{
		v1.push_back(i);
	}

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

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

	cin.get();
}

旋轉數據

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

	//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());


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

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

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

	cin.get();
}

排序並顯示步驟

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

局部排序

void  main10()
{
	vectorone;
	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());
	cout << "\n";
	for_each(one.begin(), one.end(), show());

	cin.get();
}

取出分數最低的2個學生

#include 
#include 
#include 
#include 
#include 
#include 

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 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();
}





 

 

 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved