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;
}

打印结果:

rectpointi.x=1,rectpointi.y=2

rectpointi.x=3,rectpointi.y=4

rectpointi.x=5,rectpointi.y=6

rectpointi.x=7,rectpointi.y=8

recti.x=1,recti.y=2

recti.x=3,recti.y=4

recti.x=5,recti.y=6

recti.x=7,recti.y=8

1.rect_point_dilate=0.000000 rect_point_dilate=0.000000

2.rect_point_dilate=313 rect_point_dilate=110

相关推荐
丰色木夕7 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
衍玊10 小时前
多路转接原理与应用
c++
himobrinehacken11 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
Android小码家14 小时前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
hetao173383714 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法
夏虫不可与之言冰14 小时前
第三课 点亮 LED 与定时器非阻塞闪烁
c语言·c++·vscode·单片机·嵌入式硬件·物联网
辛苦才能14 小时前
C++ 哈希表深度解析:删除为什么必须打标记,负载因子为什么卡 0.7
开发语言·数据结构·c++·算法
miller-tsunami14 小时前
STL简介&string相关知识点
开发语言·c++·string
坤坤子吖15 小时前
Linux 线程同步与互斥——互斥锁
linux·服务器·c++·笔记·学习
TDTC_XQ15 小时前
Linux网络编程accept的讲解
linux·服务器·c++