读取txt文件中的三维坐标,并且存储在vector中,并将vector转化为二维数组

本文介绍如何从TXT文件读取三维坐标数据,并存储到C++的vector和动态数组中。通过实例演示了文件流操作和vector的基本使用,包括读取数据、存储和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*
* 程序目标:读取txt文件中的坐标,并且存储在vector、矩阵和数组中
*
* 摘要:
*1.这篇文章包含两个基础内容,文件流操作和vector操作,可以参考下面两篇文章:
*2.文件流操作:https://blog.youkuaiyun.com/a125930123/article/details/53542261
*3.vector操作:https://blog.youkuaiyun.com/armman/article/details/1501974
*
* 当前版本:1.1
* 作者:Shoen
* 完成日期:2018年3月29日
*/

//#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>


using namespace std;
using namespace cv;


////************利用点的vector存储读取的坐标*************//
//vector是一个序列,读取每个值的方式是 vector.at(i)
int main()
{
    
    ifstream circle;

    //定义点的vector
    //typedef Point_<double> Point2d;
    vector<Point3d> points;
    
    //定义点,作为push_back的对象
    //这个需要注意,vector的每一项需要作为一个整体来pushback,数组就pushback数组,类就pushback类。
    Point3d temp;

    //定义两个变量,用来存储读取的数据
    double temp1 = 0, temp2 = 0, temp3 = 0;

    circle.open("Circle.txt");

    //!circle.eof() 用来控制什么时候读取终止
    for (int i = 0; !circle.eof(); i++)
    {
        //读取txt文件的坐标值
        circle >> temp1 >> temp2  >>temp3;
        temp.x = temp1;
        temp.y = temp2;
        temp.z = temp3;
        points.push_back(temp);
    }
    
    double **swp;      //动态申请二维数组 n行 m列
    swp = new double *[points.size()];
    for (int i = 0; i<points.size(); i++)
    { 
        swp[i] = new double[3];
    }
    
    
    for (int i = 0; i < points.size(); i++)
    {
        swp[i][0] = points.at(i).x;
        swp[i][1] = points.at(i).y;
        swp[i][2] = points.at(i).z;
        cout << "[" << points.at(i).x << "  ,  " << points.at(i).y <<" , "<< points.at(i).z<< "]" << endl;
        cout << "[" << swp[i][0] << "  ,  " << swp[i][1] << " , " << swp[i][2] << "]" << endl;

    }
    cout<< swp[0][0] << endl;
    circle.close();
    system("pause");
    
    /*
    Point3f p3f(3, 4, 5);

    cout << "【三维点】" << endl << p3f << endl;
    */
    return 0;

}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值