Multiset
#include#include using namespace std; void mainA() { multiset 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#include
Bitset
#include#include #include #include 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" < 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#include #include 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()< 算法函數蘭不達表達式以及類重載
#include#include #include #include using namespace std; template //模板類,實現對於某些容器元素的操作 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 myv; myv.push_back(10); myv.push_back(9); myv.push_back(7); myv.push_back(9); add addA;//省略, //for_each(myv.begin(), myv.end(), addA); //for_each(myv.begin(), myv.end(), add ()); //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 //gpu計算 #includeusing namespace concurrency; using namespace std; void main() { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; array_view 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#include #include //操作系統的底層文件,測試時間 #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 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 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加速程序,