///////////////////////////////////////////////////////////////////////////// // Creation 19/12/2003 // // // LSIT.CPP // -------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Show how to use the std::list object // ///////////////////////////////////////////////////////////////////////////// #pragma warning(disable : 4786) // Suppress low warnings #include #include #include // A custom class class CTestElem { public: CTestElem( int i = 0, std::string s = "") : m_i (i), m_string (s) {} ~CTestElem() {} std::string getString() { return m_string;} void setString(std::string s) { m_string = s;} int getInteger() { return m_i; } void setInteger(int i) { m_i = i; } private: int m_i; std::string m_string; }; // How to manage a list of integers typedef std::list ListInt; typedef ListInt::iterator ListIntIterator; // How to manage a list of strings typedef std::list ListString; typedef ListString::iterator ListStringIterator; // How to manage a list of custom objects typedef std::list ListTestElem; typedef ListTestElem::iterator ListTestElemIterator; ///////////////////////////////////////////////////////////////////////////// // // LISTINTEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --listIntExample-- // // List of integer // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void listIntExample() { ListInt listInt; ListIntIterator it; int i; // list contains 1 2 3 listInt.push_front (2); listInt.push_front (1); listInt.push_back (3); // Display the list content i = 0; for( it = listInt.begin(); it != listInt.end(); it ++) { int iListElem = *it; printf( "The elem at pos (%d) is (%d)\n", i, iListElem ); i ++; } // Remove first elem listInt.erase(listInt.begin()); // Display the list content i = 0; for( it = listInt.begin(); it != listInt.end(); it ++) { int iListElem = *it; printf( "The elem at pos (%d) is (%d)\n", i, iListElem ); i ++; } // list contains now 12 12 12 12 listInt.assign(4, 12); // Display the list content i = 0; for( it = listInt.begin(); it != listInt.end(); it ++) { int iListElem = *it; printf( "The elem at pos (%d) is (%d)\n", i, iListElem ); i ++; } // Clear the list (=> destroys all elems) listInt.clear(); if( listInt.empty() ) { printf( "list is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // LISTSTRINGEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --listStringExample-- // // Same as before, with strings // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void listStringExample() { ListString listString; ListStringIterator it; int i; // list contains 1 2 3 listString.push_front ("2"); listString.push_front ("1"); listString.push_back ("3"); // Display the list content i = 0; for( it = listString.begin(); it != listString.end(); it ++) { std::string iListElem = *it; printf( "The elem at pos (%d) is (%s)\n", i, iListElem ); i ++; } // Remove first elem listString.erase(listString.begin()); // Display the list content i = 0; for( it = listString.begin(); it != listString.end(); it ++) { std::string iListElem = *it; printf( "The elem at pos (%d) is (%s)\n", i, iListElem ); i ++; } // list contains now 12 12 12 12 listString.assign(4, "12"); // Display the list content i = 0; for( it = listString.begin(); it != listString.end(); it ++) { std::string iListElem = *it; printf( "The elem at pos (%d) is (%s)\n", i, iListElem ); i ++; } // Clear the list (=> destroys all elems) listString.clear(); if( listString.empty() ) { printf( "list is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // LISTTESTELEMEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --listTestElemExample-- // // Same as before, with a custom class // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void listTestElemExample() { ListTestElem listTestElem; ListTestElemIterator it; int i; // list contains 1 2 3 listTestElem.push_front ( CTestElem(2, "2") ); listTestElem.push_front ( CTestElem(1, "1") ); listTestElem.push_back ( CTestElem(3, "3") ); // Display the list content i = 0; for( it = listTestElem.begin(); it != listTestElem.end(); it ++) { CTestElem iListElem = *it; printf( "The elem at pos (%d) is (%d-%s)\n", i, iListElem.getInteger(), iListElem.getString() ); i ++; } // Remove first elem listTestElem.erase(listTestElem.begin()); // Display the list content i = 0; for( it = listTestElem.begin(); it != listTestElem.end(); it ++) { CTestElem iListElem = *it; printf( "The elem at pos (%d) is (%d-%s)\n", i, iListElem.getInteger(), iListElem.getString() ); i ++; } // list contains now 12 12 12 12 listTestElem.assign(4, CTestElem(12,"12") ); // Display the list content i = 0; for( it = listTestElem.begin(); it != listTestElem.end(); it ++) { CTestElem iListElem = *it; printf( "The elem at pos (%d) is (%d-%s)\n", i, iListElem.getInteger(), iListElem.getString() ); i ++; } // Clear the list (=> destroys all elems) listTestElem.clear(); if( listTestElem.empty() ) { printf( "list is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // MAIN ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --main-- // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void main() { // list int listIntExample(); // list string listStringExample(); // list of CTestElem listTestElemExample(); }