C++ Primer 5th notes (chap 13 copy control) Example 2 memory management test results

1. Test code

   	string temp[] = {
    
     "one", "two", "three" };
   	StrVec sv(begin(temp), end(temp));

   	// run the string empty funciton on the first element in sv
   	if (!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 time
   
   	ifstream 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 assignment

   	in.open("D:/code/cprimerGit/cplusprimer/cprimer/data/strvec-storyDataFile");
   	v2 = getVec(in);          // getVec(in) is an rvalue; move assignment
   	in.close();

   	StrVec vec;  // empty StrVec
   	string 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 16
   	s = "the end";
#ifdef VARIADICS
   	vec.emplace_back(10, 'c'); // adds cccccccccc as a new last element
   	vec.emplace_back(s);  // uses the string copy constructor
#else
   	vec.push_back(string(10, 'c')); // calls push_back(string&&)
   	vec.push_back(s);               // calls push_back(const string&)
#endif
   	string s1 = "the beginning", s2 = s;
#ifdef VARIADICS
   	vec.emplace_back(s1 + s2); // uses the move constructor
#else
   	vec.push_back(string(s1 + s2)); // calls push_back(string&&)
#endif

2. The content of 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. Output result:

Insert picture description here


【reference】

[1] Code copyControl.h

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/113914648