【轨迹规划】c++ vector<vector<double>>数据传递

轨迹信息传递

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdlib.h>
#include <sstream>

using	std::string;
using	std::vector;
using namespace std;
void traj_out(string* path,vector<vector<double>> &temp_vector)
{
    cout<<"*************************************************************"<<endl;
    cout<<"文件的写操作"<<endl;
    //计算结果传递给out_vector
    vector<vector<double>>	out_vector;		//用来保存输出结果的向量
    out_vector=temp_vector;
    ofstream	outfile;//outfile也是一个文件流,用来操作输出文件
    string temp;

    for (vector<double>::size_type i=0;i!=(out_vector.size());i++)
    {
        cout<<"第"<<i<<"的值是:"<<endl<<" { ";
        for(int j=0;j<6;j++)
        {
            cout << out_vector[i][j] << ", " << endl;
        }
        cout <<"} . " << endl;
    }

    outfile.open(*path);  //打开输出文件
    for (vector<double>::size_type i=0;i!=(out_vector.size());i++)
    {
        string	message;
        for(int j=0;j<6;j++)
        {
            cout << out_vector[i][j] << ", " << endl;
            temp = to_string(out_vector[i][j]);	//将10进制的数转换成string类型
            message.append(temp);
            message.append(" ");
        }
        outfile<<message<<endl;
    }
    outfile.close();		//关闭输出文件
}
//用来测试文件的读写操作,输入输出文件均为保存在程序根目录下的txt文件
void traj_in(string *path)
{
    ifstream	infile;	//infile是一个文件流,因此其实还是对流进行的操作
    vector<vector<double>> data_vector;	//用来保存文本中数据的向量
    infile.open(*path);  //打开输出文件

    string indata;
    double  temp;

    if(!infile)     //判断是否存在ifstream infile
    {
        cout<<"读入文件不存在"<<endl;
    }

    if (infile.is_open())   //判断文件流是否处于打开状态
    {
        int line = 1;
        while (infile.good()&&!infile.eof())
        {
            vector<double> group;
            cout << " line : " << line << endl;
            for(int i =0;i<6;i++)
            {
                if(infile.good()&&!infile.eof())
                {
                    infile>>indata;
                    istringstream strstream(indata);
                    strstream>>temp;
                    cout << temp <<endl;
                }
                else
                {
                    cout<<"read file finished " << endl;
                    break;
                }
                group.push_back(temp);
            }
            cout << endl;

            cout << "group size : " << group.size() <<endl;
            line = line+1;
            if(group.size()==6)
            {
                data_vector.push_back(group);
               //将数据读入到data_vector
                cout << "datavector size: "<< data_vector.size() <<endl;
            }
            else break;
        }
    }
    infile.close();

    for (vector<int>::size_type i=0;i!=(data_vector.size());i++)
    {
        for(int j =0;j<6;j++)
        {
             cout<<"line "<<i+1<<" "<< j+1 << " 项的值是:"<<data_vector[i][j]<<endl;
        }
    }
}
int main()
{
    string path = "/home/wcz/QT/output_traj_test/data2";
    vector<vector<double>>	temp_vector = {{1,2,3,4,5,6},
                                           {1.1,2.2,3.3,4.4,5.5,6.6}
                                          };	//缓存向量
    traj_out(&path,temp_vector);
    traj_in(&path);



}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值