swap

How to replace specific values in a vector in C++?

这一生的挚爱 提交于 2019-11-30 23:38:49
I have a vector with some values (3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3), and I want to replace each 3 with a 54 or each 6 with a 1, for example and so on. So I need to go through the vector first, get the [i] value, search and replace each 3 with a 54, but still keep relevant positions.std::set is vector::swap a good way? I am not even sure how to begin this :( I can't use push_back as that would not keep the correct order of values as that is important. Please keep it simple; I am just a beginner :) The tool for the job is std::replace : std::vector<int> vec { 3, 3, 6, /* ... */ }; std::replace

CSS Variables - Swapping values?

夙愿已清 提交于 2019-11-30 21:51:28
I have a very simple problem with CSS variables. I would like to swap two CSS variables, basically the CSS equivalent of [a, b] = [b, a] in ES6. Here's a simple example: <p>White background</p> <button>Black background</button> <div> <p>Black background</p> <button>White background</button> </div> :root { --primary-color: #fff; --secondary-color: #000; } body { background-color: var(--primary-color); } button { background-color: var(--secondary-color); } div { /* i'd like to do the following: */ --primary-color: var(--secondary-color); --secondary-color: var(--primary-color); /* so here, `-

reorder byte order in hex string (python)

南楼画角 提交于 2019-11-30 19:12:48
I want to build a small formatter in python giving me back the numeric values embedded in lines of hex strings. It is a central part of my formatter and should be reasonable fast to format more than 100 lines/sec (each line about ~100 chars). The code below should give an example where I'm currently blocked. 'data_string_in_orig' shows the given input format. It has to be byte swapped for each word. The swap from 'data_string_in_orig' to 'data_string_in_swapped' is needed. In the end I need the structure access as shown. The expected result is within the comment. Thanks in advance Wolfgang R #

Swap arrays by using pointers in C++

亡梦爱人 提交于 2019-11-30 17:57:49
I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially just pointers, but the following code receives a compiler error: double left[] = {1,2,3}; double right[] = {9,8,7}; double * swap = left; left = right; // Error "ISO C++ forbids assignment of arrays" right = swap; // Error "incompatible types in assignment of `double*' to `double[((unsigned int)((int)numParameters))]'" Creating the arrays dynamically

CSS Variables - Swapping values?

痞子三分冷 提交于 2019-11-30 17:50:01
问题 I have a very simple problem with CSS variables. I would like to swap two CSS variables, basically the CSS equivalent of [a, b] = [b, a] in ES6. Here's a simple example: <p>White background</p> <button>Black background</button> <div> <p>Black background</p> <button>White background</button> </div> :root { --primary-color: #fff; --secondary-color: #000; } body { background-color: var(--primary-color); } button { background-color: var(--secondary-color); } div { /* i'd like to do the following:

What's the point of iter_swap?

佐手、 提交于 2019-11-30 16:49:37
I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap , and of course it only uses swap instead of std::swap since we're already in namespace std , anyway. That leads me to the next question: Why would anybody write this: using std::swap; swap(a, b); instead of this? std::iter_swap(&a, &b); // saved an entire line of code! Are there any important differences/issues I am overlooking here? From the SGI docs ( here ): [1] Strictly speaking, iter_swap is redundant.

Is there a better way to reverse an array of bytes in memory?

纵然是瞬间 提交于 2019-11-30 12:35:19
typedef unsigned char Byte; ... void ReverseBytes( void *start, int size ) { Byte *buffer = (Byte *)(start); for( int i = 0; i < size / 2; i++ ) { std::swap( buffer[i], buffer[size - i - 1] ); } } What this method does right now is it reverses bytes in memory. What I would like to know is, is there a better way to get the same effect? The whole "size / 2" part seems like a bad thing, but I'm not sure. EDIT: I just realized how bad the title I put for this question was, so I [hopefully] fixed it. The standard library has a std::reverse function: #include <algorithm> void ReverseBytes( void

浅谈linux性能调优之二:优化swap分区

泄露秘密 提交于 2019-11-30 11:50:34
先说说什么是swap分区以及它的作用? Swap分区,即交换区,Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序 使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢 复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。 其实,Swap的调整对Linux服务器,特别是Web服务器的性能至关重要。通过调整Swap,有时可以越过系统性能瓶颈,节省系统升级费用。 分配太多的Swap空间会浪费磁盘空间,而Swap空间太少,则系统会发生错误。 如果系统的物理内存用光了,系统就会跑得很慢,但仍能运行;如果Swap空间用光了,那么系统就会发生错误。例如,Web服务器能根据不同的请求数量衍生 出多个服务进程(或线程),如果Swap空间用完,则服务进程无法启动,通常会出现“application is out of memory”的错误,严重时会造成服务进程的死锁。因此Swap空间的分配是很重要的。 通常情况下,Swap空间应大于或等于物理内存的大小,最小不应小于64M,通常Swap空间的大小应是物理内存的2-2.5倍。但根据不同的应用,应有 不同的配置:如果是小的桌面系统,则只需要较小的Swap空间

Swap two values in a numpy array.

我与影子孤独终老i 提交于 2019-11-30 11:14:05
Is there something more efficient than the following code to swap two values of a numpy 1D array? input_seq = arange(64) ix1 = randint(len(input_seq)) ixs2 = randint(len(input_seq)) temp = input_seq[ix2] input_seq[ix2] = input_seq[ix1] input_seq[ix1] = temp Ffisegydd You can use tuple unpacking. Tuple unpacking allows you to avoid the use of a temporary variable in your code (in actual fact I believe the Python code itself uses a temp variable behind the scenes but it's at a much lower level and so is much faster). input_seq[ix1], input_seq[ix2] = input_seq[ix2], input_seq[ix1] I have flagged

Android: ArrayList Move Item to Position 0

随声附和 提交于 2019-11-30 10:05:14
I have an ArrayList and I need to make sure a specific item is at the 0 position and if it is not, I need to move it there. The item has an isStartItem boolean on it, so I can easily find the specific item I need to be in position 0 but then how do I go about moving it to the right position? I am assuming I need to use something like this: for(int i=0; i<myArray.size(); i++){ if(myArray.get(i).isStartItem()){ Collection.swap(myArray, i, 0); } } But this does not seem to work... You need to use Collections class's swap method. Collections , with an s at the end. Change - Collection.swap(myArray