bartekltg <bartekltg@gmail.com>: Oct 10 01:59AM +0200 On 09.10.2015 20:38, Lynn McGuire wrote: > http://stackoverflow.com/questions/326062/in-stl-maps-is-it-better-to-use-mapinsert-than > I have a std::map where insert works better than []. It works in different ways. map<int,int> M; M.insert(make_pair(2,44)); cout<<M[2]<<endl; M.insert(make_pair(2,55)); cout<<M[2]<<endl; M[2]=66; cout<<M[2]<<endl; The output is: 44 44 66 > The code compiles > but crashes in the 2nd or 2rd copy constructor call with a null right > hand side. Your code contain an error. >"Strictly speaking, this member function is unnecessary: it exists > only for convenience." Strictly speaking, the for-loop is unnecessary, (while-loop is enough), it exist only for convenience. ;-) bartekltg |
Lynn McGuire <lmc@winsim.com>: Oct 09 08:48PM -0500 On 10/9/2015 2:19 PM, Paavo Helde wrote: > There is a bug in your code. > Cheers > Paavo Yes, there was a bug having to do with nulls in the copy constructor. Thanks, Lynn |
Lynn McGuire <lmc@winsim.com>: Oct 09 08:50PM -0500 On 10/9/2015 6:59 PM, bartekltg wrote: > Strictly speaking, the for-loop is unnecessary, (while-loop is > enough), it exist only for convenience. ;-) > bartekltg Here is my code: // find the first unused integer key and use it if ( ! found) { m_FormsMainCacheIndex = 0; while ( g_FormsMainCache.find (m_FormsMainCacheIndex) != g_FormsMainCache.end () ) m_FormsMainCacheIndex++; // In STL maps, is it better to use map::insert than []? // http://stackoverflow.com/questions/326062/in-stl-maps-is-it-better-to-use-mapinsert-than g_FormsMainCache.insert ( std::pair <int, FormsMain> (m_FormsMainCacheIndex, * FmWindow) ); } Thanks, Lynn |
Ian Collins <ian-news@hotmail.com>: Oct 10 03:00PM +1300 Lynn McGuire wrote: > // http://stackoverflow.com/questions/326062/in-stl-maps-is-it-better-to-use-mapinsert-than > g_FormsMainCache.insert ( std::pair <int, FormsMain> (m_FormsMainCacheIndex, * FmWindow) ); > } A good example why variable name prefixes make code hard to read! In this case, you have already determined the item does not exist in the map. So the clarity over potential premature optimisation rule should apply. Just write (with the suffixes if you really insist) FormsMainCache[FormsMainCacheIndex] = *FmWindow; -- Ian Collins |
Paavo Helde <myfirstname@osa.pri.ee>: Oct 10 02:22AM -0500 > g_FormsMainCache.insert ( std::pair <int, FormsMain> > (m_FormsMainCacheIndex, * FmWindow) ); > } The complexity of your algorithm is O(N*log(N)). A better approach would be just iterate over the map,this would be just O(N): int index = 0; std::map<int, FormsMain>::iterator iter = cache.begin(); while(true) { if (iter==cache.end() || iter->first!=index) { cache[index] = *FmWindow; break; } else { ++iter; ++index; } } Another option would be to use insert with a hint instead of [] cache.insert(iter, std::make_pair(index, *FmWindow)); This would avoid a bit of meaningless work (we already have located the place where the insert should happen), but OTOH it can be considered as a premature optimization as it would not change the overall complexity. hth Paavo |
Paavo Helde <myfirstname@osa.pri.ee>: Oct 10 02:42AM -0500 Ian Collins <ian-news@hotmail.com> wrote in >> (m_FormsMainCacheIndex, * FmWindow) ); >> } > A good example why variable name prefixes make code hard to read! The main problem with these names is even not the prefixes, but that they differ only in some 25% of letters. IOW, the signal/noise ratio of these names is pretty low. |
bartekltg <bartekltg@gmail.com>: Oct 10 10:46PM +0200 On 10.10.2015 03:50, Lynn McGuire wrote: >> enough), it exist only for convenience. ;-) >> bartekltg > Here is my code: Did you read Victor's Bazarov post? He posted this link. http://www.dietmar-kuehl.de/mirror/c++-faq/how-to-post.html#faq-5.8 > g_FormsMainCache.insert ( std::pair <int, FormsMain> > (m_FormsMainCacheIndex, * FmWindow) ); > } What can I do with this code? I can't run it. I even dont know what "FmWindow" is and why you dereferenced it. I do not see anything wrong. Error is probably somewhere else:) And I think you still haven't posted error message from your compiler. bartekltg |
bartekltg <bartekltg@gmail.com>: Oct 10 11:03PM +0200 On 10.10.2015 09:22, Paavo Helde wrote: > This would avoid a bit of meaningless work (we already have located the > place where the insert should happen), but OTOH it can be considered as a > premature optimization as it would not change the overall complexity. I'm afraid OP should rethink a big part of this code. If he has a lot of windows, and this operation is called often, it will be quadratic. If we do not care about indexes, it can by done in similar way like in a pool. Just a vector of union of an integer (or size_t) and our type; and an index first_index pointing to the first free element in the vector. "Occupied" elementd just contain a type, free elements points at the next free elements. Inserting and deleting has constant running time, and we use much less memory than in a map. bartekltg |
kizob123@gmail.com: Oct 10 08:38AM -0700 On Tuesday, April 29, 2003 at 6:58:00 PM UTC-7, Jesse James wrote: > appears in the word, how many times the letter x appears in the word, and so > on. When the user is finished with one word, the program should allow him or > her to enter another word. hello. h |
red floyd <no.spam.here@its.invalid>: Oct 10 01:26PM -0700 On 10/10/2015 8:38 AM, kizob123@gmail.com wrote: [blatant "do my homework for me" redacted] Your answer may be found here: https://www.cs.rit.edu/~mjh/docs/c++-faq/how-to-post.html#faq-5.2 Please show what you have done so far. Do not expect us to do your assignment for you. |
bartekltg <bartekltg@gmail.com>: Oct 10 10:38PM +0200 On 10.10.2015 22:26, red floyd wrote: > https://www.cs.rit.edu/~mjh/docs/c++-faq/how-to-post.html#faq-5.2 > Please show what you have done so far. Do not expect us to do > your assignment for you. His homework was to go to the usenet and write a post. He has failed because he has answered 'hallo' to a post from 2003 about c++ homework;-) bartekltg |
Bob Langelaan <bobl0456@gmail.com>: Oct 09 08:44PM -0700 If I have the following code: int var2 = 12; int var3 = 20; cout << '\n' << var2++ << " " << ++var2; cout << '\n' << var2; cout << "\n\n" << --var3 << " " << var3--; cout << '\n' << var3; I expect the output: 12 14 14 19 19 18 But with Microsoft Visual Studio 2013 I get instead: 13 14 14 18 20 18 I apologize if this question has been posted before. Thanks, Bob |
Paavo Helde <myfirstname@osa.pri.ee>: Oct 10 02:56AM -0500 Bob Langelaan <bobl0456@gmail.com> wrote in news:e97438d7-404f-4a80-bc21- > cout << '\n' << var2; > cout << "\n\n" << --var3 << " " << var3--; > cout << '\n' << var3; The behavior is undefined, anything can happen. If instead of an int a class with user-defined ++ and -- operators was used, then the behavior would be just unspecified and there would be a couple of possible outputs, depending on which arguments happen to be evaluated first. IOW: don't write such code. |
ram@zedat.fu-berlin.de (Stefan Ram): Oct 10 04:24AM >cout << '\n' << var2++ << " " << ++var2; »Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.« - 1.9p15 This means that it is not specified which »++« is executed first. »If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, and they are not potentially concurrent, the behavior is undefined.« - 1.9p15 This means that any behavior of the implementation is permitted. »Two actions are potentially concurrent if they are performed by different threads, or they are unsequenced, and at least one is performed by a signal handler.« - 1.10p23 |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment