[C/C ++ school] 0829-bit container multimapmutisetString/algorithm function randend expressions and Class Heavy Load/GPU Programming
Bit container multimapmutisetString
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. performance_range (100); // find the linked list node of the red/black tree, traverse all elements // find the first node of the linked list, and the last empty node of second, traverse all elements for (auto it = allfind. first; it! = Allfind. second; it ++) {cout <* it <endl;} cin. get ();}
Multimap
# Include
# Include
# Include
Using namespace std; void main2 () {multimap
Mymap; mymap. insert (pair
("Yincheng", "a"); mymap. insert (pair
("Yincheng1", "B"); mymap. insert (pair
("Yincheng", "c"); mymap. insert (pair
("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"; cout <(* pfind ). first <"" <(* pfind ). second <endl; cout <"\ n"; auto it = mymap. performance_range ("yincheng"); // unplug all linked lists with the same keywords from the tree node bar. // The first start point is an empty node behind the last node of the secondl linked list, all are iterators for (auto I = it. first; I! = It. second; I ++) {cout <(* I ). first <"" <(* I ). second <endl;} cin. get (); cin. get ();}
Bitset
# Include
# Include
# Include
# Include
Using namespace std; void main3X () {// 8-bit, (215) indicates the constructed data bitset <8> bs (215); for (int I = 0; I <8; I ++) // maximum bit storage I = 7 {cout <bs [I];} cin. get (); cin. get ();} void main3Y () {// 8 bits, (215) indicates the constructed data 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); // The binary bits bs. set (0, 0); cout <bs. size () <endl; // number of digits // bs. reset (); // All are cleared // bs. none (); // test whether there is a offside for (int I = 7; I> = 0; I --) // maximum bit storage I = 7 on {cout <bs [I];} cin. get ();}
String container
# Include
# Include
# Include
Using namespace std; // string initialization 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 ma In3s () {string str1 ("ABCD"); string str2 ("1234"); str1.append (str2); str1 + = str2; // adds the std :: cout <str1; cin. get () ;}void main4s () {string str1 ("ABCD"); string str2 ("1234"); // insert 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 (); // delete a character // str1.erase (str1.begin () + 3, str1.end ()-2); // delete a string str1.erase (3, 4 ); // c removes the four cout <str1 <endl; cin. get () ;}void main6s () {string str1 ("12345678 china"); str1.replace (5, 3, "china "); // replace the string from 0 to the third with china // replace, 1 position, length, and cout <str1 <endl; cin. get ();} void mainA1 () {string str ("233 day, Tan Sheng bought the mine, Tan shenglai danced, and burst into 250 "); // cout <(int) str. find ("Tan shengye") <endl; // int pos = str. find (","); // find the first skin configuration. If it does not match,-1 is returned, // int pos = str. rfind ("Tan Sheng"); // if the first skin is found and does not match,-1 is returned, // int pos = str. find ("Tan Sheng"); cin. get ();} void mainA2 () {string str ("ab123mn"); // int pos = str. find_first_of ("123"); // find_firstof is the first character location to be matched with the string skin. // int pos = str. find_first_not_of ("abc"); // find_firstof is the first character that is not matched with the string. // int pos = str. find_last_of ("123"); // find_firstof is the last string matching position 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; // The cout operator is overloaded <(strA = strB) <endl; // compare the address cout <str1.empty () <
Algorithm function randend expressions and class Overloading
# Include
# Include
# Include
Using namespace std; template
// Template class to add {public: void operator () (T & t) to the operation class of some container elements // reload the () operator and perform the operation {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; // omitted, // 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 expression auto funA = [] (int) {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 <endl;}); cin. get ();}
GPU Programming
Bitcoin mining often uses gpu for computing.
Helloworld
// Win7 cannot directly debug the gpu # include // gpu computing # include
Using 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 computing structure. av is stored in the GPU video memory and initialized Based on the array. // = directly operates AV, (index <1> idx) operate each element // extent each element // restrict (amp) locates the GPU and executes 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 debugging requires the following settings
Single point test
# Include
# Include
// Underlying file of the operating system, test time # define COUNT 100000 float nickName_GPU [COUNT]; float nickName_CPU [COUNT]; double rungpu (int num) restrict (amp) // only {double temp = 0; for (int I = 0; I <num; I ++) {temp + = I;} can be executed within the GPU ;} return temp;} double runcpu (int num) restrict (cpu) // can only be executed inside the CPU {double temp = 0; for (int I = 0; I <num; I ++) {temp + = I;} return temp;} double runcpugpu (int num) restrict (amp, cpu) // concurrent execution {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; // statistical time, accurate to millisecond QueryPerformanceFrequency (& freq); QueryPerformanceCounter (& strt); // query time double dx [1] = {0.0}; // data, array of an element, double db = 0.0; concurrency: array_view
Myview (1, dx); // convert to gpu for parallel_for_each (myview. extent, [=] (concurrency: index <1> idx) restrict (amp) {myview [idx] + = rungpu (20000000) ;}); myview. synchronize (); // explicitly waits for the GPU computing to complete and calls back the data to the memory printf ("% f \ n", dx [0]); QueryPerformanceCounter (& ed ); // display printf ("GPU time consumed: % d millisecond \ r \ n", (ed. quadPart-strt. quadPart) * 1000/freq. quadPart); QueryPerformanceCounter (& strt); printf ("% f \ n", runcpu (20000000); QueryPerformanceCounter (& ed); printf ("CPU duration: % d millisecond \ r \ n ", (ed. quadPart-strt. quadPart) * 1000/freq. quadPart); puts ("test ended"); getchar (); return 0;} int mainW (void) // test parallel computing {LARGE_INTEGER freq; LARGE_INTEGER strt; LARGE_INTEGER ed; queryPerformanceFrequency (& freq); QueryPerformanceCounter (& strt); concurrency: array_view
MyView (COUNT, nickName_GPU); // input data into the video memory, 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 (); // explicitly waits for the GPU computing to complete and calls back the data to the memory QueryPerformanceCounter (& ed); printf ("GPU time consumed: % d millisecond \ 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 Duration: % d millisecond \ r \ n ", (ed. quadPart-strt. quadPart) * 1000/freq. quadPart); for (int idx = 0; idx <COUNT; idx ++) {if (nickName_CPU [idx]! = NickName_GPU [idx]) {puts ("the CPU and GPU computing results do not match! "); Getchar (); return 0 ;}} puts (" test ended "); getchar (); return 0 ;}
High Cpu frequency and gpu, suitable for single-point computing, but there are many gpu containers, suitable for parallel computing.
The Cpu advantage lies in single-point computing. Around a calculator, only one number is calculated, and the calculation speed is the fastest.
Gpu advantage: concurrent computing. Gpu Acceleration Program,