C++ Primer 5th笔记(chap 13 拷贝控制) 实例2内存管理测试结果
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(chap 13 拷贝控制) 实例2内存管理测试结果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 測試代碼
string temp[] = { "one", "two", "three" };StrVec sv(begin(temp), end(temp));// run the string empty funciton on the first element in svif (!sv[0].empty())sv[0] = "None"; // assign a new value to the first string // we'll call getVec a couple of times// and will read the same file each timeifstream in("D:/code/cprimerGit/cplusprimer/cprimer/data/strvec-storyDataFile");//ifstream in("../data/strvec-storyDataFile");StrVec svec = getVec(in);print(svec);in.close();cout << "copy " << svec.size() << endl;auto svec2 = svec;print(svec2);cout << "assign" << endl;StrVec svec3;svec3 = svec2;print(svec3);StrVec v1, v2;v1 = v2; // v2 is an lvalue; copy assignmentin.open("D:/code/cprimerGit/cplusprimer/cprimer/data/strvec-storyDataFile");v2 = getVec(in); // getVec(in) is an rvalue; move assignmentin.close();StrVec vec; // empty StrVecstring s = "some string or another";vec.push_back(s); // calls push_back(const string&)vec.push_back("done"); // calls push_back(string&&)// emplace member covered in chpater 16s = "the end"; #ifdef VARIADICSvec.emplace_back(10, 'c'); // adds cccccccccc as a new last elementvec.emplace_back(s); // uses the string copy constructor #elsevec.push_back(string(10, 'c')); // calls push_back(string&&)vec.push_back(s); // calls push_back(const string&) #endifstring s1 = "the beginning", s2 = s; #ifdef VARIADICSvec.emplace_back(s1 + s2); // uses the move constructor #elsevec.push_back(string(s1 + s2)); // calls push_back(string&&) #endif2. strvec-storyDataFile文件內容:
Alice Emma has long flowing red hair. Her Daddy says when the wind blows through her hair, it looks almost alive, like a fiery bird in flight. A beautiful fiery bird, he tells her, magical but untamed. "Daddy, shush, there is no such thing," she tells him, at the same time wanting him to tell her more. Shyly, she asks, "I mean, Daddy, is there?"3. 輸出結果:
【參考】
[1] 代碼copyControl.h
總結
以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 13 拷贝控制) 实例2内存管理测试结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha