目录
1.文件流指针(输入输出)
【C++】C++ 文件读写 ofstream和ifstream详细用法 - bdy - 博客园 (cnblogs.com)
方法1:substr截取字符串
#define FILENAME "C:\\Users\\xiao_\\Desktop\\text.csv"
#define FILENAME1 "C:\\Users\\xiao_\\Desktop\\text1.csv"
int main()
{
std::list<std::string> StatYearList;
//从test.txt文件读取数据
std::ifstream fin(FILENAME , std::ios::in);
if (!fin.is_open())
{
fmt::print( "error open file![{0}]\n", FILENAME);
return -1;
}
while (!fin.eof())
{
std::string tmp;
std::string data;
IemsStatYear statYear;
getline(fin, tmp);
size_t star = tmp.find(',', 0);
if (star == std::string::npos)
{
fmt::print("找不到字符串[{0}]\n", star);
}
statYear.year = stoi(tmp.substr(0, star));
star++;
statYear.devName = tmp.substr(star, tmp.find(',', star));
star++;
statYear.devType = tmp.substr(star, tmp.find(',', star));
star++;
statYearList.push_back(statYear);
}
fin.close();
//输出到另一个文件test0.txt
ofstream out(FILENAME1 , ios::out);
out << "NAME,DATA_TYPE,DATA_MONTH,DATA_DAY,VAL,UNIT" << endl;
for (int i = 0; i < statYearList.size(); i++)
{
out << statYearList[i].NAME << "," << statYearList[i].DATA_TYPE << "," <<
statYearList[i].DATA_MONTH << "," << statYearList[i].DATA_DAY << "," <<
statYearList[i].VAL << "," << statYearList[i].UNIT << endl;
}
out.close()
return 0;
}
方法2:boost::split()截取字符串
用法:
#include <boost/algorithm/string.hpp>
std::string line("a fox jump ");
// trim(), trim_left(), trim_right()
boost::trim(line); // line: "a fox jump"
std::vector<std::string> contents;
// 分割,通过boost::is_any_of指定分割符。
boost::split(contents, line, boost::is_any_of(", "));
// 拼接
std::string line2 = boost::join(contents, "="); //line2: a=fox=jump
举例:
int main()
{
std::ifstream fin(fileName, std::ios::in);
if (!fin.is_open())
{
fmt::print( "error open file![{0}]\n", fileName);
return -1;
}
while (!fin.eof())
{
TYPE_iems_stat_year statYear;
std::string tmp;
getline(fin, tmp);
std::vector<std::string> contents;
boost::split(contents,tmp,boost::is_any_of(","));
if(contents[0] == "")
{
return 0;
}
if(contents[0] == "year")
{
continue;
}
std::string sqlCmd = fmt::format("insert into iems_stat_year values ({},\" {}\",\"{}\",\"{}\",\"{}\",{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{})",
contents[0],contents[1],contents[2],contents[3],contents[4],contents[17],contents[5],contents[6],contents[7],contents[8],contents[9],
contents[10],contents[11],contents[12],contents[13],contents[14],contents[15],contents[16],contents[18],contents[19],contents[20],contents[21]);
StatYearList.push_back(sqlCmd);
}
fin.close();
return 0;
}
2.string::find()函数
删除字符串中的所有空格
字符串中的双引号替换为单引号
//删除字符串中的所有的空格
void remomeSpace(std::string& str)
{
int ndex = 0 ;
if(!str.empty())
{
while((ndex = str.find(' ',ndex)) != std::string::npos)
{
str.erase(ndex,1);
}
}
}
//将字符串中的双引号替换为单引号
void replaceStr(std::string& str)
{
int pos = 0;
if(!str.empty())
{
while((pos = str.find('"',pos)) != std::string::npos)
{
str.replace(pos,1,"'");
pos++;
}
}
else
LOG(ERROR)<<"sqlWhere is null !!";
}
int main(int argc,char** argv)
{
std::string str = " aaa bbb ccc ddd ";
int num = str.size();
printf("%d\n",num);
//删除字符串中的所有的空格
remomeSpace(str);
//将字符串中的双引号替换为单引号
str = "case_name="aaa"";
replaceStr(sqlWhere);
printf("%s\n",str.c_str());
return 0;
}
3.数据库value横向相加与纵向相加
//value1-value96
struct Table
{
char aa[32];
char bb[128];
float v1;
float v2;
...
...
float v96;
}
Table table;
std::vector<Table> tableVec
//将数据填充到tableVec
....
....
float value;
for(int i = 0; i < tableVec.size(); i++)
{
float* value = &tableVec[i].v1;
for(int j = 0; j < 96; j++)
{
value += value[i]; //横向相加(v1+...+v96)
}
}
float* value;
for(int i = 0; i < 96)
{
float valTmp = 0;
float* val ;
for(int j = 0; j < tableVec.size(); j++)
{
val = &tableVec[j].v1;
valTmp += val[i]
}
value[i] = valTmp;//纵向相加(v1+...+v1,...,v96+...+v96)
}
3.STL
1.std::map<int,std::vector<float> > mp
struct Value{
float p;
float q;
...
...
};
//初始化map
std::map<int,std::vector<float> > mp;
std::vector<Value> vec;
//填充vec,步骤省略...
//插入map
std::vector<float> fVec;
for(int i = 0; i < vec.size(); i++)
{
mp[i].push_back(vec[i].p);
mp[i].push_back(vec[i].q);
...
...
}
for(auto& it : mp)
{
auto it = mp.find(it.first);
if(it != mp.end())
{
float sum = 0.0;
std::vector<float>& values = it->second;
for(auto& val : values)
{
sum += val;
}
fVec.push_back(sum);
}
}
4.OpenMP
1. OpenMP实现多线程
(1) 在.pro文件加上CONFIG *= omp
#include <omp.h>
#include <thread>//sleep_for
using namespace std::chrono_literals;//sleep_for
int main()
{
omp_set_num_threads(2);//创建两个线程
#pragma omp parallel // 工作并行区域(parallel): 用来设置并行区域,创建线程来执行任务。
{
#pragma omp single // Single 范围内的代码,让一个线程来操作,不一定是主线程
{
#pragma omp task // task是omp的核心特性
{
while(true)
{
if(0 == time(nullptr) % 300)
{
//核心代码.....
}
std::this_thread::sleep_for(300ms);
}
}
#pragma omp task
{
while(true)
{
if(0 == time(nullptr) % 300)
{
//核心代码...
}
std::this_thread::sleep_for(300ms);
}
}
}
}
return 0;
}
5.sleep_for的使用
std::this_thread::sleep_for - cppreference.com
6.Lambda应用
C ++ Lambda表达式详解_c++ lambda-CSDN博客
7.字符串删除特定的字符
std::string str = "aa bb cc";
//删除字符串空格
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());