C++ Primer Exercise 5.18

来源:互联网 发布:java汽车销售管理系统 编辑:程序博客网 时间:2024/05/16 05:01

Understanduing the difference between C++ and C# therefore know the computer language deeper.

 

    vector<string*> svec;

    string line;

    

     while(getline(cin,line))

     {

       //1)Instead, the new expression returns a pointer to the newly allocated objects.

      string *sp= new string(line);

      svec.push_back(sp);

     }

      // 2) understanding the iterator which use the composition not the inhertiance to solve the traverse problem to any container.

     vector<string*>::iterator ster = svec.begin();

    while (ster != svec.end())

    {

        cout <<**ster<<" "<<(**ster).size() << endl;

        ster++;

     }

     //3) delete the allocated storage in heap

     ster = svec.begin();

     while (ster != svec.end())

     {  

           delete *ster;

          ster++;

     }

return 0;

 

//Following is the result.

原创粉丝点击