/////////////////////////////////////////////////////////////////// // types.cpp - demonstrate use of value_type from STL container // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2001 // /////////////////////////////////////////////////////////////////// //#pragma warning(disable : 4786) #include #include #include #include #include #include #include using namespace std; //----< format container display string using typeid() >------- template string display(const container &c) { ostringstream dStr; dStr << "\n container type is "; string type = typeid(c).name(); if(type.size() > 55) { type.resize(55); type += " ..."; } dStr << type; dStr << "\n container size is "; dStr << c.size(); dStr << "\n container values: "; container::const_iterator it; for(it=c.begin(); it!=c.end(); it++) { dStr << (*it); dStr << " "; } return dStr.str(); } //----< compute sum of values in an STL container >------------ // // Note the use of container::value_type // template typename container::value_type sumOfContents(const container &c) { container::const_iterator it; container::value_type sum; if(typeid(sum) != (typeid(string))) sum = container::value_type(0); for(it=c.begin(); it!=c.end(); it++) sum += *it; return sum; } // //----< extract reference to nth element of container >-------- // // - Note use of reference type: value_type&. // - Use at(index) for vectors, dequeues, and strings. // It's much more efficient. // template typename container::value_type& getVal(container &c, int index) { if(0 > index || index >= c.size()) throw exception("index domain error in getVal(...)"); int i; container::iterator it = c.begin(); for(i=0; i---------------------------- void main() { cout << "\n Demonstrate use of container type definitions " << "\n ===============================================\n"; cout << "\n test container display " << "\n ------------------------"; string strArray[] = { "one", "two", "three" }; list listStr(&strArray[0],&strArray[3]); cout << display(listStr).c_str(); int intArray[] = { 1, 2, 3 }; vector vecInt(&intArray[0],&intArray[3]); cout << display(vecInt).c_str(); double dblArray[] = { 1.0/3.0, 1.0/4.0, 1.0/5.0 }; set setDbl(&dblArray[0],&dblArray[3]); cout << display(setDbl).c_str() << endl; cout << "\n test computing container sum - shows use of type_value" << "\n --------------------------------------------------------"; cout << "\n sum of list of strings = " << sumOfContents(listStr).c_str(); cout << "\n sum of vector of ints = " << sumOfContents(vecInt); cout << "\n sum of set of doubles = " << sumOfContents(setDbl) << endl; cout << "\n test accessing nth element with bounds checking " << "\n -------------------------------------------------"; try { cout << "\n modifying second element:"; getVal(listStr,1) = "2nd"; cout << display(listStr).c_str() << endl; cout << "\n attempting to modify the fourth element "; getVal(listStr,3) = "4th"; cout << display(listStr).c_str() << endl; } catch(exception &e) { cout << "\n " << e.what() << "\n"; } cout << "\n\n"; }