emplace_back和push_back() 函数添加 cv::Point 类型数据

emplace_back 和 push_back 是 C++ 中 std::vector用于添加元素的两个成员函数。

区别:

‌push_back‌:接受一个‌已构造好的对象‌(通过拷贝或移动),将其添加到容器末尾。

‌emplace_back‌:‌直接在容器内部构造对象‌,避免额外的拷贝或移动操作。

复制代码
#pragma once
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
#include <vector> 



using namespace cv;
using namespace std;

int main()
{	

    vector <cv::Point>  rectpoint;
	vector <cv::Point>  rect;
    // 1.emplace_back写入数据:
	for (int i = 0; i < 9; i++)
	{
		if (i == 0)
		{
			rectpoint.emplace_back(1, 2);
		}
		if (i == 2)
		{
			rectpoint.emplace_back(3, 4);
		}
		if (i == 6)
		{
			rectpoint.emplace_back(5, 6);
		}
		if (i == 8)
		{
			rectpoint.emplace_back(7, 8);
		}
	}
    
    // 2.push_back写入数据:
    for (int i = 0; i < 9; i++)
	{
		if (i == 0)
		{
			
			// 方法1:push_back
			cv::Point p(1, 2);
			rect.push_back(p);
			// 方法2:rect.push_back(cv::Point(1, 2));
		}
		if (i == 2)
		{
			rect.push_back(cv::Point(3, 4));
		}
		if (i == 6)
		{

			rect.push_back(cv::Point(5, 6));
		}
		if (i == 8)
		{
			rect.push_back(cv::Point(7, 8));
		}
	}

    // 3.打印数据rectpoint:
	for (int i = 0; i < 4; i++)
	{
      cout << "rectpoint[i].x=" << rectpoint[i].x << ",rectpoint[i].y=" << rectpoint[i].y << endl;    ;
		 
	}
    // 4.打印数据rect:
	for (int i = 0; i < 4; i++)
	{
		cout << "rect[i].x=" << rect[i].x << ",rect[i].y=" << rect[i].y << endl; ;

	}


    // 5.修改-打印数据:
    rect[0].x = 313.760223;
	rect[0].y = 110.513077;
	rect[1].x = 422.942902; 
	rect[1].y = 112.106293;

	printf("1.rect_point_dilate=%f  rect_point_dilate=%f\n",
		int(rect[0].x), int(rect[0].y));
	printf("2.rect_point_dilate=%d  rect_point_dilate=%d\n",
		int(rect[0].x), int(rect[0].y));

    return 0;
}

打印结果:

rectpoint[i].x=1,rectpoint[i].y=2

rectpoint[i].x=3,rectpoint[i].y=4

rectpoint[i].x=5,rectpoint[i].y=6

rectpoint[i].x=7,rectpoint[i].y=8

rect[i].x=1,rect[i].y=2

rect[i].x=3,rect[i].y=4

rect[i].x=5,rect[i].y=6

rect[i].x=7,rect[i].y=8

1.rect_point_dilate=0.000000 rect_point_dilate=0.000000

2.rect_point_dilate=313 rect_point_dilate=110

相关推荐
咩咦16 小时前
C++学习笔记24:构造函数初始化列表
c++·学习笔记·类和对象·构造函数·初始化列表·const引用
计算机安禾17 小时前
【c++面向对象编程】第43篇:可变参数模板(C++11):优雅处理不定长参数
java·开发语言·c++
10岁的博客17 小时前
C++ 进制转换:通用 a 进制转 b 进制(2-36进制)题解
开发语言·c++
小贾要学习18 小时前
【Linux】基于自定义TCP协议的日期计算器
linux·网络·c++·网络协议·tcp/ip
YsyaaabB18 小时前
ACM 模式通用代码模板
java·c++·python·算法
我命由我1234518 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
ZPC821019 小时前
单物体最优抓取轨迹生成
python·opencv·计算机视觉
代码村新手19 小时前
C++-多态
开发语言·c++
玖釉-19 小时前
旋转图像:从矩阵转置、镜像到坐标变换的系统理解
c++·windows·算法·图形渲染
掘根20 小时前
【openCV】图像显示,色彩空间转换
人工智能·opencv·计算机视觉