【C/C++学院】0829-位容器multimapmutisetString/算法函数兰不达表达式以及类重载/GPU编程

位容器multimapmutisetString

Multiset

#include <set>
#include <iostream>

using namespace std;

void mainA()
{
	multiset<int> myset;
	myset.insert(100);
	myset.insert(101);
	myset.insert(100);
	myset.insert(103);
	myset.insert(100);

	auto pfind = myset.find(101);
	std::cout << *pfind << std::endl;

	auto allfind = myset.equal_range(100);
	//找到红黑树的链表节点,遍历所有的元素

	//find链表的头结点,second最后一个空节点,遍历所有的元素
	for (auto it = allfind.first; it != allfind.second;it++)
	{
		cout << *it << endl;
	}

	cin.get();
}

Multimap

#include <iostream>
#include <map>
#include <string>

using namespace std;

void main2()
{
	multimap<string, string> mymap;
	mymap.insert(pair<string, string>("yincheng", "a"));
	mymap.insert(pair<string, string>("yincheng1", "b"));
	mymap.insert(pair<string, string>("yincheng", "c"));
	mymap.insert(pair<string, string>("yincheng", "d"));

	auto ib = mymap.begin();
	auto ie = mymap.end();
	for (;ib!=ie;ib++)
	{
		cout << (*ib).first << "   "<<(*ib).second << endl;
	}

	auto pfind = mymap.find("yincheng");
	cout << "\n\n\n";
	cout << (*pfind).first << "   " << (*pfind).second<< endl;
	cout << "\n\n\n";
	auto it = mymap.equal_range("yincheng");//从树节点吧关键字相同的链表全部拔下

	//first起点,,secondl链表最后的节点后面一个空节点,都是迭代器
	for (auto i = it.first; i != it.second;i++)
	{
		cout << (*i).first << "   " << (*i).second << endl;
	}

	cin.get();
	cin.get();
}

Bitset

#include <set>
#include <bitset>
#include <iostream>
#include<string>

using namespace std;

void main3X()
{
	//8 位, (215)代表构造的数据
	bitset<8>bs(215);
	for (int i = 0; i < 8;i++)//最高位存储i=7上
	{
		cout << bs[i];
	}

	cin.get();
	cin.get();
}

void main3Y()
{
	//8 位, (215)代表构造的数据
	bitset<8>bs(215);
	for (int i = 7; i >=0; i--)
	{
		cout << bs[i] << "  " << ~bs[i] << endl;
	}

	cin.get();
	cin.get();
}

void  main3Z()
{
	float num = 1231231236.8;
	bitset<32> myset(num);
	for (int i = 31; i >=0;i--)
	{
		cout << myset[i];
	}

	cin.get();
}

void  main3S()
{
	int  num =-5;
	bitset<32> myset(num);
	for (int i = 31; i >= 0; i--)
	{
		cout << myset[i];
	}
	string str = myset.to_string();
	cout <<"\n" <<str << endl;

	unsigned int data;
	data = myset.to_ulong();//补码
	cout << "\n" << data<< endl;

	cin.get();
}

void main345()
{
	bitset<8>bs(255);
	bs.set(7, 0);//操作二进制位
	bs.set(0, 0);
	cout << bs.size() << endl;//位数
	//bs.reset();//全部清零
	//bs.none();//测试下是否有越位
	for (int i = 7; i >=0; i--)//最高位存储i=7上
	{
		cout << bs[i];
	}

	cin.get();
}

String容器

#include<string>
#include <iostream>
#include <stdlib.h>

using namespace std;
//字符串初始化
void main1s()
{
	char str[124] = "china is  big";
	//str = "12321";C写法

	//string str1(str);
	//str1 = "china  is great";
	string str1("ABCDEFG");
	str1 = "china  is  china";
	std::cout << str1;

	cin.get();
}

void main2s()
{
	string str1("ABCD");
	string str2("1234");
	string str3 = str1 + str2;
	std::cout << str3;

	char stra[12]="1231";
	char strb[24]="2132";
	//char strc[36] = stra + strb;
	cin.get();
}

void main3s()
{
	string str1("ABCD");
	string str2("1234");
	str1.append(str2);
	str1 += str2;//字符串的增加
	std::cout << str1;

	cin.get();
}

void main4s()
{
	string str1("ABCD");
	string str2("1234");
	//任意位置插入字符
	str1.insert(str1.begin(),'X');
	str1.insert(str1.end(), 'X');
	str1.insert(str1.begin()+3,3, 'X');

	std::cout << str1;

	cin.get();
}

void  main5s()
{
	string str1("12345678");
	auto ib = str1.begin();
	auto ie = str1.end();
	for (;ib!=ie;ib++)
	{
		cout << *ib << endl;
	}
	//str1.erase(str1.begin());//删除一个字符
	//str1.erase(str1.begin()+3,str1.end()-2);//删除某个字符串
	str1.erase(3, 4);//c从第三个字符开始删除四个字符
	cout << str1 << endl;

	cin.get();
}

void main6s()
{
	string str1("12345678china");
	str1.replace(5, 3, "china");//从0到第三个字符替换为china
	//replace,1位置,长度,字符串

	cout << str1 << endl;

	cin.get();
}

void mainA1()
{
	string str("233锄禾日当午,谭胜把地雷买下土,谭胜来跳舞,炸成250");
	//cout << (int)str.find("谭胜大爷") << endl;
	//int pos = str.find(",");//找到第一个皮配的,不匹配返回-1,
	//int pos = str.rfind("谭胜");//找到第一个皮配的,不匹配返回-1,
	//int pos = str.find("谭胜");

	cin.get();
}

void mainA2()
{
	string str("ab123mn");
	//int pos = str.find_first_of("123");
	//find_firstof是第一个找到与字符串皮配字符位置
	//int pos = str.find_first_not_of("abc");
	//find_firstof是第一个找到与字符串不皮配字符位置

	//int pos = str.find_last_of("123");
	//find_firstof是最后一个找到与字符串皮配字符位置
	int pos = str.find_last_not_of("123");

	cout << pos << endl;

	cin.get();
}

void main1234()
{
	string str1 = "calc";
	string str2 = "ABC1";
	char strA[5] = "Asd";
	char strB[5] = "Asd";
	cout <<( str1 == str2) << endl;//重载了运算符
	cout << (strA == strB) << endl;//比较地址

	cout << str1.empty()<<endl;////是否为空
	const char *p = str1.c_str();
	system(p);

	cin.get();
}

void main()
{
	//谭胜 是 流氓
	string str("abc is abc  china is china");
	//int pos = str.find('a', 0);//字符也一样
	//std::cout << pos << endl;
	//pos = str.find('a', pos+1);
	//std::cout << pos << endl;
	int pos = str.find("abc", 0);//find从指定位置查找

	std::cout << pos << endl;
    pos = str.find("abc", pos+3);
	std::cout << pos << endl;

	cin.get();
}

算法函数兰不达表达式以及类重载

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

template<class T>  //模板类,实现对于某些容器元素的操作
class add
{
public:
	void operator()( T &t)//重载()运算符,进行操作
	{
		t *= 2;
		std::cout << t<<"\n";
	}
};

void  go(int a)
{
	a *= 2;
	std::cout << a << "\n";
}

void main()
{
	vector<int> myv;
	myv.push_back(10);
	myv.push_back(9);
	myv.push_back(7);
	myv.push_back(9);

	add<int> addA;//省略,
	//for_each(myv.begin(), myv.end(), addA);
	//for_each(myv.begin(), myv.end(), add<int>());
	//for_each(myv.begin(), myv.end(), go);

	auto fun = [](int a, int b){ return a + b; };//Lanmba表达式

	auto funA = [](int a){a *= 2; cout << a << endl; };
	cout << fun(1, 2) << endl;
	for_each(myv.begin(), myv.end(), funA);
	for_each(myv.begin(), myv.end(), [](int a){a *= 2; cout << a << endl; });

	cin.get();
}

GPU编程

比特币挖矿,经常使用gpu进行计算。

Helloworld

//win7 无法对gpu进行直接的调试
#include <amp.h>//gpu计算
#include <iostream>

using namespace concurrency;
using namespace std;

void main()
{
	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	array_view<int> av(10,a);//GPU计算结构,av存储到GPU显存,根据数组初始化

								  //=直接操作AV,(index<1>idx)操作每一个元素
					//extent每一个元素					//restrict (amp) 定位GPU执行
	parallel_for_each(av.extent, [=](index<1>idx) restrict (amp)
	{

		av[idx] += 123;
	});

	for (int i = 0; i < 10;i++)
    {
		std::cout << av[i] << endl;	 

    }

	cin.get();
}

Gpu调试,需要进行如下的设置

单点测试

#include <iostream>
#include <amp.h>
#include <WinBase.h>//操作系统的底层文件,测试时间

#define COUNT 100000

float nickName_GPU[COUNT];
float nickName_CPU[COUNT];

double rungpu(int num)		 restrict(amp)//限定了只能在GPU内部执行
{
	double temp = 0;
	for (int i = 0; i < num; i++)
	{
		temp += i;
	}

	return temp;

}
double runcpu(int num)		 restrict(cpu)	 //只能在CPU内部执行
{
	double temp = 0;
	for (int i = 0; i < num; i++)
	{
		temp += i;
	}
	return temp;

}
double runcpugpu(int num)		 restrict(amp, cpu)//并发执行
{
	double temp = 0;
	for (int i = 0; i < num; i++)
	{
		temp += i;
	}
	return temp;

}

int main()
{
	LARGE_INTEGER freq;
	LARGE_INTEGER strt;
	LARGE_INTEGER ed;//统计时间, 可以精确到毫秒
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&strt);//查询时间

	double dx[1] = { 0.0 };//数据, 一个元素的数组
	double  db = 0.0;

	concurrency::array_view<double> myview(1, dx);//转到gpu进行计算
	parallel_for_each(myview.extent,
		[=](concurrency::index<1> idx) restrict(amp)
	{
		myview[idx] += rungpu(20000000);
	});

	myview.synchronize();//显式等待GPU计算完成并将数据打回内存
	printf("%f\n", dx[0]);

	QueryPerformanceCounter(&ed);//把每一毫秒全到精确的显示出来
	printf("GPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart);
	QueryPerformanceCounter(&strt);

	printf("%f\n", runcpu(20000000));

	QueryPerformanceCounter(&ed);
	printf("CPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart);
	puts("测试结束");

	getchar();
	return 0;
}

int mainW(void)//测试并行计算
{
	LARGE_INTEGER freq;
	LARGE_INTEGER strt;
	LARGE_INTEGER ed;
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&strt);

	concurrency::array_view<float> myView(COUNT, nickName_GPU); //将数据打入显存  ,100000个元素的数组

	concurrency::parallel_for_each(myView.extent, [=](concurrency::index<1> idx) restrict(amp)
	{
		for (int i = 0; i < COUNT/10; i++)
		{
			myView[idx] = (myView[idx] + 0.1f) / 2.3f;
		}
	});

	myView.synchronize();//显式等待GPU计算完成并将数据打回内存  

	QueryPerformanceCounter(&ed);
	printf("GPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart);
	QueryPerformanceCounter(&strt);

	for (int idx = 0; idx < COUNT; idx++)
	{
		for (int i = 0; i < COUNT/10; i++)
		{
			nickName_CPU[idx] = (nickName_CPU[idx] + 0.1f) / 2.3f;
		}
	}
	QueryPerformanceCounter(&ed);
	printf("CPU耗时: %d 毫秒\r\n", (ed.QuadPart - strt.QuadPart) * 1000 / freq.QuadPart);

	for (int idx = 0; idx < COUNT; idx++)
	{
		if (nickName_CPU[idx] != nickName_GPU[idx])
		{
			puts("CPU和GPU的计算结果不相符!");
			getchar();
			return 0;
		}
	}
	puts("测试结束");

	getchar();
	return 0;
}

Cpu的频率快与gpu,适合于单点计算,但是gpu的容器比较多,适合并行计算。

Cpu优势在于单点计算。围绕一个计算器,只计算一个数,计算速度最快。

Gpu优势:并发计算。Gpu加速程序,


时间: 2024-08-01 16:02:21

【C/C++学院】0829-位容器multimapmutisetString/算法函数兰不达表达式以及类重载/GPU编程的相关文章

【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_ba

ACM STL容器和算法

1.4      STL 的组成 STL有三大核心部分:容器(Container).算法(Algorithms).迭代器(Iterator),容器适配器(container adaptor),函数对象(functor),除此之外还有STL其他标准组件.通俗的讲: 容器:装东西的东西,装水的杯子,装咸水的大海,装人的教室--STL里的容器是可容纳一些数据的模板类. 算法:就是往杯子里倒水,往大海里排污,从教室里撵人--STL里的算法,就是处理容器里面数据的方法.操作. 迭代器:往杯子里倒水的水壶,

加密 解密-从前面的36位算出后面的8位 是什么算法?或者哪些算法能够实现 回答正确必有重谢

问题描述 从前面的36位算出后面的8位 是什么算法?或者哪些算法能够实现 回答正确必有重谢 463036454543353543313833463136394538 20264351 464136304532354644423745464336324530 20264352 394338334331423942443946394438334345 20264353 333632353237313330373435333732353246 20264354 44313439304237374533

STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数

 1.STL(Standard Template Library,是用泛型技术来设计完成的实例)的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)   STL的六大组件分别是: 容器(Container) 算法(Algorithm) 迭代器(Iterator) 仿函数(Function object) 适配器(Adapter) 空间配置器(allocator):只能分配内存等   2.容器与算法 案例如下: #incl

密码-求哈希值的算法函数,来看看咯

问题描述 求哈希值的算法函数,来看看咯 由4位数字和字母组成的密码,只知道它哈希值的前10位--c2979c7124,能算出后面的数吗?密码是多少? 解决方案 http://blog.csdn.net/lyflower/article/details/2540300 解决方案二: 要问,你也得等到大赛结束之后再问啊! 解决方案三: 要问,你也得等到大赛结束之后再问啊!

PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解

数据结构很重要,算法+数据结构+文档=程序使用PHP描述冒泡排序算法,对象可以是一个数组 复制代码 代码如下: //冒泡排序(数组排序)function bubble_sort($array) { $count = count($array); if ($count <= 0) return false; for($i=0; $i<$count; $i++){ for($j=$count-1; $j>$i; $j–){ if ($array[$j] < $array[$j-1]){

c++标准库-C++标准库中,set容器的insert函数中的比较函数重写问题

问题描述 C++标准库中,set容器的insert函数中的比较函数重写问题 在**set **容器里我把它的其中的元素定义为map,然后我就不会写compare函数了.因此他的insert函数就跪了--求大神助--哭-- 解决方案 #include #include using std::string; #include using std::map; #include using std::set; #include using std::make_pair; #include using s

c-哪位知道下面这个函数的功能啊!谢谢

问题描述 哪位知道下面这个函数的功能啊!谢谢 int HOUS(double c[][MMax] double d[] double x[] double eps double &rd int m int n){/* c: c[1..n][1..m] d: d[1..n] m: the number of unknown n: the number of equations rd: ||d+cx||*/ double lk ckk beta sm; double qk[MMax] ad[MMax

给某个DIV容器赋值js函数,ajax中使用,支持ie和firefox

给某个DIV容器赋值js函数,ajax中使用,支持ie和firefox   <script> function setValueForDiv(id,content){  var element = document.getElementById(id);  element.innerHTML = unescape(content);  if(!element.innerHTML)  {    try{     element.innerHTML = unescape(content);