目录
工具
动态改变保存文件名
int sprintf(char * str, const char * format,...)
将格式数据写入字符串。
str:保存字符串的buffer;
format: 和print类似。
例子:
#include <stdio.h>
int main()
{
char file_paht[100];
for(int i = 0; i < 10; i++)
{
sprintf(file_path, "D:\\images\\US\\Phatom\\tilt\\images\\T\\Smoothing\\%d.bmp",i);
}
}
return 0;
获取文件夹下所有文件路径
#include <filesystem>
#include <iostream>
using namespace std;
namespace fs = std::filesystem;
int main()
{
std::string path = "D:\\PickAndPlaceMachine\\Dinnerware\\抓取点\\抓取点\\imgs\\";
for (const auto &entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
替换文件名后缀
将.jpg替换为.txt为例
#include <filesystem>
#include <iostream>
using namespace std;
namespace fs = std::filesystem;
int main()
{
std::string paths = "D:\\PickAndPlaceMachine\\Dinnerware\\抓取点\\抓取点\\imgs\\";
for (const auto &entry : fs::directory_iterator(paths))
{
size_t last_index = entry.path().string().find_last_of(".");
string txt_path = entry.path().string().replace(last_index,4, ".txt");
cout << txt_path << std::endl;
}
}
替换文件名中的路径
#include <filesystem>
#include <iostream>
#include <string>
string jpg_root = "D:\\images\\";
string txt_root = "D:\\txt\\";
using namespace std;
namespace fs = std::filesystem;
using std::string;
int main()
{
for (const auto &entry : fs::directory_iterator(src_path))
{
size_t last_index= entry().path().string().find_last_of("\\");
string txt_name = entry().paht().string().replace(0, last_index, txt_root);
std::cout << txt_name << std::endl;
}
}
删除文件
#include<stdio.h>
#include<string>
using std::string;
int main()
{
string path = "log.log";
remove(path);
}
写csv文件
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ofstream csv_file;
csv_file.open("1.csv", ios::out);
csv_file << "第一行第一列" << ',' << "第一行第二列" << endl;
csv_file << "第二行第一列" << ',' << "第二行第二列" << endl;
csv_file.close();
}
按行读取txt文件
#include <fstream>
#include <string>
int main()
{
std::ifstream file("1.txt");
std::string line;
while (std::getline(file, line))
{
// Process line
}
}
按行写txt文件
#include <fstream>
#Include <string>
int main()
{
std::string line = "test";
std::fstream file(file_name.c_str(), std::ios::out | std::ios::trunc);
file << line << std::endl;
}
按空格将字符串分割成字符串组
按行读取txt,并把每一行按空格分成X,Y,Z
#include <sstream>
#include <fstream>
#include <string>
int main()
{
std::ifstream file("1.txt");
std::string line;
while (std::getline(file, line))
{
std::stringstream lines(line);
std::string x,y,z;
lines >> x >> y >> z;
}
}
欧式距离
void EculideanDistance(const double& x, const double& y,double* distance)
{
*distance = sqrt(x*x + y*y);
}
opencv 获取图像宽高
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main()
{
img = imread("1.jpg", IMREAD_COLOR);
int width = img.cols;
int height = img.rows;
}
vector 从小到大排序后,排序索引
/*
求vector 从大到小排序的索引
*/
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
using std::vector;
template <typename T>
vector<size_t> SortIndexes(vector<T> &V)
{
vector<size_t> index(V.size());
std::iota(index.begin(), index.end(), 0);
sort(index.begin(), index.end(),
[&V](size_t ind1, size_t ind2) {return V[ind1] < V[ind2]; });
return index;
}
int main()
{
vector<int> vec = { 2, 50, 23, 534, 23 };
vector<size_t> index = SortIndexes(vec);
for (vector<size_t>::iterator it = index.begin(); it != index.end(); it++)
{
std::cout << *it << std::endl;
}
}
c++17
vs2017 使用c++17
属性->配置属性->C/C+±>语言->C++语言标准->ISO C++17标准
语法
main
默认:mian 返回值表示成功,非0返回值由系统定义。
vector
函数参数为容器的const引用,迭代器要相应选择const_iterator
void f(const vector<int>& par)
{
for(vector<int>::const_iterator it = par.begin(); it != par.end(); it++)
{
}
}
iota
#include <numeric>
类成员函数内定义static变量
类的所有对象共享这个变量。
vector
clear
- 将size变为0;即begin() == end()
- 如果未进行重写,原地址的数据不会变且可以访问。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a = {1, 3, 6};
a.clear();
vector<int>::iterator it = a.begin();
std::cout << *(it+2) << std::endl; // 返回 6
// for无循环执行
for(vector<int>::iterator it = a.begin(); it != a.end(); it++)
std::cout << *it << std::endl;
return 0;
}