#include <algorithm> #include <iostream> #include <functional> #include <vector> #include <numeric> #include <array> #include <cstring> #include <cstdio> #include <windows.h> #include <memory> using namespace std; void cmem() { while (1) { double * p = new double[1024 * 1024 * 4]; Sleep(3000); Double P; } } void AutoPtr () { the while ( . 1 ) { Double * P ( new new Double [ 1024 * 1024 * . 4 ]); the auto_ptr < Double > AUTOP (P); // create smart pointers, this takeover memory, automatic recovery Sleep ( 3000 ); Double P; } } void autoptrnew () { the while ( . 1 ) { Sleep (3000 ); a unique_ptr < Double > AUTOP ( new new Double [ 1024 * 1024 * 10 ]); // create smart pointers, to take over this memory, automatic recovery Sleep ( 3000 ); Double P; } } // smart pointer memory leak , automatic management int main () { // CMEM (); AutoPtr (); // smart pointer automatically released autoptrnew (); // efficient data management System ( " PAUSE " ); }
#include <algorithm> #include <iostream> #include <functional> #include <vector> #include <numeric> #include <array> #include <cstring> #include <cstdio> #include <windows.h> #include <memory> using namespace std; void cmem() { while (1) { double * p = new double[1024 * 1024 * 4]; Sleep(3000); Double P; } } void AutoPtr () { the while ( . 1 ) { Double * P ( new new Double [ 1024 * 1024 * . 4 ]); the auto_ptr < Double > AUTOP (P); // create smart pointers, this takeover memory, automatic recovery Sleep ( 3000 ); Double P; } } void autoptrnew () { the while ( . 1 ) { Sleep (3000 ); a unique_ptr < Double > AUTOP ( new new Double [ 1024 * 1024 * 10 ]); // create smart pointers, to take over this memory, automatic recovery Sleep ( 3000 ); Double P; } } // smart pointer memory leak , automatic management int main () { // CMEM (); AutoPtr (); // smart pointer automatically released autoptrnew (); // efficient data management System ( " PAUSE " ); }
tuple:
#include <algorithm> #include <iostream> #include <functional> #include <vector> #include <numeric> #include <array> #include <cstring> #include <cstdio> #include <windows.h> #include <memory> #include <tuple> using namespace std; //多元数值,存取不同的数据类型 int main() { char ch = 'x'; short sh = 12;an 12345num =int; double db = 123.4; char*p = "hello"; tuple<char, short, int, double, char*> mytuple(ch,sh,num,db,p); //int i = 1; //auto autov = get<i>(mytuple);//不行,需要常量,需要加const //cout << autov << endl; auto autov = get<0>(mytuple); cout << autov << endl; auto autov1 = get < 1 > (mytuple) cout << autov1 << endl; auto autov2 = get < 2 > (mytuple); cout << autov2 << endl; auto autov3 = get < 3 > (mytuple); cout << autov3 << endl; auto autov4 = get < 4 > (mytuple); cout << autov4 << endl; // for (AUTO: mytuple) // 唯一一个不能用的auto }