Thread: copying differents maps

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    >> Wow, horrible code for something that should be something so easy. <<
    This type of comment is useless (or worse) without an explanation or other example. Although if you'd care to start a discussion about it I'd hope it doesn't derail the thread.
    I changed the code (see above, red color).
    It's quite enlightening to clearly see what's going on.
    I had a very difficult time trying to understand the code cpjust originally posted. Splitting it like that helped a lot.
    (I hope it is of benefit to someone.)
    Last edited by Elysia; 10-13-2008 at 02:17 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    The sorted vector idea works best if you add all your elements at the start, then sort them (and make them unique if necessary) before using them. If the data must be unique, it works best if you don't have a lot of duplicates.

    If you do have a lot of duplicates, then adding everything to the vector first could waste a lot of space (although you might be able to make the data unique in stages to reduce that waste).

    If you don't insert all the elements at the beginning, and instead do it as you go (meaning you insert some elements, then use the vector, then insert some more, then use the vector, etc), then a vector is a bad idea because you would either have to insert into the middle (which is costly) or sort the container and make it unique each time you add something. Both options might be slower than using the set and then at the end copying to a vector, but it sounds like this might not be a problem for you if you're just filling the container before sending it.
    All the elements are added at the beginning (or in rare occasions such as if configuration file is reloaded). So then it shouldnt be a problem using a vector (which would after sort and erasing duplicates operations store only unique elements) and sending it over to the clients. From your post above I guess this would be the best solution?

    Many thanks!

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> From your post above I guess this would be the best solution? <<

    It depends on how many duplicates. If 90&#37; of the data is a duplicate, then perhaps not. But if 10% of the data is a duplicate, then yes, it sounds pretty good.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I changed the code (see above, red color). <<

    Did you only change the spacing? Calling something "horrible code" implies that more than just formatting is bad. And BTW, I prefer cpjust's formatting when actually writing such code (although not when posting to the forum).

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, how's this formatting instead?
    Code:
    	for ( mapStrSet::iterator it = map_str_set.begin(); it != map_str_set.end(); ++it )
    	{
    		map_str_vector.insert( mapStrVector::value_type( it->first,
    								 std::vector<std::string>( it->second.begin(),
    											   it->second.end() ) ) );
    	}
    That's usually how I'd format it when I'm not in a hurry.

    I just wish this site used tabs that were 4 spaces wide, so I wouldn't have to fix it when copying from VC++ to here.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    >> I changed the code (see above, red color). <<

    Did you only change the spacing? Calling something "horrible code" implies that more than just formatting is bad. And BTW, I prefer cpjust's formatting when actually writing such code (although not when posting to the forum).
    Yeah, I meant that it's so much complexity for something that sounds so easy. That's what I meant with horrible.
    Btw, I contracted the spaces to make it more compact.

    Quote Originally Posted by cpjust View Post
    OK, how's this formatting instead?
    Code:
    	for ( mapStrSet::iterator it = map_str_set.begin(); it != map_str_set.end(); ++it )
    	{
    		map_str_vector.insert( mapStrVector::value_type( it->first,
    								 std::vector<std::string>( it->second.begin(),
    											   it->second.end() ) ) );
    	}
    That's usually how I'd format it when I'm not in a hurry.
    I think it's slightly better, but I prefer to separate the arguments myself.
    Since insert is a function, I put its arguments on new lines.
    And if there's just one or max two arguments dependant on each other, I put them all on a single row.
    It makes it so much easier to see what ) and ( which belongs to where and what arguments function X and function Y takes.

    I just wish this site used tabs that were 4 spaces wide, so I wouldn't have to fix it when copying from VC++ to here.
    Now there's something we agree on!
    Btw, I never really commented on your style. You are one of the few who write code the way I do and like
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading maps
    By divineleft in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2006, 10:09 AM
  2. Hash_map, STL maps query
    By alvifarooq in forum C++ Programming
    Replies: 15
    Last Post: 06-07-2005, 09:07 AM
  3. Shallow/Deep copying, pointers
    By littleweseth in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 06:36 PM
  4. Finding terrain maps...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-25-2001, 07:44 PM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM