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是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...
随机推荐
- 把玩Pencil项目之编译并运行
Pencil是个好项目.使用Electron作为运行环境,如同Vs Code一样,将JS跨平台桌面应用做了一个好的示范.个人很喜欢这种方式,毕竟多年来关注Web全栈开发,有一种JS一统天下的感觉.我的 ...
- springMVC--annotation
一 解析类 ComponentScanBeanDefinitionParser component-scan标签解析类 component-scan 兼容 annotation-config ,因此前 ...
- CentOS 7 MySQL HA之DRBD
一.DRBD简介 DRBD的全称为:Distributed ReplicatedBlock Device(DRBD)分布式块设备复制,DRBD是由内核模块和相关脚本而构成,用以构建高可用性的集群.其实 ...
- day12 python作业:员工信息表
作业要求: 周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearc ...
- Java 多线程之Timer与ScheduledExecutorService
1.Timer管理延时任务的缺陷 a.以前在项目中也经常使用定时器,比如每隔一段时间清理项目中的一些垃圾文件,每个一段时间进行数据清洗:然而Timer是存在一些缺陷的,因为Timer在执行定时任务时只 ...
- 2017-2018-2 20155228 《网络对抗技术》 实验八:Web基础
2017-2018-2 20155228 <网络对抗技术> 实验八:Web基础 1. 实践内容 1.1 Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET ...
- 三月pat(转)
转自https://blog.csdn.net/weixin_40688413/article/details/88082779 担心别人删除了就找不到了.因为九月要考. 7-1 Sexy Prime ...
- DataGridView 列排序 内存表查找
DataRow[] drow = dt.Select("列名 = 列名的值" ); 就这句话,dt是一个datatable 且断点调试时能看到里面有trade这个列,可为什么执行到 ...
- STD函数的内部计算公式
各股票软件的标准差函数STD是不同的,而布林线的上下轨是以STD为基础计算出来的,所以使用布林线应小心.以2008/3/28的上证综指为例,利用如下代码:"收盘价3日STD:STD(CLOS ...
- 【.NET】 C# 时间戳和DataTime 互相转换
1.C# DateTime转换为Unix时间戳 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(, , )); // ...