刚刚开始学习C++,涉及基础知识的比较多,但兴趣是取好的老师,按照自己的想法,把知识点更多的组织在一起进行统合使用,是最有意思的。
比如下面的程序,可以使用最笨的办法,快速的把自己的硬件塞满(小心练习哟)
#include <iostream>
#include <fstream>
#include <thread>
#include <ctime>
#include <random>
#include <Windows.h>
#include <Strsafe.h>
#include <codecvt>
#include <locale>
using namespace std;
std::string unicodeToString(unsigned char* s_src, int len)
{
std::string u8str;
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
for (int i = 0; i < len; ) {
char32_t uhan = s_src[i] << 8 | s_src[i+1];
u8str += converter.to_bytes(uhan);
i += 2;
}
return u8str;
}
//随机数字
int randretu(){
int min = 0,max = 655369;
random_device seed;
ranlux48 engine(seed());
uniform_int_distribution<> distrib(min, max);
return distrib(engine);
}
//生成指定数量的字符
string rangchn(int str_count){
string v;
int j = 0;
int iRange1 = 0xaa - 0xff;
int iRange2 = 0x11 - 0xee;
unsigned seed; // Random generator seed
for (int j=0;j<str_count;++j)
{
unsigned char iCode1 = randretu()%iRange1 + 0xb0;
unsigned char iCode2 = randretu()%iRange2 + 0xa1;
unsigned char ch[2] = {iCode1,iCode2};
v=v+unicodeToString(ch, 2);
}
return v;
}
//向指定的文件写入指定的行数
int wirtefile( std::string filenna,int line_count)
{
ofstream myout(filenna, std::ios::in | std::ios::out | std::ios::app);
time_t now = time(0);
char* dt = ctime(&now);
myout << "write begin time: " << dt << endl;
for (int i = 1; i <= line_count; i++) {
myout << "当前行是: " << i <<" 随机内容是:"<<rangchn(60)<< endl;
}
time_t nowend = time(0);
char* dtend = ctime(&nowend);
myout << "write end time: " << dtend << endl;
myout.close();
return 0;
}
//多线程写入文件
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
thread th[10];
for (int i = 0; i < 10; i++){
string file_name ="c:/my"+std::to_string(i+1)+".txt";
int l_count =400;
th[i] = thread(wirtefile,file_name,l_count);
}
return a.exec();
}
程序用于学习多线程,文件写入,随机数生成,函数调用