Afficher un message
Vieux 04/06/2008, 02h23   #18
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to create a vector of vector?

Kai-Uwe Bux wrote:

> Kai-Uwe Bux wrote:
>
>> Ramon F Herrera wrote:
>>
>>> What I am saving is a set of radio button (or check mark) widgets.
>>> Some of them belong to a logical group, which is used to provide the
>>> mutual exclusivity. When the user clicks on one, the other N-1 are
>>> automatically turned off.
>>>
>>> See below how I ended up implementing the insertion. If two buttons
>>> have the same name, they belong in the same logical group. Observant
>>> readers will notice this is of PDF.
>>>
>>> -RFH
>>>
>>> ------------
>>>
>>> void insertGroupedRadioButton(RadioButtonNode *radio)
>>> {
>>> int i;
>>> RadioButtonGroup *empty_vector = new RadioButtonGroup();
>>>
>>> for (i = 0; i < (int)ListOfRadioButton.size() && radio->name !=
>>> ListOfRadioButton[i][0].name; i++);
>>>
>>> if (i == ListOfRadioButton.size()) {
>>> ListOfRadioButton.push_back(*empty_vector);
>>> }
>>>
>>> ListOfRadioButton[i].push_back(*radio);
>>> }

>>
>> It looks to me that
>>
>> std::multimap< std::string, RadioButtonNode* >
>>
>> is something that might be of interest to you. E.g.:

> [snip]
>
> There even is a way to hide the collection:

[snip]

Come to think of it: using multiset and a custom predicate, one can avoid
storing the name string twice:

#include <string>
#include <set>
#include <cassert>
#include <iostream>
#include <ostream>

class RadioButtonNode {

std::string the_name;

class ButtonCollection {

typedef bool (*RadioCompare)( RadioButtonNode*, RadioButtonNode*);

static
bool radio_cmp ( RadioButtonNode* lhs, RadioButtonNode* rhs ) {
assert( lhs != 0);
assert( rhs != 0 );
return ( lhs->name() < rhs->name() );
}

typedef std::multiset< RadioButtonNode*, RadioCompare > map_type;

map_type the_data;

public:

ButtonCollection ( void )
: the_data( &radio_cmp )
{}

void insert ( RadioButtonNode * radio ) {
the_data.insert( radio );
}

void erase ( RadioButtonNode * radio ) {
assert( radio != 0 );
for ( map_type::iterator iter =
the_data.lower_bound( radio );
iter != the_data.upper_bound( radio ); ++iter ) {
if ( *iter == radio ) {
the_data.erase( iter );
return;
}
}
}

void on ( RadioButtonNode * radio ) {
assert( radio != 0 );
for ( map_type::iterator iter =
the_data.lower_bound( radio );
iter != the_data.upper_bound( radio ); ++iter ) {
if ( *iter == radio ) {
(*iter)->turn_on();
} else {
(*iter)->turn_off();
}
}
}

void off ( RadioButtonNode * radio ) {
radio->turn_off();
}

};

ButtonCollection & the_collection ( void ) {
static ButtonCollection dummy;
return ( dummy );
}

protected:

virtual
void turn_on ( void ) {
std::cout << "on: " << name() << " [" << this << "]\n";
}

virtual
void turn_off ( void ) {
std::cout << "off: " << name() << " [" << this << "]\n";
}

public:

RadioButtonNode ( std::string const & name )
: the_name ( name )
{
the_collection().insert( this );
}

~RadioButtonNode ( void ) {
the_collection().erase( this );
}

std::string const & name ( void ) const {
return ( the_name );
}

void on ( void ) {
the_collection().on( this );
}

void off ( void ) {
the_collection().off( this );
}

};

int main ( void ) {
RadioButtonNode * n1 = new RadioButtonNode ( "n" );
RadioButtonNode * n2 = new RadioButtonNode ( "n" );
RadioButtonNode * n3 = new RadioButtonNode ( "n" );
RadioButtonNode * n4 = new RadioButtonNode ( "n" );
RadioButtonNode * q1 = new RadioButtonNode ( "q" );
RadioButtonNode * q2 = new RadioButtonNode ( "q" );
RadioButtonNode * q3 = new RadioButtonNode ( "q" );
RadioButtonNode * q4 = new RadioButtonNode ( "q" );

n2->on();
std::cout << '\n';
delete n1;
n3->on();
}


Best

Kai-Uwe Bux
  Réponse avec citation
 
Page generated in 0,08044 seconds with 9 queries