Ali nail face questions

Define a class list for encapsulation, so that can be operated by a suffix list:
. 1
#include <list> 2 #include <Vector> . 3 #include <the iostream> . 4 #include < String > . 5 . 6 the using namespace STD; . 7 . 8 class the Test . 9 { 10 public : . 11 the Test (); 12 is // ~ the Test (); 13 is the Test (List < String > strlist); 14 String & operator [] ( int n-);It should generally have // & can be prevented (the process as return values, there will be a copy of the value down) to call an object constructor returns 15 16 public : . 17 List < String > m_strList; 18 is }; . 19 20 is 21 is :: the Test the Test (List < String > strlist) 22 is { 23 is m_strList = strlist; 24 } 25 26 is // the Test :: ~ the Test () // when defining destructor, achieved without error. 27 // { 28 // 29 // } 30 31 is String & the Test :: operator[](int n) 32 { 33 list<string>::iterator i = m_strList.begin(); 34 vector<list<string>::iterator> vecStrIter; 35 for (; i != m_strList.end(); i++) 36 { 37 vecStrIter.push_back(i); 38 } 39 return *(vecStrIter[n]); 40 } 41 42 43 int main() 44 { 45 string str[4] = {"a", "li", "ba", "ba!"}; 46 list<string> strList(str, str+4); 47 Test test(strList); 48 cout << test[1] << endl; 49 return 0; 50 }

 

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11846021.html