1

Project should give as random number but that is not important, then that number of random find in first map and add into second map.

int rand = 2;
QPixmap pixmap1 = QPixmap (":/imag/sedam_one.jpg");
QPixmap pixmap2 = QPixmap (":/imag/gedam_one.jpg");
QPixmap pixmap3 = QPixmap (":/imag/tedam_one.jpg");
QMap<int, QPixmap> map;
map.insert(1, pixmap1);
map.insert(2, pixmap2);
map.insert(3, pixmap3);
QMap<int, QPixmap> myMap;
myMap.insert(map.key(rand), map.value(rand));
mario
  • 111
  • 2
  • 3
  • 8

3 Answers3

0

Your code will be different based on the behavior you want when rand is not a valid key.

  1. If you want to ignore the key if it is not there in map, you would use:

    if ( map.find(rand) != map.end() )
    {
      myMap.insert(map.key(rand), map.value(rand));
    }
    
  2. If you want a default-constructed value when the key is not there map, you would not create the if check from the above code and simply use the code that you have:

    myMap.insert(map.key(rand), map.value(rand));
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    @mario, if you think your solution will be useful to SO, please post it as an answer. Otherwise, delete your question. – R Sahu Sep 03 '14 at 15:15
0

Here's one way to do that:

  int rand = 2;
 QPixmap pixmap1 = QPixmap (":/imag/sedam_one.jpg");
 QPixmap pixmap2 = QPixmap (":/imag/gedam_one.jpg");
 QPixmap pixmap3 = QPixmap (":/imag/tedam_one.jpg");
 QMap<int, QPixmap> map;
 map.insert(1, pixmap1);
 map.insert(2, pixmap2);
 map.insert(3, pixmap3);
 QMap<int, QPixmap> myMap;
 myMap.insert(rand, map.value(rand));

Notice the last line. map.key(rand) should be just rand because the method map.key() requires that you put in a value such as a QPixmap

mike510a
  • 2,102
  • 1
  • 11
  • 27
0

Warning: This will overwrite values. You can use a QMultiMap if you wish to have multiple keys.

QMap<QString,QString> sneed;
QMap<QString,QString> feed;

QMapIterator<QString,QString> i_Map(feed);
while ( i_Map.hasNext() ) {
        i_Map.next();
        sneed.insert( i_Map.key(), i_Map.value() );
}
Anon
  • 2,267
  • 3
  • 34
  • 51