您的位置:首页 > 编程语言 > MATLAB

C++和Matlab最常用的编程技能,实现复杂算法的基础

2017-02-09 11:40 826 查看
1、文件读写

(1)获得文件夹下所有的文件

void getFiles(string path, vector<string>& files)

{
//文件句柄  
long   hFile = 0;
//文件信息  
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目录,迭代之  
//如果不是,加入列表  
if ((fileinfo.attrib &  _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}

}

(2)读取某个文件的数据

void ReadData(string filepath)

{

ifstream fin(filepath);
string sx,sy,sz;
while (fin >> sx>>sy>>sz)
{

//cout << sx <<" "<< sy <<" "<< sz << endl;
x = atof(sx.c_str());
y = atof(sy.c_str());
z = atof(sz.c_str());
}

}

(3)输出数据到某个文件

void OutputFile()

{
for (int i = 0; i < curvenum; i++)
{

if (curvelist[i].length <=2)
continue;
int point_num_incurve=curvelist[i].point_list.size();
Curve3d curve = curvelist[i];
char outputfilename[300];
sprintf_s(outputfilename, 300, "D:\\CurvesOutput\\x1\\curve%d.off", i);

                //定制文件名

std::ofstream out(outputfilename, std::ios::out);
out << "OFF" << endl;
out << curvelist[i].length << " 0 " << "0 " << endl;


bool isfirst = true;
Point3d point_last;

for (int j = 0; j < point_num_incurve; j++)
{
int pointindex = curve.point_list[j];
Point3d point = pointlist[pointindex];
if (point.valid == 0)
{
continue;
}
out << point.x << " " << point.y << " " << point.z << endl;
}
out.close();
}

}

2、超大矩阵

vector<vector<double>> M1(a1, vector<double>(b1, 0));

建立一个a1*b1的超大矩阵,全部赋值为0,可以非常大

3、基本算法

可以参考我原来写过的算法总结,包括各种基本数据结构的使用等等

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: