C++实现的一些功能代码
将当前时间输出到txt中:
调用c++中的fstream流文件,用tm结构获取日期和时间,其在time.h中定义
用ofstream的时候,ofstream out(txtpath,ios::app); //追加的方式写入 ofstream out(txtpath) //打开文件,从零开始写入
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std; void writeTimeToTxt(string txtname)
{
ofstream fid(txtname); time_t tt = time(NULL);
struct tm local_time;
localtime_s(&local_time, &tt); fid
<< "***testing date: "
<< local_time.tm_year + << "-" << local_time.tm_mon + << "-" << local_time.tm_mday << " "
<< local_time.tm_hour << ":" << local_time.tm_min << ":" << local_time.tm_sec << endl;
fid.close(); return;
} int main()
{
string txtname = "time.txt";
writeTimeToTxt(txtname); return ;
}
输出的结果:
将当前时间以字符串的形式返回:
#include <time.h> string get_time_string()
{
time_t tt = time(NULL);
struct tm local_time;
localtime_s(&local_time, &tt);
clock_t now_clock = clock();
int msecond = now_clock % CLOCKS_PER_SEC; char text[];
sprintf(text, "%d%d%d%d%d%d%03d", local_time.tm_year + , local_time.tm_mon + , local_time.tm_mday,
local_time.tm_hour, local_time.tm_min, local_time.tm_sec, msecond); return text;
}
通过argv向程序中输入参数:
#include <iostream>
#include <string>
using namespace std; int main(int argc, char* argv[]) { if ( != argc)
{
cout << "input params error" << endl;
return -;
} string type = argv[];
string cutmethod = argv[];
string resolution = argv[]; cout << type << "_" << cutmethod << "_" << resolution << endl; system("pause");
return ;
}
输出结果:
在vs中参数的输入,项目-属性-调试-命令参数-train expand 64x64 输入的字符串,双引号可加可不加
生成的exe调用过程中,.\OutputTimeToTxt.exe "train" expand 64x64 字符串的双引号,可加可不加
linux:
以微秒为单位,返回string类型的时间戳,并以时间戳为名称,建立文件夹
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#include <string> #include <sys/stat.h>
#include <sys/types.h> std::string get_nowtime_linux()
{
struct timeval tv;
gettimeofday(&tv,NULL);
std::string now_time=std::to_string(tv.tv_sec)+"_"+std::to_string(tv.tv_usec);
return now_time;
} int main(int argc,char** argv)
{
std::string now_time=get_nowtime_linux();
std::cout<<now_time<<std::endl; int isCreate = mkdir(now_time.c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
if( !isCreate )
printf("create path:%s\n",now_time.c_str());
else
printf("create path failed! error code : %d %s \n",isCreate,now_time.c_str()); return ;
}
二维数组传引用:
#include <iostream>
using namespace std; /*传二维数组*/ //第1种方式:传数组,第二维必须标明
/*void display(int arr[][4])*/
void display1(int arr[][4], const int irows)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<4; ++j)
{
cout << arr[i][j] << " "; //可以采用parr[i][j]
arr[i][j] += 1;
}
cout << endl;
}
cout << endl;
} //第2种方式:一重指针,传数组指针,第二维必须标明
/*void display(int (*parr)[4])*/
void display2(int(*parr)[4], const int irows)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<4; ++j)
{
cout << parr[i][j] << " "; //可以采用parr[i][j]
parr[i][j] += 1;
}
cout << endl;
}
cout << endl;
}
//注意:parr[i]等价于*(parr+i),一维数组和二维数组都适用 //第3种方式:传指针,不管是几维数组都把他看成是指针
/*void display3(int *arr)*/
void display3(int *arr, const int irows, const int icols)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<icols; ++j)
{
cout << *(arr + i*icols + j) << " "; //注意:(arr+i*icols+j),不是(arr+i*irows+j)
*(arr + i*icols + j) += 1;
}
cout << endl;
}
cout << endl;
} int main()
{
int arr[][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
int irows = 3;
int icols = 4;
//display1(arr, irows);
display2(arr, irows); //注意(int*)强制转换.个人理解:相当于将a拉成了一维数组处理。
//display3((int*)arr, irows, icols);
for (int i = 0; i < irows; i++)
{
for (int j = 0; j < icols; j++)
printf("%d ",arr[i][j]);
printf("\n");
} system("pause");
return 0;
}
C++实现的一些功能代码的更多相关文章
- 原生JS实现购物车结算功能代码+zepto版
html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 常见.NET功能代码汇总 (2)
常见.NET功能代码汇总 23,获取和设置分级缓存 获取缓存:首先从本地缓存获取,如果没有,再去读取分布式缓存写缓存:同时写本地缓存和分布式缓存 private static T GetGradeCa ...
- 通过javascript库JQuery实现页面跳转功能代码
通过javascript库JQuery实现页面跳转功能代码的四段代码实例如下. 实例1: 1 2 3 4 $(function(){ var pn = $("#gotopagenum&quo ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
- 20个开发人员非常有用的Java功能代码
本文将为大家介绍20个对开发人员非常有用的Java功能代码.这20段代码,可以成为大家在今后的开发过程中,Java编程手册的重要部分. 1. 把Strings转换成int和把int转换成String ...
- day1 函数 (独立功能代码块)
1.引入函数 2.函数执行过程 4.带参数的函数 5.带返回值的函数 6. 多个返回值 (return a,b,c)元组 7.4种函数 1.引入函数 独立功能代码块 ---> 封装 ----&g ...
- js经常使用功能代码
js经常使用功能代码(持续更新): 1---折叠与展开 <input id="btnDisplay" type="button" class=" ...
- html和js基础功能代码备份
1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a> 3) ...
- 常见.NET功能代码汇总
1,在Web上修改指定文件位置的Web.config 这里需要使用 WebConfigurationManager 类,但必须使用WebConfigurationFileMap类来指定文件位置,看代码 ...
- 手机开发必备技巧:javascript及CSS功能代码分享
1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...
随机推荐
- Android -- Glide框架详解(一)
1,使用这个框架快两年了,今天去github上去看了一下,貌似已经从3.X升级到4.X了,想着自己还没有对这个框架在博客上做过总结,所以这里打算出三篇博客来介绍,内容有基本使用.3.X与4.X的不通. ...
- 重装@angular/cli reason: write EPROTO 139955972261696:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:252:
前几天不小心卸载了 angular@cli,然后重装的时候发现,一直报错.如下: ××××××××@××××ln622653:/$ npm install -g @angular/clinpm ERR ...
- linux svn安装 及 常用命令
安装 yum -y install subversion svnserve --version 显示版本信息,则表示安装成功 mkdir /home/svn 创建版本库目录 svnadmin crea ...
- P1186 玛丽卡
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- highcharts 大数据 String+,StringBuilder,String.format运行效率比较
实现String字符串相加的方法有很多,常见的有直接相加,StringBuilder.append和String.format,这三者的运行效率是有差异的,String是final类型的,每次相加都会 ...
- shell里连接数据库,将结果输出到变量
result=$(sqlplus -s 'ccc/ccc@21.96.34.34:1521'<<EOF ..... EOF )
- pandas 时间格式转换
OUTLINE 常见的时间字符串与timestamp之间的转换 日期与timestamp之间的转换 常见的时间字符串与timestamp之间的转换 这里说的字符串不是一般意义上的字符串,是指在读取日期 ...
- 'scope' is defined but never used
错误如下: 解决办法: 1.scope这个属性在最新版本vue已经被弃用,升级成slot-scope了 ,所以属性名应该改为slot-scope. 2.如上所示,我们发现,还是有报错,原因是vetur ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)A Red Rover
题目链接 大概意思就是,找出一个字串,将其所有出现的地方替换成一个字母M后的长度len1,然后再加上这个字串的长度t_len得到一个新的长度len,求这个len最小等于多少. 当然,也可以不找这个串, ...
- what i want
i want to be the object of every beautiful creature. they strongly want to talk with me, and study f ...