// ------- test-std1.cpp beg --------- // // Using STL, Standard Template Library for C++. // Testing datatypes vector and list. // // This is how to compile and run this code: // g++ -Wall test-std1.cpp -o test-std1 // ./test-std1 #include #include #include #include #include // For std::in, std::out using namespace std; void printItem(string &s) { cout << s << endl; } int main () { // String test string s("abcdefghijk"); cout <<"c strlen=" << strlen(s.c_str()) << ",s.size=" << s.size() << endl; // Vector array test vector floatArray; floatArray.push_back(1.2345); floatArray.push_back(2.3456); floatArray.push_back(3.4567); floatArray[2] *= 4; // vector::iterator fi = floatArray.begin(); // const_iterator is better here vector::const_iterator fi = floatArray.begin(); while (fi != floatArray.end()) { cout << *fi++ << endl; } // List test list *lis = new list; for (int i=10; i; i--) { ostringstream os; // Convert i to string os << i; lis->push_front(string("Item") + os.str()); } // Print the list for_each (lis->begin(), lis->end(), printItem); delete lis; // Ok return 0; } // ------- test-std1.cpp end ---------