c++ 向量复制_在C ++中复制向量的不同方法

c++ 向量复制

In this article, we are going to see how to copy a vector into another vector in various ways? We will also learn about shallow copy and deep copy, their differences, and consequences while checking each of the vector copy methods.

在本文中,我们将看到如何以多种方式将一个向量复制到另一个向量中? 在检查每种向量复制方法时,我们还将了解浅拷贝和深拷贝 ,它们的区别以及后果。

Before starting with the methods for copying a vector into the other, let's discuss what shallow copy and deep copy are. What are differences b/w them, and why do we need two different terms to classify the copy process.

在介绍将向量复制到另一个向量的方法之前,让我们讨论什么是浅拷贝和深拷贝。 它们之间有什么区别,为什么我们需要两个不同的术语来对复制过程进行分类。

浅拷贝与深拷贝 (Shallow copy vs Deep copy)

A vector is essentially an object which has some memory allocation upon execution. Now say you have defined and declared some vector A and you want to copy the content of A into another vector B. Now, there can be two cases, one case is we define a new vector B, but doesn't allocate any new memory for that, rather just link up to the vector A similar like vector B is pointing to the same location of vector A. Thus it has the same copy of vector A. This is known as a shallow copy. If we make any changes to vector A even after our copy operation is done, vector B will have the same changes which are not intended.

向量本质上是一个在执行时具有一些内存分配的对象。 现在说您已经定义并声明了一些向量A,并且想要将A的内容复制到另一个向量B中 。 现在有两种情况,一种情况是我们定义了一个新的向量B ,但没有为此分配任何新的内存,只是链接到向量A,类似向量B指向向量A的相同位置。 因此,它具有向量A的相同副本。 这被称为浅表副本。 如果即使在完成复制操作后对向量A进行任何更改,向量B也会具有相同的变化,这是不希望的。

On the other hand, when we create a new location for vector B and copy contents from vector A, then it's known as deep copy. If we make any change to vector A after the copy operation it won't be reflected in vector B which is of course intended.

另一方面,当我们为向量B创建一个新位置并复制向量A中的内容时,则称为Deep copy 。 如果我们在复制操作之后对向量A进行了任何更改,它都不会反映在向量B中 ,这当然是预期的。

While discussing the copy method we will detail whether it's a shallow copy or deep copy and why so that you never make any mistake using the copy method and guess it's a deep copy! (yes we often intend to have a deep copy normally).

在讨论复制方法时,我们将详细说明它是浅复制还是深复制以及原因,这样您就永远不会使用复制方法犯任何错误,而猜测它是深复制 ! (是的,我们通常通常打算复制一个深层副本)。

不同的复印方式 (Different copy methods)

1)反复复制内容 (1) Copy content iteratively)

Here we simply copy the contents of vector A into vector B iteratively. Vector B is defined as a new location, so it's of course a deep copy. We have verified that too by changing elements after our copy operation.

在这里,我们简单地将向量A的内容迭代地复制到向量B中 。 向量B被定义为新位置,因此它当然是深层副本。 我们也通过在复制操作后更改元素来验证这一点。

Example code:

示例代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //vector A;
    //a vector of 5 element where each 
    //element is -1
    vector<int> A(5, -1); 

    cout << "printing vector A:\n";
    for (auto it : A)
        cout << it << " ";
    cout << endl;

    //vector B defined
    vector<int> B;

    //copy content iteratively
    for (int i = 0; i < A.size(); i++) {
        B.push_back(A[i]);
    }
    cout << "Vector A copied into vector B\n";
    cout << "printing vector B:\n";
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 C++ 实现 "estimate_pose_for_tag_homography" 算法之前,需要确保已经安装了适当的开发环境和相关的计算机视觉库和工具包,如OpenCV和Eigen等。 以下是一个简单的示例代码,演示如何使用 OpenCV 库和 Eigen 库实现 "estimate_pose_for_tag_homography" 算法: ``` #include <iostream> #include <opencv2/opencv.hpp> #include <Eigen/Dense> using namespace std; using namespace cv; using namespace Eigen; int main() { // Load reference image and target image Mat ref_img = imread("ref_image.jpg"); Mat target_img = imread("target_image.jpg"); // Detect and match features between reference image and target image vector<KeyPoint> ref_kp, target_kp; Mat ref_desc, target_desc; Ptr<ORB> orb = ORB::create(); orb->detectAndCompute(ref_img, Mat(), ref_kp, ref_desc); orb->detectAndCompute(target_img, Mat(), target_kp, target_desc); BFMatcher matcher(NORM_HAMMING); vector<DMatch> matches; matcher.match(ref_desc, target_desc, matches); // Extract matched feature points vector<Point2f> ref_pts, target_pts; for (int i = 0; i < matches.size(); i++) { ref_pts.push_back(ref_kp[matches[i].queryIdx].pt); target_pts.push_back(target_kp[matches[i].trainIdx].pt); } // Compute homography matrix Mat H = findHomography(ref_pts, target_pts, RANSAC); // Compute pose from homography matrix Matrix3d K, R; Vector3d t; K << 1000, 0, ref_img.cols/2, 0, 1000, ref_img.rows/2, 0, 0, 1; Matrix3d H_eigen; cv2eigen(H, H_eigen); Matrix3d inv_K = K.inverse(); Matrix3d A = inv_K * H_eigen; double scale = 1.0 / A.col(0).norm(); A *= scale; double sign = (A.determinant() > 0) ? 1 : -1; R.col(0) = A.col(0).normalized(); R.col(1) = A.col(1).normalized(); R.col(2) = R.col(0).cross(R.col(1)); t = sign * scale * A.col(2); cout << "Rotation matrix:" << endl << R << endl; cout << "Translation vector:" << endl << t << endl; return 0; } ``` 该代码演示了如何使用 ORB 特征检测算法和 BFMatcher 特征匹配算法来检测和匹配参考图像和目标图像之间的特征点,然后使用 findHomography 函数计算单应性矩阵,并使用 Eigen 库和数学公式计算出姿态(旋转矩阵和平移向量)。在实际应用,还需要对算法进行优化和调整,以提高估计精度和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值