swap

C++ trying to swap values in a vector

為{幸葍}努か 提交于 2020-01-18 21:06:10
问题 This is my swap function: template <typename t> void swap (t& x, t& y) { t temp = x; x = y; y = temp; return; } And this is my function (on a side note v stores strings) call to swap values but whenever I try to call using values in a vector I get an error. I'm not sure what I'm doing wrong. swap(v[position], v[nextposition]); //creates errors 回答1: I think what you are looking for is iter_swap which you can find also in <algorithm> . all you need to do is just pass two iterators each pointing

Swapping element in an array

人走茶凉 提交于 2020-01-15 03:16:09
问题 I've been trying to work this out: Say I have an array: int[] n = {0, 0, -1, 1, 0, 1, 1, -1, 1}; I need to be able to sort through the array and if there is a zero with a non zero preceding it, then they should be swapped. For example: 0, 0, -1, 1, 0, 1, 1, -1, 1 will become: 0, 0, -1, 0, 1, 1, 1, -1, 1 I have been trying to do it using a for loop and if statements with no luck. Any tips? 回答1: Try this: for (int i = 1 ; i < n.length ; i++) if (n[i] == 0 && n[i - 1] != 0) { int tmp = n[i - 1];

Swapping element in an array

谁说胖子不能爱 提交于 2020-01-15 03:16:07
问题 I've been trying to work this out: Say I have an array: int[] n = {0, 0, -1, 1, 0, 1, 1, -1, 1}; I need to be able to sort through the array and if there is a zero with a non zero preceding it, then they should be swapped. For example: 0, 0, -1, 1, 0, 1, 1, -1, 1 will become: 0, 0, -1, 0, 1, 1, 1, -1, 1 I have been trying to do it using a for loop and if statements with no luck. Any tips? 回答1: Try this: for (int i = 1 ; i < n.length ; i++) if (n[i] == 0 && n[i - 1] != 0) { int tmp = n[i - 1];

Jquery Mouseenter/Mouse Leave image swap

时光总嘲笑我的痴心妄想 提交于 2020-01-14 03:18:13
问题 So I wrote this little bit of code to try out some new way of doing an image swap for purposes of preloading and I am having a bit of trouble. My problem is that I have a container with the images that has some padding and text, but the activation of my rollover only happens when someone rolls over the image, instead of the container. I must have some small bit wrong, hopefully someone can help me out. Still learning! Here is the html: <div class="projectThumb"> <img src="/img/aeffect_button

How do I make a constexpr swap function?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 00:31:48
问题 I'm making my own String View class for learning purposes, and I'm trying to make it 100% constexpr. To test it, I have a member function that returns an hash value. I then construct my string view in a switch statement and call that same member function, if it passes, that member function has fullfiled its purpose. To learn, I'm using / reading / comparing my implementation with Visual Studio 2017 latest update std::string_view , however, I've noticed that, despite swap being marked as

Swap button for image (Jquery)

流过昼夜 提交于 2020-01-13 12:18:21
问题 I have a button and when it's clicked, I want to replace the button with an image. How can I do this in JQuery? Is it possible to replace the background of the image as well? The button itself is within a large div, and I don't want to add another div around the button because it messes up a previous layout. 回答1: If you want to replace the button element: $('the-button').bind('click', function () { $(this).replaceWith('<img src="/wherever.jpg"/>'); }); If you want to change the background

Swap button for image (Jquery)

人盡茶涼 提交于 2020-01-13 12:16:33
问题 I have a button and when it's clicked, I want to replace the button with an image. How can I do this in JQuery? Is it possible to replace the background of the image as well? The button itself is within a large div, and I don't want to add another div around the button because it messes up a previous layout. 回答1: If you want to replace the button element: $('the-button').bind('click', function () { $(this).replaceWith('<img src="/wherever.jpg"/>'); }); If you want to change the background

Swap method with const members

白昼怎懂夜的黑 提交于 2020-01-13 09:01:09
问题 I want to implement a Swap() method for my class (let's call it A) to make copy-and-swap operator=(). As far as I know, swap method should be implemented by swapping all members of the class, for example: class A { public: void swap(A& rhv) { std::swap(x, rhv.x); std::swap(y, rhv.y); std::swap(z, rhv.z); } private: int x,y,z; }; But what should I do if I have a const member? I can't call std::swap for it, so I can't code A::Swap(). EDIT: Actually my class is little bit more complicated. I

Why are there so many specializations of std::swap?

旧巷老猫 提交于 2020-01-13 07:36:07
问题 While looking at the documentation for std::swap, I see a lot of specializations. It looks like every STL container, as well as many other std facilities have a specialized swap. I thought with the aid of templates, we wouldn't need all of these specializations? For example, If I write my own pair it works correctly with the templated version: template<class T1,class T2> struct my_pair{ T1 t1; T2 t2; }; int main() { my_pair<int,char> x{1,'a'}; my_pair<int,char> y{2,'b'}; std::swap(x,y); } So

How to add std::swap for my template class? [duplicate]

三世轮回 提交于 2020-01-13 03:03:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: how to provide a swap function for my class? There are some questions about this, but a lot of contradictions (person A giving solution A' with many upvotes with person B saying it's UB) or "only works if the compiler supports ADL" is answered. So, say I have the following template (container) class: template<typename T> class C { // ... void swap(C<T>& y) throw(); // C x; x.swap(y); } then what is the correct