What i wanna do is to convert a list like [1,3.0,5,5.5,8] to [1.0,3.0,5.0,5.5,8.0].
The problem is I don't know the list, and so I can't use float_of_int, because it wont work for elements that are already floats.
Your title says (in essence) (int * float) list. However your example lists (correcting for OCaml syntax) are impossible. There is no list in OCaml that contains some floats and some ints. All elements of a list must be the same type.
If you really have (int * float) list, they look like this:
[(1, 1.5); (2, 3.8); (3, 8.9)]
Every element is a pair with an int and a float. So it's easy to handle them all with the same code.
Related
I am relative new at C++ and I have little problem. I have vector and in that vector are vectors with 3 integers.
Inner vector represents like one person. 3 integers inside that inner vector represents distance from start, velocity and original index (because in input integers aren't sorted and in output I need to print original index not index in this sorted vector).
Now I have given some points representing distance from start and I need to find which person will be first at that point so I have been thinking that my first step would be that I would find closest person to the given point so basically I need to find lower_bound/upper_bound.
How can I use lower_bound if I want to find the lower_bound of first item in inner vectors? Or should I use struct/class instead of inner vectors?
You would use the version of std::lower_bound which takes a custom comparator (the versions marked "(2)" at the link); and you would write a comparator of vectors which compares vectors by their first item (or whatever other way you like).
Howerver:
As #doctorlove points out, std::lower_bound doesn't compare the vectors to each other, it compares them to a given value (be it a vector or a scalar). So it's possible you actually want to do something else.
It's usually not a good idea to keep fixed-length sequences of elements in std::vector's. Have you considered std::array?
It's very likely that your "vectors with 3 integers" actually stand for something else, e.g. points in a 3-dimensional geometric space; in which case, yes, they should be in some sort of class.
I am not sure that your inner things should be std::vector-s of 3 elements.
I believe that they should std::array-s of 3 elements (because you know that the size is 3 and won't change).
So you probably want to have
typedef std::array<double,3> element_ty;
then use std::vector<element_ty> and for the rest (your lower_bound point) do like in einpoklum's answer.
BTW, you probably want to use std::min_element with an explicit compare.
Maybe you want something like:
std::vector<element_ty> vec;
auto minit =
std::min_element(vec.begin(), vec.end(),
[](const element_ty& x, const element_ty&y) {
return x[0] < y[0]));
I would like to use the Accelerate Framework libraries for sorting data (pairs of x and y values).
I used the function vDSP_vsorti to find the vector with the ordering indices of the x data. Now I should reorder the y data accordingly to the x sorting indices order.
How I could do it? Does exist a function in Accelerate Framework to reorder the vector?
Can you use vDSP_vgathr? This APIs grabs values from a vector using another vector of indices.
https://developer.apple.com/library/mac/documentation/Accelerate/Reference/vDSPRef/index.html#//apple_ref/c/func/vDSP_vgathr
Here is a summary:
Single-Vector Gathering
The functions in this group use either indices or pointers stored within one source vector to generate a new vector containing the chosen elements from either a second source vector or from memory.
vDSP_vgathr
Vector gather; single precision.
Declaration
SWIFT
func vDSP_vgathr(_ __vDSP_A: UnsafePointer<Float>,
_ __vDSP_B: UnsafePointer<vDSP_Length>,
_ __vDSP_J: vDSP_Stride,
_ __vDSP_C: UnsafeMutablePointer<Float>,
_ __vDSP_K: vDSP_Stride,
_ __vDSP_N: vDSP_Length)
OBJECTIVE-C
void vDSP_vgathr ( const float *__vDSP_A, const vDSP_Length *__vDSP_B, vDSP_Stride __vDSP_IB, float *__vDSP_C, vDSP_Stride __vDSP_IC, vDSP_Length __vDSP_N );
Parameters
__vDSP_A
Single-precision real input vector
__vDSP_B
Integer vector containing indices
__vDSP_J
Stride for B
__vDSP_C
Single-precision real output vector
__vDSP_K
Stride for C
__vDSP_N
The number of elements to process
Discussion
Performs the following operation:
Uses elements of vector B as indices to copy selected elements of vector A to sequential locations in vector C. Note that 1, not zero, is treated as the first location in the input vector when evaluating indices. This function can only be done out of place.
Suppose I have some tuple on the stack:
let a:(u8,u8,u8) = (1,2,3);
How do I create a slice into all, or some of a?
Rust reference defines tuples as having contiguous layout and defined order, so you can take a pointer to the first element of a tuple and transform it to a slice:
#![feature(tuple_indexing)]
use std::slice;
fn main() {
let t = (1u8, 2u8, 3u8);
let f: *const u8 = &t.0;
let s = unsafe { slice::from_raw_buf(&f, 3) };
println!("{}", s); // [1, 2, 3]
}
There is also this RFC but it was closed quite some time ago.
In most cases it doesn't make sense to do this. The main distinction between a tuple and a fixed size array of the same size is that the tuple supports heterogeneous elements, while arrays contain elements of the same type. Slices are fat pointers to an array of values of the ~same type that are consecutive in memory, so while they might make sense for ~some tuples, they don't in general, and therefore slice operations are not supported on tuples.
I want to write a function that takes as an argument a list of three tuples.
What I have isn't passing the eval, as I assume that it's reading it as tuple of two ints plus an int list:
fun check_list(tuples : int*int*int list) = so forth and so forth
What is the proper syntax for typing a list of tuples?
The problem is in your syntax.
This int*int*int list is equivalent to int * int * (int list)
But what you meant is (int * int * int) list. The use of parentheses should make it clearer.
if you want a tuple of three ints then you would pass to a function like so:
fun check_list(a:int,b:int,c:int)=[(a,b,c)];
check_list(1,2,3);
plug it in and try it. I will return [(1,2,3)]. If I'm understanding your question correctly.
Nested list is a list of lists.
std::list<std::list<int> > nestedList;
How to create multi-dimension depth dynamicly list? For examle, user enters number 3, and then how dynamicly create 3-dimension depth nested list(list of list of lists...) ?
I know how to declare manual, but I need to create it dynamicly.
My go-to approach for simple problems like this is to allocate a vector and handle the dimensions through indexing.
For example, a 2D array of size WxH is std::vector<int> v(W*H);
Then access it with int& get(int x, int y){ return v[y * W + x]; }
You can extend this pattern to as many dimensions as you want, but it may not be suitable depending on your problem. Consider it an option.
You could either:
Predefine what the valid number of dimensions are so all of them can be compiled. Then use a switch and templated functions.
or
Write a new n-d list class.
The constructor would be called along the lines of :
size_t dimensions(3);
ndList<int> myNDList(dimensions);