///////////////////////////////////////////////////////////////////////////// // Creation 20/12/2003 // // // SET.CPP // ------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Same as list.cpp, but it is possible to look for data // without looping wint the find() method // ///////////////////////////////////////////////////////////////////////////// #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 set of integers typedef std::set SetInt; typedef SetInt::iterator SetIntIterator; // How to manage a set of strings typedef std::set SetString; typedef SetString::iterator SetStringIterator; // How to manage a set of custom objects typedef std::set SetTestElem; typedef SetTestElem::iterator SetTestElemIterator; ///////////////////////////////////////////////////////////////////////////// // // SETINTEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --setIntExample-- // // Set of integer // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void setIntExample() { SetInt setInt; SetIntIterator it; int i; // set contains 1 2 3 4 setInt.insert(1); setInt.insert(2); setInt.insert(3); setInt.insert(4); // Display the set content i = 0; for( it = setInt.begin(); it != setInt.end(); it ++) { int iSetElem = *it; printf( "The elem at pos (%d) is (%d)\n", i, iSetElem ); i ++; } // Remove first elem setInt.erase(setInt.begin()); // Display the set content i = 0; for( it = setInt.begin(); it != setInt.end(); it ++) { int iSetElem = *it; printf( "The elem at pos (%d) is (%d)\n", i, iSetElem ); i ++; } // find 3 in the set it = setInt.find( 3 ); if( it != setInt.end() ) { int iSetElem = *it; printf( "Found 3 (*it=%d) in the set\n", iSetElem ); } // Clear the set (=> destroys all elems) setInt.clear(); if( setInt.empty() ) { printf( "set is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // SETSTRINGEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --setStringExample-- // // Same as before, with strings // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void setStringExample() { SetString setString; SetStringIterator it; int i; // set contains 1 2 3 4 setString.insert("1"); setString.insert("2"); setString.insert("3"); setString.insert("4"); // Display the set content i = 0; for( it = setString.begin(); it != setString.end(); it ++) { std::string iSetElem = *it; printf( "The elem at pos (%d) is (%s)\n", i, iSetElem ); i ++; } // Remove first elem setString.erase(setString.begin()); // Display the set content i = 0; for( it = setString.begin(); it != setString.end(); it ++) { std::string iSetElem = *it; printf( "The elem at pos (%d) is (%s)\n", i, iSetElem ); i ++; } // find 3 in the set it = setString.find( "3" ); if( it != setString.end() ) { std::string iSetElem = *it; printf( "Found 3 (*it=%s) in the set\n", iSetElem ); } // Clear the set (=> destroys all elems) setString.clear(); if( setString.empty() ) { printf( "set is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // SETTESTELEMEXAMPLE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --setTestElemExample-- // // Same as before, with a custom class // // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void setTestElemExample() { SetTestElem setTestElem; SetTestElemIterator it; int i; CTestElem t1 (1, "1"), t2 (2, "2"), t3(3, "3"), t4(4, "4"); // set contains 1 2 3 4 setTestElem.insert( &t1 ); setTestElem.insert( &t2 ); setTestElem.insert( &t3 ); setTestElem.insert( &t4 ); // Display the set content i = 0; for( it = setTestElem.begin(); it != setTestElem.end(); it ++) { CTestElem * pSetElem = *it; printf( "The elem at pos (%d) is (%d-%s)\n", i, pSetElem->getInteger(), pSetElem->getString() ); i ++; } // Remove first elem setTestElem.erase(setTestElem.begin()); // Display the set content i = 0; for( it = setTestElem.begin(); it != setTestElem.end(); it ++) { CTestElem * pSetElem = *it; printf( "The elem at pos (%d) is (%d-%s)\n", i, pSetElem->getInteger(), pSetElem->getString() ); i ++; } // find 3 in the set (not very useful in this case) it = setTestElem.find( &t3 ); if( it != setTestElem.end() ) { CTestElem * pSetElem = *it; printf( "Found 3 (%d-%s)\n", pSetElem->getInteger(), pSetElem->getString() ); } // Clear the set (=> destroys all elems) setTestElem.clear(); if( setTestElem.empty() ) { printf( "set is empty\n" ); } } ///////////////////////////////////////////////////////////////////////////// // // MAIN ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --main-- // ARGUMENTS // RETOUR/RESULTAT // void // REMARQUE // Rev 19/12/2003 ////////////////////////////////////////////////////////////////////////////// void main() { // set int setIntExample(); // set string setStringExample(); // set of CTestElem setTestElemExample(); }