将UnsafeMutablePointer转换为UnsafePointer(Convert UnsafeMutablePointer to UnsafePointer)
我在我的Swift项目中使用了一个C库,其中一个函数需要一个
UnsafePointer<UnsafePointer<UInt8>?>!
作为一个内部参数,我应该传递我的数据。 但问题是我在UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>!
有这些数据UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>!
类型。我的问题是 - 在Swift 3
UnsafeMutablePointer
UnsafePointer
转换为UnsafePointer
的最有效和最简单的方法是什么? 这些指针在“纸上”并没有太大的不同,它们应该与技术背景没有太大的不同,但我无法找到关于该主题的有价值的信息。任何帮助,将不胜感激。 谢谢
I'm using a C library in my Swift project, and one of functions requires a
UnsafePointer<UnsafePointer<UInt8>?>!
as an in-out argument where I should pass my data. But the problem is that I have this data inUnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>!
type.My question is - What is the most efficient and easiest way to convert
UnsafeMutablePointer
toUnsafePointer
in Swift 3? Those pointers are not much different "on paper" and they shouldn't be much different from technical context, but I couldn't find valuable information on that topic.Any help would be appreciated. Thanks
原文:https://stackoverflow.com/questions/48915124
最满意答案
使用
<algorithm>
标头中的std::equal
函数:if (std::equal(v1.begin(), v1.begin() + n, v2.begin()) std::cout << "success" << std::endl;
请注意,两个向量必须至少有
n
元素。 如果任一个太短,您的程序的行为将不确定。如果你想检查整个向量是否等于另一个,只需比较它们,就像比较别的东西:
if (v1 == v2)
您的(失败)代码是将一个向量的迭代 器与另一个向量的迭代器进行比较。 相等向量的迭代器不相等。 每个迭代器被绑定到它所迭代的序列,所以一个向量的迭代器永远不会等于另一个向量的迭代器。
Use the
std::equal
function from the<algorithm>
header:if (std::equal(v1.begin(), v1.begin() + n, v2.begin()) std::cout << "success" << std::endl;
Note that both vectors must have at least
n
elements in them. If either one is too short, behavior of your program will be undefined.If you want to check whether the entire vector is equal to the other, just compare them like you'd compare anything else:
if (v1 == v2)
Your (failed) code was comparing an iterator of one vector with an iterator of the other. Iterators of equal vectors are not equal. Each iterator is tied to the sequence it's iterating, so an iterator from one vector will never be equal to the iterator of another.
相关问答
更多-
为什么这些矢量不相等?(Why aren't these vectors equal?)[2021-08-01]
vectorv1; v1.reserve(3); v1[0] = 0; v1[1] = 1; v1[2] = 2; 这可能是一个未定义的行为(尽管不确定它是否取决于实现)。 你不能使用operator[]来填充矢量,因为它将引用返回到底层对象,在你的情况下它只是一堆比特。 你应该使用push_back()或者只是resize你的vector.Using后者: - vector v1; v1.resize(3); v1[0] = 0; v1[1] = 1; v1[2] = 2; ve ... -
检查两个向量是否相等(Check if two vectors are equal)[2023-06-12]
使用标头中的std::equal函数: if (std::equal(v1.begin(), v1.begin() + n, v2.begin()) std::cout << "success" << std::endl; 请注意,两个向量必须至少有n元素。 如果任一个太短,您的程序的行为将不确定。 如果你想检查整个向量是否等于另一个,只需比较它们,就像比较别的东西: if (v1 == v2) 您的(失败)代码是将一个向量的迭代 器与另一个向量的迭代器进行比较。 相等向量的迭 ... -
我假设一般矢量长度是相同的。 有一个这样的功能 - 你可以用它来定义HammingDistance : elcom[a_List, b_List] := Length[a] - HammingDistance[a, b] 测试一下 elcom[a, b] 2 另外检查EditDistance 。 I assume length of vectors is the same in general. There is a function for this - HammingDistance you can ...
-
我不相信有一个函数,但是使用apply很容易: set.seed(21) z1 <- zoo(rnorm(10), Sys.Date()-10:1) z2 <- zoo(rnorm(10), Sys.Date()-10:1) z <- merge(z1,z2) z$z3 <- apply(z,1,mean) I don't believe there's a function, but it's pretty easy to do with apply: set.seed(21) z1 <- zoo(rn ...
-
一个简单的方法是std::unordered_set vector
vec1; vector vec2; //Fill both of the vectors, with vec1 containing all existing //objects of Test, and vec2 containing some of them. std::unordered_set set2(vec2.begin(),vec2.end()); for (Test* t : ... -
如何在Python中找到两个向量具有相同元素的索引集合(How to find the set of indices where two vectors have equal elements in Python)[2023-10-31]
这是用numpy做的一种方式: np.where(np.equal(Predictions, Labels)) 这相当于: np.equal(Predictions, Labels).nonzero() 它会返回一个元素元组,所以为了得到实际的数组,添加[0]如下所示: np.equal(Predictions, Labels).nonzero()[0] This is one way of doing it with numpy: np.where(np.equal(Predictions, Lab ... -
您也可以使用grep ,并将ignore.case参数设置为TRUE > unlist(sapply(B, function(x){ grep(x, ScanName, ignore.case = TRUE, value = TRUE) }, USE.NAMES = FALSE)) ## [1] "hsa-let-7a-2" "hsa-miR-1238" 这给出了相同的结果 > ScanName[tolower(ScanName) %in% tolower(B)] ## [1] " ...
-
如何找到距离等于X的两个向量的点(How to find the points along two vectors where the distance is equal to X)[2023-10-01]
你可以使用余弦定律 。 以下是将此问题应用于您的问题的说明。 你应该做的是注意矢量A和矢量B的大小比例允许你在图片中从a转换为b。 从那里,C是已知的,A = 180度 - C - B. 你有六个变量,abc ABC,两个约束(A = 180 - C - B和a = ratio * b),以及C形式的常量。 您现在可以为a或b,A或B或c中的三个中的两个选择值。 使用正弦定律将此变量等同于其各自的伙伴。 这样做会留下一个未知的,你可以使用wolfram页面上的相应公式来解决(或者只是从页面上的一个公式中获取 ... -
两个向量的元素比较,同时忽略两个向量之间的所有NaN(Elementwise comparison of two vectors while ignoring all NaN's in between)[2023-11-30]
我就是这样做的: temp1 = vec1(~isnan(vec1)); temp2 = vec2(~isnan(vec2)); m = min(numel(temp1), numel(temp2)); M = max(numel(temp1), numel(temp2)); results = [(temp1(1:m) == temp2(1:m)), false(1,M-m)]; 请注意,此处results是二进制数组。 如果你需要数字,你可以将它转换为double。 关于你对NaN的关注,取决于你想对 ... -
使用NumPy's broadcasting feature ,将A扩展为带有None/np.newaxis的2D版本,然后与1D阵列B进行比较,得到所需的2D布尔数组 - A[:,None] == B 创建A 2D版本的替代方案将是重新整形,使得第二轴的length = 1 。 因此,另一种解决方案是 - A.reshape(-1,1) == B 样品运行 - In [89]: A Out[89]: array([0, 1, 2, 3]) In [90]: B Out[90]: array([4, ...