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

相关推荐
clint45619 小时前
C++进阶(1)——前景提要
c++
夜悊1 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴1 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0012 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾2 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
梦想三三2 天前
OpenCV银行卡数字识别项目(图像预处理与字符分割)
人工智能·opencv·计算机视觉
один but you2 天前
constexpr函数
c++
凡人叶枫2 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫2 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss2 天前
BRpc使用
c++·rpc