Range of QVector - qt

I am wondering how can I find the range between two elements in QVector using c++.
When using C# it's easier, and looks like following:
QVector aaa;
aaa.getRange(item1, item2);

Your question is not very clear. By googling what .NET's getRange actually does, it seems to return given count of elements from given starting position. QVector<T> QVector::mid(int pos, int length = -1); does the same with QVector.

Related

is there a better way to append a QVector for 'zero padding'?

I have a QVector (v) of length (n), which has multiple values already in it. I want to double the length of this QVector and populate the second half with 0s. Is there a better way to do this than:
for (int i=0;i<n;i++) {
v.append(0);
}
It just feels inefficient/inelegant to be calling append in a loop
Thanks in advance...
Solution
Simply double the length by calling v.resize( 2*v.length() ). That's all.
Explanation
The documentation says that QVector<T>::resize() initializes the new elements with a default-constructed value. When T is a numeric type, the default value is 0.

Convert af::array to QVector

I am looking for a way to convert a image, which is represented (because I used ArrayFire function loadimage()) as af::array to QVector. Any advice on how can I do this?
Thank you!
Let a be you af::array. Provided you can retrieve the pointer to the actual data via a.host() and the number of array elements with a.bytes(), the question reduces to "How can I convert a C array to a QVector.
That is already answered here: Initialize QVector from array

What is the Arduino equivalent of C#'s Mathf.pingpong?

In C#, more specifically in Unity, there is a a method called Mathf.PingPong. What is the equivalent for Arduino?
I haven't used Unity, but if I understand the definition of that function correctly, you can use the modulo operator.
int pingpong(int t, int length)
{
return t % length;
}
You can probably use fmod, if you need floating point numbers.
Edit: I assume you mean in C when you are talking about arduino.

Rust - Vector with size defined at runtime

How do you create an array, in rust, whose size is defined at run time?
Basically, how do you convert in rust the following code:
void f(int n){ return std::vector<int>(n); }
?
This is not possible in rust:
let n = 15;
let board: [int, ..n];
Note: I saw that it was impossible to do this in a simple manner, here, but I refuse to accept that such a simple thing is impossible :p
Thanks a lot!
Never-mind, I found it the way:
let n = 15; // number of items
let val = 17; // value to replicate
let v = std::vec::from_elem(val, n);
The proper way in modern Rust is vec![value; size].
Values are cloned, which is quite a relief compared to other languages that casually hand back a vector of references to the same object. E.g. vec![vec![]; 2] creates a vector where both elements are independent vectors, 3 vectors in total. Python's [[]] * 2 creates a vector of length 2 where both elements are (references to) the same nested vector.

Use pointer to a value as a slice

Is it possible to convert a pointer to certain value to a slice?
For example, I want to read single byte from io.Reader into uint8 variable. io.Reader.Read accepts a slice as its argument, so I cannot simply provide it a pointer to my variable as I'd do in C.
I think that creating a slice of length 1, capacity 1 from a pointer is safe operation. Obviously, it should be the same as creating a slice from an array of length 1, which is allowed operation. Is there an easy way to do this with plain variable? Or maybe I do not understand something and there are reasons why this is prohibited?
A slice is not only a pointer, like an array in C. It also contains the length and capacity of the data, like this:
struct {
ptr *uint8
len int
cap int
}
So, yes, you will need to create a slice. Simplest way to create a slice of the var a uint8 would be []uint8{a}
a := uint8(42)
fmt.Printf("%#v\n", []uint8{a})
(But after rereading your question, this is not a solution as all)
But if you wish to create the slice from the variable, pointing to the same space of memory, you could use the unsafe package. This is most likely to be discouraged.
fmt.Printf("%#v\n", (*[1]uint8)(unsafe.Pointer(&a))[:] )
Instead of (over)complicating this trivial task, why not to use the simple solution? I.e. pass .Read a length-1 slice and then assign its zeroth element to your variable.
I found a way to overcome my case when I want to supply a variable to io.Reader. Go standard library is wonderful!
import (
"io"
"encoding/binary"
)
...
var x uint8
binary.Read(reader, LittleEndian, &x)
As a side effect this works for any basic type and even for some non-basic.

Resources