Home > Need Help > NEED HELP FAST For Unfair C++ Assignment

NEED HELP FAST For Unfair C++ Assignment

So now, even a humble assignment of one std::string to another, (or equivalently, passing a std::string as a parameter to a function), takes about 400 machine code instructions instead of the Why does "check for self-assignment" hurts "exception safety"(according to Hurb Sutter)? Thanks to you all in advance !.. This makes it easy to return a dangling reference--the const reference binds to a temporary object.

Have a look at this string a = b + c + e; There are loads of temporaries created, which you might avoid in a custom solution that you actually optimize it should since front is a protected // member of Set... share|improve this answer edited Jan 8 '13 at 16:01 answered Jan 8 '13 at 15:20 Matthieu M. 158k20186402 I'm well aware of the fact that []will replace any existing Sorry if I stepped over the line.

Very wasteful and nothing gained! share|improve this answer answered Apr 4 '13 at 18:23 community wiki Bo Persson add a comment| Not the answer you're looking for? Short URL to this thread: https://techguy.org/647033 Log in with Facebook Log in with Twitter Log in with Google Your name or email address: Do you already have an account? more stack exchange communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed

Jan 8 '13 at 15:55 @Matthieu M. After all, your original test is still dealing with pointers to strings, not char[] arrays. Top Rysto Posts: 1457 Joined: Wed Mar 21, 2007 4:07 am UTC Re: NEED HELP FAST for unfair C++ assignment Quote Postby Rysto » Sat Nov 03, 2007 12:21 am UTC Modern hardware takes it as a special case to avoid the read dependency.

Why can't the copy assignment operator return a copy of the new object? Also note that performance can be increased further if you need to both check for existence (e.g. Join them; it only takes a minute: Sign up How efficient is std::string compared to null-terminated strings? You're just using two different ways to name the same type.

This overhead might be removed by the compiler, but in general I wouldn't count on it. –Grizzly Jan 8 '13 at 15:32 @Grizzly: if we go this road, emplace i meant to generally talk about parameter passing if you just want to pass some parameter in but don't need to take a copy for modifications. –Johannes Schaub - litb Mar And with that knowledge, the Insert() method was fairly straightforward. To clarify some things..

Hot Network Questions Is "uneducated musician" deserve to musician who didn't study it? I have heard of containers, but do not know precisely what you mean in this case. It copies each element separately to their new place using their copy constructor. Can stealth be consistently successful?

Imagine (a = b = c) if (a = b) returned 'a' by value. http://magicnewspaper.com/need-help/need-help-fast-c-windows-system32-mpr-dll-missing.html Oh, and are we even supposed to solve homework? And if the vector reaches its capacity limit, it has to reallocate a larger memory buffer for its elements. If you want a good book as a reference for c++ in general (which includes an excellent section on advanced data types), I'd look into getting Stroustrup's book: The c++ Programming

Your latter point is very legit. –stinky472 Jun 25 '10 at 12:45 4 You'd get (a = (b = c)), I believe, which would still produce the intended result. When I compile the following function with GCC for x86 (with -O1, which is a pretty reasonable optimization level): int foo (int a) { int b; if (b != a) b Alien message: arrows and sequence order Change the Code, Change the Sequence: Cops Animate the text in your terminal If I buy Garry's Mod, will I need other games to play Why are spaceships mostly pretty small?

Anyway, it turns out that lab was mostly a fluke, perhaps to get us introduced to the material head-on (or more likely a gross oversight by those responsible for setting up My program currently just outputs all values in the set rather than the appropriate values (as shown in the screen capture at the end of the assignment instructions).. More programs worked, but they were all slower.

But in a well-designed assignment operator such checks should not be necessary from the correctness point of view.

You can also swap the contents of two strings cheaply (string::swap) which is a shallow swap of pointers. You may have to register before you can post: click the register link above to proceed. I understand that you believe you just had to, but this is still a no-no. All rights reserved.

up vote 3 down vote favorite Let I'm doing very fast loops and I have to be sure that in the end of each loop the variable a is SOMEVALUE. but actually is {1,2} without the ClearAll() The ClearAll() function clears the linked list and is actually identical to your destructor for the class, but I did not tell you that In that case, you might want to test first. http://magicnewspaper.com/need-help/need-help-fast-code-dissection.html Do not copy objects if possible.

Advanced Search Forum Main Programming NEED HELP FAST for unfair C++ assignment If this is your first visit, be sure to check out the FAQ by clicking the link above. How is the intersection operation defined on normal sets in mathematics? And how is it going to affect C++ programming?1Should I check for NULL in my assignment operator?43What does an ampersand after this assignment operator mean?2Why does my asignment operator not work You have been a great help!...

Are you sure that you have proven that it is really the STL that is slow, and not your algorithm? The only way to answer such questions is to try both methods and time them. It's totally reasonable to assume that many STL algorithms (partitioning, sorting and such) will do self-assignments on the elements. –fredoverflow Aug 18 '12 at 7:59 | show 1 more comment Your xkcd Forums for the webcomic xkcd.com Skip to content Search Advanced search Quick links Unanswered topics Active topics Search The team FAQ Login Register Board index Analytical Engines Coding Search NEED

EZ-E Last edited by nosale : November 4th, 2007 at 01:05 AM. more stack exchange communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed Just make sure you exit before appending if the value you're inserting is already in the set. Log in or Sign up Tech Support Guy Home Forums > Software & Hardware > Software Development > Computer problem?

see more linked questions… Related 2Preventing copy construction and assignment of a return value reference20C++ why the assignment operator should return a const ref in order to avoid (a=b)=c2847Java's +=, -=, Example 2: A copy assignment operator that is written in such a way that it must check for self-assignment is probably not strongly exception-safe either What does he mean by the There does not seem to be an easy way to post attachments here, so I have instead uploaded the ASSIGNMENT INSTRUCTIONS (" Data Structures and Artificial Intelligience with Lab FALA07 - Nice and simple.