位容器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加速程序,