kostis89 Δημοσ. 15 Μαΐου 2013 Δημοσ. 15 Μαΐου 2013 Το ναι πάει στο ότι σας επιτρέπουν να χρησιμοποιήσετε π.χ STL templates; Ναι, αρκεί να βγαίνει το επιθυμητο αποτέλεσμα με οποιοδήποτε τρόπο!
Timonkaipumpa Δημοσ. 15 Μαΐου 2013 Δημοσ. 15 Μαΐου 2013 Εάν σας επιτρέπουν... τότε δες προηγούμενα post μου στο ίδιο thread που δίνουν τα containers της C++ και την σελίδα με το documentation τους.
temp_ Δημοσ. 15 Μαΐου 2013 Δημοσ. 15 Μαΐου 2013 (επεξεργασμένο) Ναι, αρκεί να βγαίνει το επιθυμητο αποτέλεσμα με οποιοδήποτε τρόπο! Ο C κώδικας σε αυτό το ποστ, με list containers σε C++98 θα μπορούσε να γραφεί κάπως έτσι: #include <iostream> #include <list> #include <cstdlib> #include <ctime> using namespace std; typedef struct Column Column; struct Column { int id; int value; }; typedef struct Line Line; struct Line { int id; list<Column> cols; }; /* ----------------------------------------------------- */ Column new_col( int id, int value ) { Column ret = {id, value}; return ret; } /* ----------------------------------------------------- */ Line new_line( int id ) { Line ret; ret.id = id; return ret; } /* ----------------------------------------------------- */ int main( void ) { list<Line> lines; // list of lines list<Line>::iterator itl; // lines-list iterator list<Column>::iterator itc; // cols-list iterator srand( time(0) ); // populate the lines-list with 4 lines for (int i=0; i < 4; i++ ) { Line ln = new_line(i); // add randomly 1 to 5 columns in the cols-list of the current line for (int j=0; j < rand() % 5 + 1; j++) { int cid = rand() % 255 + 1; // column id: random from 1 to 255 int cval = rand(); // column value: random ln.cols.push_back( new_col(cid,cval) ); } lines.push_back( ln ); } // print the contents of the lines-list cout << "--- CONTENTS OF lines LIST ---\n" << endl; for (itl = lines.begin(); itl != lines.end(); itl++) { // current line's fields cout << "--- line ---" << endl; cout << "id : " << itl->id << endl; cout << "cols: @" << &(*itl) << endl; // current line's cols-list cout << "\t--- cols list ---" << endl; for (itc = itl->cols.begin(); itc != itl->cols.end(); itc++) { cout << "\tid : " << itc->id << endl; cout << "\tvalue: " << itc->value << endl; cout << endl; } } system( "pause" ); // Windows only return 0; } Έξοδος: --- CONTENTS OF lines LIST --- --- line --- id : 0 cols: @0x3e2dd0 --- cols list --- id : 65 value: 14940 id : 112 value: 25324 --- line --- id : 1 cols: @0x3e2e38 --- cols list --- id : 14 value: 12349 id : 41 value: 29201 id : 239 value: 4967 --- line --- id : 2 cols: @0x3e2ea0 --- cols list --- id : 247 value: 2730 --- line --- id : 3 cols: @0x3e2ed8 --- cols list --- id : 124 value: 32086 id : 226 value: 17106 id : 91 value: 16462 Press any key to continue . . . Έχω καιρό να ασχοληθώ με C++ οπότε σίγουρα κάποιος άλλος θα μπορέσει να σου δώσει πολύ πιο συμπαγή λύση (με κλάσεις, references, κλπ). Χρησιμοποίησα list containers αντί για forward_list γιατί αυτά τα τελευταία νομίζω μπήκαν στην C++11. Επεξ/σία 16 Μαΐου 2013 από temp_
temp_ Δημοσ. 15 Μαΐου 2013 Δημοσ. 15 Μαΐου 2013 Με αφορμή το νήμα βρήκα λίγο χρόνο και ξεσκόνισα πάνω-πάνω την C++ μου, καταλήγοντας σε πιο αντικειμενοστραφή κώδικα. Το αν όμως τώρα αυτό είναι καλύτερο ή χειρότερο, θα σας γελάσω (πάντως με exceptions βαρέθηκα να ασχοληθώ)... #include <iostream> #include <list> #include <cstdlib> #include <ctime> #define DBG_ERROR( msg ) \ do { \ std::cerr << "*** " << __FILE__ << "|" << __func__ << "()|ln:" << __LINE__ << endl;\ std::cerr << "*** " << (msg) << endl << endl; \ }while(0) /* cross-platform alternative to Windows system("pause") but breaks if misused */ #define pressENTER() \ do { \ std::cout << "press ENTER... "; \ std::cout.flush(); \ std::cin.clear(); \ while ( '\n' != std::cin.get() ) \ ; \ }while(0) using namespace std; /* ----------------------------------------------------- * Class defining a column. */ class Column { int id; int value; public: /** * Constructors. */ Column() { id = -1; value = -1; }; Column( int id, int value ) { this->id = id; this->value = value; } /** * Print contents of the current column. */ void print( char indent ) { cout << indent << "id : " << this->id << endl; cout << indent << "value: " << this->value << endl; cout << endl; } }; /* ----------------------------------------------------- * Class defining a line. */ class Line { int id; // line id list<Column> cols; // list of columns public: /** * Constructors. */ Line(){ this->id = -1; }; Line( int id ){ this->id = id; }; /** * Add into the cols-list the column pointed to by col. */ void add_column( Column *col ) { if ( !col ) { DBG_ERROR( "EINVAL (NULL pointer argument)" ); return; } this->cols.push_back( *col ); } /** * Add randomly up to n columns into the cols-list of the line, * with each column having its id set to a random integer from * 1 to idMax, and its value set to a unrestricted random integer. * (if n < 1 then no columns are inserted & a warning is printed). */ void add_random_columns( int n, int idMax=255 ) { if ( n < 1 ) { DBG_ERROR( "EINVAL (zero or negative value of n)" ); return; } for (int i=0; i < rand() % n + 1; i++) { Column col( rand() % idMax + 1 , rand() ); this->add_column( &col ); } } /** * Print the contents of line's cols-list. */ void print_columns( const char *title, char indent='\t' ) { if ( title ) cout << indent << title << endl; list<Column>::iterator it; for (it = this->cols.begin(); it != this->cols.end(); it++) it->print(indent); } /** * Print the contents of the line. */ void print( const char *lntitle, const char *cstitle, char csindent='\t' ) { if ( lntitle ) cout << lntitle << endl; cout << "id : " << this->id << endl; cout << "cols: @" << &this->cols << endl; this->print_columns( cstitle, csindent ); } }; /* ----------------------------------------------------- * Main entry of the program */ int main() { list<Line> lines; // the lines-list list<Line>::iterator it; // an iterator for the lines-list srand( time(0) ); // init pseudo-random generator /* Populate the lines-list with 4 lines, assigning them sequential ids from 0 to 3. Each line has its cols-list populated with up to 5 random columns, via the method call: add_random_columns(5,255)... see its doc. */ for (int i=0; i < 4; i++ ) { Line ln(i); ln.add_random_columns(5, 255); lines.push_back( ln ); } /* Print the contents of the lines-list */ cout << "--- CONTENTS OF lines LIST ---\n" << endl; for (it = lines.begin(); it != lines.end(); it++) { it->print( "--- line ---", "--- cols list ---" ); } pressENTER(); return 0; } Η έξοδος είναι ακριβώς ίδια με προηγουμένως.
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα