Results 1 to 13 of 13

Thread: map question

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default map question

    Hi, it happen to me this thing: I create lightmap[4] element and dimensione map became 4; is I do lightmap[6] = Light(); then 6 element...why this?
    Qt Code:
    1. map<int,Light> lightmap;
    2. lightmap[4] = Light();
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map question

    Quote Originally Posted by mickey
    Hi, it happen to me this thing: I create lightmap[4] element and dimensione map became 4; is I do lightmap[6] = Light(); then 6 element...why this?
    Qt Code:
    1. map<int,Light> lightmap;
    2. lightmap[4] = Light();
    To copy to clipboard, switch view to plain text mode 
    I don't understand what you are asking.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map question

    Qt Code:
    1. lightmap[4] = light();
    2. int i = lightmap.size();
    3. printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...
    To copy to clipboard, switch view to plain text mode 
    I thought lightmap[4] = light(); create an element with index <int=4, > ...
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map question

    Quote Originally Posted by mickey
    Qt Code:
    1. lightmap[4] = light();
    2. int i = lightmap.size();
    3. printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...
    To copy to clipboard, switch view to plain text mode 
    I thought lightmap[4] = light(); create an element with index <int=4, > ...
    I am still not completely understanding you. If you mean that it is creating them element lightmap[4] then yes it does do that. If you are mean that it should create elements lightmap[0] through lightmap[4] then no it does not do that.

    lightmap.size() only returns how many elements the map has, not the key of any one particular element. So, if you did the following:
    Qt Code:
    1. map<int, Light> lightmap;
    2. lightmap[3] = light();
    3. lightmap[4] = light();
    4. lightmap[5] = light();
    5. int i = lightmap.size();
    6. printf("ADDING LIGHT dimensione <map> %i\n" ,i);
    To copy to clipboard, switch view to plain text mode 
    The output would be 3.

    A map is declared as "map <key type, value type, ...>" where the key type is the type of key you will be using. You can then use the brackets [] to get or set the value at map element [key]. So when you call lightmap[4] you are NOT telling it to create lightmap[0] through lightmap[4], you are simply saying I am holding this value at element location named 4 in the map named lightmap. You could have declared your map as:
    map<string, Light> lightmap;

    And changed
    lightmap[4] = light();
    to
    lightmap["Four"] = light();

    Does this help?

  5. #5
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map question

    Quote Originally Posted by mickey
    Qt Code:
    1. lightmap[4] = light();
    2. int i = lightmap.size();
    3. printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...
    To copy to clipboard, switch view to plain text mode 
    I thought lightmap[4] = light(); create an element with index <int=4, > ...
    The `i` should be 1, and I cann't reproduce your problem...
    here's my code
    Qt Code:
    1. class A
    2. {
    3. };
    4. int main()
    5. {
    6. map<int,A> m;
    7. m[4]=A();
    8. cout<<m.size();
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    and it prints `1`
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map question

    my problem probabily was another:
    Qt Code:
    1. map<int, Light> lightmap;
    2. lightmap[3] = light();
    3. lightmap[4] = light();.
    4. lightmap[5] = light();
    5. int i = lightmap.size();
    6. printf("ADDING LIGHT dimensione <map> %i\n" ,i); "ADDING LIGHT dimensione <map> %i\n" ,i); //this should print 3
    To copy to clipboard, switch view to plain text mode 
    now I need to to a thing (but I dont know how):
    Qt Code:
    1. int size = lightmap.size();
    2. for (int i=0; i<size; i++) {
    3. lightmap[i].DOSomething();
    4. }
    To copy to clipboard, switch view to plain text mode 
    here above I need to put in a for a my Light class function; but the for is working on elements 0,1,2; I need to use map with KEY 3,4,5 (they're iserted before).
    Are u understand? thanks
    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: map question

    Qt Code:
    1. for(QMap<int, Light>::iterator iter = lightmap.begin(); iter!=lightmap.end();++iter){
    2. iter.data().doSomething();
    3. }
    To copy to clipboard, switch view to plain text mode 

    The same goes with std::map, just the iterator may behave different (just look at its docs to see what member you need to access). It'll probably be "iter->doSomething()".
    Last edited by wysota; 7th June 2006 at 08:30.

  8. The following user says thank you to wysota for this useful post:

    mickey (10th June 2006)

  9. #8
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Wink Re: map question

    wysota's right, just use the`iterator`

    And seems your English's poor
    Last edited by bood; 8th June 2006 at 05:14.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  10. #9
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: map question

    Quote Originally Posted by bood
    And seems your English's poor
    And it seems your English's poor.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  11. The following user says thank you to Michiel for this useful post:

    mickey (10th June 2006)

  12. #10
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face Re: map question

    Quote Originally Posted by Michiel
    And it seems your English's poor.
    I thought it can be ignored in oral English...can't it?
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  13. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: map question

    Are we speaking?
    Regards

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: map question

    Stop the flame, please.

  15. #13
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: map question

    Quote Originally Posted by bood
    I thought it can be ignored in oral English...can't it?
    Not if you prepend it with 'And'. Anyway, what relevance does it have? As long as it's not 1337-speak and we can understand each other...
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  2. Replies: 6
    Last Post: 19th March 2006, 18:41
  3. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.