本文主要是介绍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&&)
#endif
2. 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内存管理测试结果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!