Shared pointer in C++
Level 4190 keystrokes
Two shared_ptr objects share ownership of one int; use_count shows the number of owners, which drops from 2 to 1 after reset.
C++ code
// make_shared allocates an int holding 42; sp1 is its first owner1auto sp1 = make_shared<int>(42);// Copying the pointer adds a second owner of the same int2auto sp2 = sp1;// Both point to the same value: prints 42 423cout << *sp1 << " " << *sp2 << endl;// use_count is the number of owners: Count: 24cout << "Count: " << sp1.use_count() << endl;// sp2 gives up ownership; the int stays alive because sp1 still owns it5sp2.reset();// Only one owner is left: Count: 16cout << "Count: " << sp1.use_count() << endl;Output
42 42 Count: 2 Count: 1
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
<× 21;× 6"× 6(× 4)× 4_× 3.× 3:× 2*× 2=× 2