C/C++ 碎片整理

sprintf

  • //int sprintf( char *buffer, const char *format [argument], … );
    //类似 printf,不过是把打印去向变为 内存中的字符串
    // buffer : 缓冲区 format : 格式化字符串
    // argument: 实参 parament:形参
  • char array[30] = { 0 };
    //sprintf(array, “%d”, 123);
    //sprintf(array, “%-4d%4d%4x”, 123, 123, 123); // 重写,原来数据清除
    //打印格式要求 与 printf 完全一致
    // %-8x: x(16进制) -(左对齐) 8(宽度为8,不足补空格)
    sprintf(array, “%5d”, 123); //上位(左边)不足补‘0’
    sprintf(array, “%-10.3f”, 3.1415626); // 产生:"3.142 "
  • //“% m.ns”中,m表示占用宽度(字符串长度不足时补空格,超出了则按照实际宽度打印)
    //n才表示从相应的字符串中最多取用的字符数
  • //sprintf 采用“* ”来占用一个本来需要一个指定宽度或精度的常数数字的位置,
    //同样,而实际的宽度或精度就可以 和其它被打印的变量一样被提供出来
    //sprintf(array, “%*.*f”, 10, 2, 3.1415926); // 产生" 3.14″

Time

  • // C 的风格------------->>>>>>>>>>
    // tm 是结构体,内部有 int tm_year, int tm_mon等成员,精确到秒,从1900.1.1开始计时
    // 要输出真实的日期,需要tm_year+1900, tm_mon+1
#include <ctime>
	#include <iostream>
	#include <sstream>
	
	int main(void) {
		time_t now = time(NULL);
		tm* tm_t = localtime(&now);
		std::stringstream ss;
		ss << "year:" << tm_t->tm_year + 1900 << " month:" << tm_t->tm_mon + 1 << " day:" << tm_t->tm_mday << " hour:" << tm_t->tm_hour << " minute:" << tm_t->tm_min << " second:" << tm_t->tm_sec;	
		std::cout << ss.str();
		return 0; // 打印当前时间
}
  • //:
    // clock_t: Alias of a fundamental arithmetic type capable of representing clock tick counts.
    // time_t: an integral value representing the number of seconds since 1970_1_1
    //
    // time(): Get the current calendar time as a value of type time_t.
    // 当参数为 NULL ,返回值有效,否则将当前时间传给参数
    // time_t timer;
    // time(&timer); 或者 timer = time(NULL);
    //
    // struct tm * localtime (const time_t * timer);
    // Use the value pointed by timer to fill a tm structure
    // with the values that represent the corresponding time
    //
    // char* asctime (const struct tm * timeptr);
    // converts contents of the tm structure to a C-string
    // containing a human-readable version of the corresponding date and time
    // 打印形式:Wed Feb 13 15:46:11 2013
    //
    // char* ctime (const time_t * timer);
    // converts contents of the tm structure to a C-string
    // 打印形式:Wed Feb 13 15:46:11 2013
  • // C++11 的风格------------->>>>>>>>>>
    // chrono:
    // chrono is the name of a header, but also of a sub-namespace:
    // All the elements in this header (except for the common_type specializations)
    // are defined under the !!!std::chrono namespace.!!!
    //
    // template <class Rep, class Period = ratio<1> >
    // class duration; 类模板
    // expresses a time span by means of a count and a period.
    // 例:std::chrono::duration MyChrono;
    //
    // typedef duration <Rep, ratio<3600,1>> hours;
    // typedef duration <Rep, ratio<60, 1>> minutes;
    // typedef duration <Rep, ratio<1, 1>> seconds;
    // typedef duration <Rep, ratio<1, 1000>> milliseconds; //毫秒
    // typedef duration <Rep, ratio<1, 1000000>> microseconds; //微秒
    // typedef duration <Rep, ratio<1, 1000000000>> nanoseconds; //纳秒
    //
    // hours Myhour(24); // constructor—>>> 24h
    // Myhour.count(); // 获得 Rep
    // duration类 重载了各类 operator 函数
    //
    // template <class Clock, class Duration = typename Clock::duration>
    // class time_point;
    // A time_point object expresses a point in time relative to a clock’s epoch
    //
    // 注:epoch 指1970.1.1新纪元
    // system_clock:
    // Clock classes provide access to the current time_point.
    //
    // system_clock::now()
    // Returns the current time_point in the frame of the system_clock.
    //
    // time_t system_clock::to_time_t (const time_point& tp)
    // Converts tp into its equivalent of type time_t
    //
    // system_clock::time_point和system_clock::duration间可以进行算术运算
    //
    // std::chrono::duration_cast<const duration&>(exp)
    // 例:std::chrono::milliseconds ms
    // = std::chrono::duration_caststd::chrono::milliseconds (s); 秒到毫秒
    //
#include<chrono>
#include<ratio>
#include<iostream>
#include<ctime>
using namespace std;

int main() {
	cout << chrono::milliseconds::period::num << " / "
		<< chrono::milliseconds::period::den;
	return 0;
}

杂绪

  • // VPN:(virtual private network)虚拟专用网络
    // 利用公用网络架设专用网络, 实现远程访问内网
    // SSL VPN 利用 SSL(Secure Sockets Layer 安全套接层协议)
    // Web VPN 基于Web (万维网)
    // 内网 == 局域网
    // WiFi:
    // wireless network protocols which are commonly used for local area networking of devices and Internet access,
    // allowing nearby digital devices to exchange data by radio waves.
  • // ratio: 类模板(分数)
    // template <intmax_t N, intmax_t D = 1> class ratio;
    // numerator 分子 denominator 分母
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值