SlideShare a Scribd company logo
1 of 1
Download to read offline
本页已使用福昕阅读器进行编辑。
 Trolltech | Documentation | Qt Quarterly | Seriously Weird QRegExp »
                                                                                                                                 福昕软件(C)2005-2009,版权所有,
                                                                                                                                 仅供试用。
Abusing QMap

     The QMap<Key, Value> class stores (key, value) pairs where each key is associated with a single value. This article presents a quick-and-dirty trick to make keys work
     with multiple values.

The basic idea is to replace the Value type with a more complex type, such as QValueList<Value>, like this:   标记一下
  QMap<QString, QStringList> map;

(QStringList is an enhanced QValueList<QString>.) Inserting a value into a "multi-map" is simple:

  map["presidents"].append( "George Washington" );

If the multi-map map already contains a "presidents" key, map[presidents] returns a reference to the QStringList that corresponds to it; otherwise, an empty QStringList is automatically created.

The other operations -- testing the presence of a (key, value) pair and removing a (key, value) pair -- are just as easy:

 if ( map["presidents"].contains("Bill Clinton") )
map["presidents"].remove( "Ronald Reagan" );

The remove() operation above will remove the pair ("presidents", "Ronald Reagan") from the map if the map contains it; otherwise it will do nothing. We can even insert the same (key, value)
pair many times:

  map["presidents"].append( "George Bush" );
  map["presidents"].append( "George Bush" );

Removing one of them is straightforward:

  map["presidents"].remove(
   map["presidents"].find("George Bush") );

In programming, as in politics, duplicate values aren't always welcome. They can easily be avoided in the first place:

 if ( !map["presidents"].contains("John Adams") )
map["presidents"].append( "John Adams" );

And they can easily be removed entirely:

  map["presidents"].remove( "John Adams" );


Copyright © 2002 Trolltech.                                                               Trademarks                                                               Seriously Weird QRegExp »

More Related Content

Similar to Issue 1 Abusing Q Map

A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkZachary Blair
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35Bilal Ahmed
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qtaccount inactive
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopSages
 
Aggregating In Accumulo
Aggregating In AccumuloAggregating In Accumulo
Aggregating In AccumuloBill Slacum
 
Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Robert Metzger
 
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...confluent
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheeltcurdt
 
How to ship customer value faster with step functions
How to ship customer value faster with step functionsHow to ship customer value faster with step functions
How to ship customer value faster with step functionsYan Cui
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseSages
 
Crossing Abstraction Barriers When Debugging In Dynamic Languages
Crossing Abstraction Barriers When Debugging In Dynamic LanguagesCrossing Abstraction Barriers When Debugging In Dynamic Languages
Crossing Abstraction Barriers When Debugging In Dynamic LanguagesBastian Kruck
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Raffi Krikorian
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined FunctionsChristoph Bauer
 
Malli: inside data-driven schemas
Malli: inside data-driven schemasMalli: inside data-driven schemas
Malli: inside data-driven schemasMetosin Oy
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryDatabricks
 

Similar to Issue 1 Abusing Q Map (20)

A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application Framework
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Scala en
Scala enScala en
Scala en
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qt
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
 
Aggregating In Accumulo
Aggregating In AccumuloAggregating In Accumulo
Aggregating In Accumulo
 
Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)Stratosphere Intro (Java and Scala Interface)
Stratosphere Intro (Java and Scala Interface)
 
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...
Data-Oriented Programming with Clojure and Jackdaw (Charles Reese, Funding Ci...
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
How to ship customer value faster with step functions
How to ship customer value faster with step functionsHow to ship customer value faster with step functions
How to ship customer value faster with step functions
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
 
Crossing Abstraction Barriers When Debugging In Dynamic Languages
Crossing Abstraction Barriers When Debugging In Dynamic LanguagesCrossing Abstraction Barriers When Debugging In Dynamic Languages
Crossing Abstraction Barriers When Debugging In Dynamic Languages
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
 
Malli: inside data-driven schemas
Malli: inside data-driven schemasMalli: inside data-driven schemas
Malli: inside data-driven schemas
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 

Issue 1 Abusing Q Map

  • 1. 本页已使用福昕阅读器进行编辑。 Trolltech | Documentation | Qt Quarterly | Seriously Weird QRegExp » 福昕软件(C)2005-2009,版权所有, 仅供试用。 Abusing QMap The QMap<Key, Value> class stores (key, value) pairs where each key is associated with a single value. This article presents a quick-and-dirty trick to make keys work with multiple values. The basic idea is to replace the Value type with a more complex type, such as QValueList<Value>, like this: 标记一下 QMap<QString, QStringList> map; (QStringList is an enhanced QValueList<QString>.) Inserting a value into a "multi-map" is simple: map["presidents"].append( "George Washington" ); If the multi-map map already contains a "presidents" key, map[presidents] returns a reference to the QStringList that corresponds to it; otherwise, an empty QStringList is automatically created. The other operations -- testing the presence of a (key, value) pair and removing a (key, value) pair -- are just as easy: if ( map["presidents"].contains("Bill Clinton") ) map["presidents"].remove( "Ronald Reagan" ); The remove() operation above will remove the pair ("presidents", "Ronald Reagan") from the map if the map contains it; otherwise it will do nothing. We can even insert the same (key, value) pair many times: map["presidents"].append( "George Bush" ); map["presidents"].append( "George Bush" ); Removing one of them is straightforward: map["presidents"].remove( map["presidents"].find("George Bush") ); In programming, as in politics, duplicate values aren't always welcome. They can easily be avoided in the first place: if ( !map["presidents"].contains("John Adams") ) map["presidents"].append( "John Adams" ); And they can easily be removed entirely: map["presidents"].remove( "John Adams" ); Copyright © 2002 Trolltech. Trademarks Seriously Weird QRegExp »