Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2007
Passing QMap as anonymous object ?


Message 1 in thread

Is it possible to construct an anonymous QMap object, using
some syntax that I can't quite think of ? e.g.

some_func_taking_a_qmap(QMap<QString, int>( <what goes here ?>));

I suspect the answer is no, but I'd be glad if someone can
prove me wrong.

-- 
 [ signature omitted ] 

Message 2 in thread

Stephen Collyer wrote:
>Is it possible to construct an anonymous QMap object, using
>some syntax that I can't quite think of ? e.g.
>
>some_func_taking_a_qmap(QMap<QString, int>( <what goes here ?>));
>
>I suspect the answer is no, but I'd be glad if someone can
>prove me wrong.

Hmm... no, not with QMap. You can, but it's only useful for a single 
element.

	some_func(QMap<QString,int>().insert(key, value));

It's a lot more useful with QList:

	some_func(QStringList() << string1 << string2 << string3);

Which means the following helper function can solve your problem:

template<typename Key, typename Value>
QMap<Key, Value> &operator<<(QMap<Key, Value> &map, QPair<Key, Value> v)
{
    map.insert(v.first, v.second);
    return map;
}

Then you can write:

	some_func(QMap<QString,int>() << qMakePair(string1, 1)
	    << qMakePair(string2, 2)
	    << qMakePair(string3, 3));

Notes:
	1) untested, use at your own risk
	2) probably not portable to some old compilers
	3) I could probably add that operator<< to QMap, right?

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Ah!

Thiago Macieira wrote:
> Stephen Collyer wrote:
>> Is it possible to construct an anonymous QMap object, using
>> some syntax that I can't quite think of ? e.g.
>>
>> some_func_taking_a_qmap(QMap<QString, int>( <what goes here ?>));
>>
>> I suspect the answer is no, but I'd be glad if someone can
>> prove me wrong.
> 
> Hmm... no, not with QMap. You can, but it's only useful for a single 
> element.

I see I misunderstood the question... /me switches on brain.

Tim
----------------------------------------------------------------------
dr. t. dewhirst
director                                       [w] www.bugless.co.uk
bugless software development ltd.

--
 [ signature omitted ] 

Message 4 in thread

Thiago Macieira wrote:

> Which means the following helper function can solve your problem:
> 
> template<typename Key, typename Value>
> QMap<Key, Value> &operator<<(QMap<Key, Value> &map, QPair<Key, Value> v)
> {
>     map.insert(v.first, v.second);
>     return map;
> }
> 
> Then you can write:
> 
> 	some_func(QMap<QString,int>() << qMakePair(string1, 1)
> 	    << qMakePair(string2, 2)
> 	    << qMakePair(string3, 3));
> 

That's the kind of solution I was vaguely thinking about. The
motivation for this is that I want to replicate the ability,
available in Perl, to do something like:

some_func(string1 => 1, string2 => 2, string => 3);

sub some_func
{
    my %args = @_;

    print $args{string1};
}

so I was trying to dream up a similar-ish syntax. This would
be useful for code that deals with the various QSql classes.

At the moment I'm not sure that it's possible to do much better
than what you have done - you'd need language support (I think)
for a std::make_pair literal syntax.

> 	3) I could probably add that operator<< to QMap, right?

Hmm. I don't know. Is it of general enough interest ?

-- 
 [ signature omitted ] 

Message 5 in thread

Stephen Collyer wrote:
>>       3) I could probably add that operator<< to QMap, right?
>
>Hmm. I don't know. Is it of general enough interest ?

STL's associative container classes have members that operate on 
std::pair. I don't see why we shouldn't do it.

I would have loved to use foreach( ) on associative containers and still 
be able to reach the key, not just the value.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 6 in thread

On Dec 19, 2007 8:27 AM, Thiago Macieira <thiago.macieira@xxxxxxxxxxxxx> wrote:
> Stephen Collyer wrote:
> >> 3) I could probably add that operator<< to QMap, right?
> >
> >Hmm. I don't know. Is it of general enough interest ?
>
> STL's associative container classes have members that operate on
> std::pair. I don't see why we shouldn't do it.
>
> I would have loved to use foreach( ) on associative containers and still
> be able to reach the key, not just the value.
>

Something like this?

void showAll(QMap<QString, TextBook*> map) {
    foreach (QString key, map.keys()) {
        Textbook* tb = map.value(key);
        cout << '[' << key << ']' << ":"
             << tb->toString() << endl;
    }
}

--
 [ signature omitted ] 

Message 7 in thread

Alan Ezust wrote:
> <thiago.macieira@xxxxxxxxxxxxx> wrote:
> > I would have loved to use foreach( ) on associative 
> > containers and still
> > be able to reach the key, not just the value.
> 
> Something like this?
> 
> void showAll(QMap<QString, TextBook*> map) {
>     foreach (QString key, map.keys()) {
>         Textbook* tb = map.value(key);
>         cout << '[' << key << ']' << ":"
>              << tb->toString() << endl;
>     }
> }

You're missing the point here, try the same when using QMultiMap :-)
(not to mention it's less efficient to iterate over keys and then having
to lookup each value)

Cheers,
Peter

--
 [ signature omitted ] 

Message 8 in thread

Alan Ezust wrote:
>On Dec 19, 2007 8:27 AM, Thiago Macieira <thiago.macieira@xxxxxxxxxxxxx> 
wrote:
>> Stephen Collyer wrote:
>> >> 3) I could probably add that operator<< to QMap, right?
>> >
>> >Hmm. I don't know. Is it of general enough interest ?
>>
>> STL's associative container classes have members that operate on
>> std::pair. I don't see why we shouldn't do it.
>>
>> I would have loved to use foreach( ) on associative containers and
>> still be able to reach the key, not just the value.
>
>Something like this?
>
>void showAll(QMap<QString, TextBook*> map) {
>    foreach (QString key, map.keys()) {
>        Textbook* tb = map.value(key);
>        cout << '[' << key << ']' << ":"
>             << tb->toString() << endl;
>    }
>}

Yes, but more efficiently.

The above code creates a temporary QList<QString>, iterates over it and 
then looks up each key. If the lookup time in QMap is O(log n) [it's a 
binary search of ordered keys], then your algorithm runs on O(n log n), 
with a memory overhead of O(n).

Replacing it with:
  QMap<QString, TextBook *>::ConstIterator it = map.constBegin();
  for ( ; it != map.constEnd(); ++it)
      cout << '[' << it.key() << ']' << ": "
        << it.value()->toString() << endl;

will mean there's no temporary object overhead and it runs on O(n).

PS: QHash should have a lookup time of O(1), so if your code operated on a 
QHash instead of QMap, it should run on O(n) as well, but with a memory 
overhead.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 9 in thread

Thiago Macieira wrote:
> Then you can write:
> 
> 	some_func(QMap<QString,int>() << qMakePair(string1, 1)
> 	    << qMakePair(string2, 2)
> 	    << qMakePair(string3, 3));
> 
> Notes:
> 	1) untested, use at your own risk
> 	2) probably not portable to some old compilers
> 	3) I could probably add that operator<< to QMap, right?

3) would be wonderful :) i've always loved this, particularly with
QStringList (it's also supported with QVector, QSet, etc.) 
it should be available for all of Qt's containers! (including QMap,
QHash, QMultiMap, QMultiMap :)

Cheers,
Peter

--
 [ signature omitted ] 

Message 10 in thread

Hi,

Stephen Collyer wrote:
> Is it possible to construct an anonymous QMap object, using
> some syntax that I can't quite think of ? e.g.
> 
> some_func_taking_a_qmap(QMap<QString, int>( <what goes here ?>));
> 
> I suspect the answer is no, but I'd be glad if someone can
> prove me wrong.

You want to be able to pass a QMap to a function but you don't know the 
map types?

template < typename KeyT, typename ValueT >
void some_func( QMap< KeyT, ValueT > m )
{
     ... do stuff
}

Should do the trick.

Tim
----------------------------------------------------------------------
dr. t. dewhirst
director                                       [w] www.bugless.co.uk
bugless software development ltd.

--
 [ signature omitted ]