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
#include
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
#include
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
#include
#include
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
#include
using std::string;
int main()
{
string path = "log.log";
remove(path);
}
#include
#include
#include
#include
using namespace std;
int main()
{
ofstream csv_file;
csv_file.open("1.csv", ios::out);
csv_file << "第一行第一列" << ',' << "第一行第二列" << endl;
csv_file << "第二行第一列" << ',' << "第二行第二列" << endl;
csv_file.close();
}
#include
#include
int main()
{
std::ifstream file("1.txt");
std::string line;
while (std::getline(file, line))
{
// Process line
}
}
#include
#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
#include
#include
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);
}
#include
#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 从大到小排序的索引
*/
#include
#include
#include
#include
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/C+±>语言->C++语言标准->ISO C++17标准
默认:mian 返回值表示成功,非0返回值由系统定义。
void f(const vector<int>& par)
{
for(vector<int>::const_iterator it = par.begin(); it != par.end(); it++)
{
}
}
#include
类的所有对象共享这个变量。
#include
#include
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;
}