- vectors of large objects - 5 Updates
- C++11/C++14 appreciation thread - 1 Update
- private static member variables - 1 Update
- private static member variables - 1 Update
Paul <pepstein5@gmail.com>: Jan 24 01:03PM -0800 Suppose we want to swap the first two elements of a vector<T> Suppose further that the type T is very large so we want to avoid copying. Suppose v is a vector<T> We can do T temp = v[0]; v[0] = v[1]; v[1] = temp; However, this seems to involve unnecessary copying. I therefore suggested that, for an operation that involves a lot of swapping (for example, a sort), we should use a vector of pointers to T rather than a vector<T> and swap via the pointers. However, someone (in a private conversation so I don't want to name the person) told me that T temp = v[0]; v[0] = v[1]; v[1] = temp; does not in fact move large objects around, and only moves pointers, so that my issue doesn't really exist. But it does exist, doesn't it? For example, T temp = v[0]; is copying a member of T and we don't want to do that. Can the issue be handled instead via references -- T& temp = v[0]; v[0] = v[1]; v[1] = temp; However, doesn't the line v[0] = v[1] involve an assignment of a large type which is likely to be expensive? Any ideas? Thanks, Paul |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jan 24 09:11PM On Sun, 2016-01-24, Paul wrote: > Suppose we want to swap the first two elements of a vector<T> > Suppose further that the type T is very large so we want to avoid > copying. What's wrong with std::swap(v[0], v[1]) ? /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Paul <pepstein5@gmail.com>: Jan 24 01:14PM -0800 On Sunday, January 24, 2016 at 9:11:51 PM UTC, Jorgen Grahn wrote: > > Suppose further that the type T is very large so we want to avoid > > copying. > What's wrong with std::swap(v[0], v[1]) ? Nothing. But I want to write the swap myself. Paul |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Jan 24 10:14PM > operation that involves a lot of swapping (for example, a sort), we > should use a vector of pointers to T rather than a vector<T> and swap > via the pointers. The word "via" bothers me. If you swap the pointers, yes, you gain that way. But you are not swapping via the pointers -- you are just swapping the pointers. > the person) told me that T temp = v[0]; v[0] = v[1]; v[1] = temp; does > not in fact move large objects around, and only moves pointers, so > that my issue doesn't really exist. The stuff about pointers sounds crazy enough that, if it were me, I'd want to to check the I'd understood the person. It's true that using a temporary need not do all that copying, because the compiler might know enough about the types to be able to optimise the code, but that's not really what your correspondent seems to have been saying. > But it does exist, doesn't it? For example, T temp = v[0]; is copying > a member of T and we don't want to do that. Can the issue be handled > instead via references -- T& temp = v[0]; v[0] = v[1]; v[1] = temp; Costs aside, that won't work. The first initialisation makes temp and v[0] be aliases -- two names for the same object. The second assignment (to v[1]) will simply copy what it in v[0] which you've already made a copy of v[1]. > However, doesn't the line v[0] = v[1] involve an assignment of a large > type which is likely to be expensive? If you want to swap two objects in any literal sense of the word, you must be prepared to pay the cost a reading them both and writing them both. You can make the cost only a very little more by doing the swap byte by byte, but that may not be faster than going via a whole extra copy to a temporary. With luck, the std::swap algorithm will be implemented in a way that makes it the best choice (I know you are asking about doing it yourself). -- Ben. |
"Öö Tiib" <ootiib@hot.ee>: Jan 24 02:18PM -0800 On Sunday, 24 January 2016 23:03:52 UTC+2, Paul wrote: > operation that involves a lot of swapping (for example, a sort), we > should use a vector of pointers to T rather than a vector<T> and swap via > the pointers. If you need sorted things then keep them in sorted containers. > the person) told me that T temp = v[0]; v[0] = v[1]; v[1] = temp; does not > in fact move large objects around, and only moves pointers, so that my > issue doesn't really exist. That is not correct. > However, doesn't the line v[0] = v[1] involve an assignment of a large > type which is likely to be expensive? > Any ideas? If the type 'T' has move assignment then you can write: T temp = std::move(v[0]); v[0] = std::move(v[1]); v[1] = std::move(temp); However 'std::swap(v[0],v[1])' does same. |
Vir Campestris <vir.campestris@invalid.invalid>: Jan 24 08:58PM On 23/01/2016 11:34, Alf P. Steinbach wrote: > avoids confronting a learner up-front with the (according to Kernighan > and Stroustrup) failed syntax experiment of C and early C++ While C syntax is a mess I don't think we can call it failed. I'm still writing it, 40 odd years later... though it does feel very painful knowing C++ is available. Andy |
Gareth Owen <gwowen@gmail.com>: Jan 24 07:56PM > I think that is a false argument, but whether you are right or wrong, > the response by Stefan was (in classic Stefan style) of no relevance to > the question raised by the original poster. You'll get no disagreement. The question is whether Stefan's classic irrelevance required Paavo's classic pedantry. |
ram@zedat.fu-berlin.de (Stefan Ram): Jan 24 02:02PM >Who is "we"? And no, these are not part of the class (they cannot access >private members of the class, for example). »We« is Herb Sutter who wrote: »For a class X, all functions, including free functions, that both (a) "mention" X, and (b) are "supplied with" X are logically part of X« on his web page www.gotw.ca/publications/mill02.htm . |
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