C++-STL(18)-ctime-VS2019下的正解

 

时间函数大家很熟了,但是发现在vs2019下,好用的ctime等函数编译不通过。何解?

因为不安全,全部替换成 类似于ctime_s这样的。
上代码:编译不通过
 

void test_time()
	{
		//1.程序启动时间 clock_t
		clock_t starttm = clock();
  	// 基于当前系统的当前日期/时间
		time_t now = time(0);
		cout << "1970到目前经过秒数:" << now << endl;
		
		// 把 now 转换为字符串形式
		char* dt = ctime(&now);
      	cout << "本地日期和时间:" << dt << endl;

		// 基于当前系统的当前日期/时间
		tm* lctm = localtime(&now);
		dt = asctime(lctm);
		cout << "Local 日期和时间:" << dt << endl;

		// 把 now 转换为 tm 结构(GMT时间)
		tm* gmtm = gmtime(&now);
		dt = asctime(gmtm);
		cout << "UTC 日期和时间:" << dt << endl;

	}

 

可以编译通过的

void test_time_s()
	{
		//1.程序启动时间 clock_t
		clock_t starttm = clock();
		// 基于当前系统的当前日期/时间
		time_t now = time(0);
		cout << "1970到目前经过秒数:" << now << endl;


		// 把 now 转换为字符串形式
		char dt[26];
		ctime_s(dt, sizeof dt, &now);
		cout << "本地日期和时间:" << dt << endl;
		
		// 基于当前系统的当前日期/时间
		tm lctm;
		localtime_s(&lctm, &now);
		cout << "Local 日期和时间:" << endl;
		cout << "year-" << lctm.tm_year << "month-" << lctm.tm_mon << "day-" << lctm.tm_yday << ":" << lctm.tm_hour << ":" << lctm.tm_min << ":" << lctm.tm_sec << endl;


		// 把 now 转换为 tm 结构(GMT时间)
		tm gmtm;
		gmtime_s(&gmtm, &now);
		cout << "UTC 日期和时间:" << endl;
		cout << "year-" << gmtm.tm_year << "month-" << gmtm.tm_mon << "day-" << gmtm.tm_yday << ":" << gmtm.tm_hour << ":" << gmtm.tm_min << ":" << gmtm.tm_sec << endl;


	}

 

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以给您提供一个简单的示例,这个游戏是一个猜数字游戏,玩家需要猜出随机生成的数字。 首先,我们需要包含必要的头文件: ```c++ #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> ``` 然后,我们需要定义一个函数来生成随机数字。在这个函数中,我们使用了 `srand` 函数来设置随机数生成器的种子,以便每次运行程序时都能生成不同的随机数。 ```c++ int generateRandomNumber(int min, int max) { srand(time(NULL)); return (rand() % (max - min + 1)) + min; } ``` 接下来,我们需要定义一个函数来处理玩家的猜测并返回结果。在这个函数中,我们使用了 `std::vector` 来存储之前的猜测,并使用 `std::find` 函数来检查玩家是否已经猜过这个数字。如果玩家猜对了,函数将返回一个 `true` 值,否则返回一个 `false` 值。 ```c++ bool handleGuess(int guess, int number, std::vector<int>& guesses) { if (std::find(guesses.begin(), guesses.end(), guess) != guesses.end()) { std::cout << "You've already guessed that number. Try again.\n"; return false; } guesses.push_back(guess); if (guess == number) { std::cout << "Congratulations! You guessed the number.\n"; return true; } else if (guess < number) { std::cout << "Your guess is too low. Guess again.\n"; return false; } else { std::cout << "Your guess is too high. Guess again.\n"; return false; } } ``` 最后,我们需要定义 `main` 函数来运行游戏。在 `main` 函数中,我们首先生成一个随机数字,然后使用一个循环来让玩家猜测数字,直到玩家猜中为止。 ```c++ int main() { int min = 1; int max = 100; int number = generateRandomNumber(min, max); std::vector<int> guesses; std::cout << "Welcome to the Number Guessing Game!\n"; while (true) { std::cout << "Guess a number between " << min << " and " << max << ": "; int guess; std::cin >> guess; if (handleGuess(guess, number, guesses)) { break; } } return 0; } ``` 完整代码如下: ```c++ #include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> int generateRandomNumber(int min, int max) { srand(time(NULL)); return (rand() % (max - min + 1)) + min; } bool handleGuess(int guess, int number, std::vector<int>& guesses) { if (std::find(guesses.begin(), guesses.end(), guess) != guesses.end()) { std::cout << "You've already guessed that number. Try again.\n"; return false; } guesses.push_back(guess); if (guess == number) { std::cout << "Congratulations! You guessed the number.\n"; return true; } else if (guess < number) { std::cout << "Your guess is too low. Guess again.\n"; return false; } else { std::cout << "Your guess is too high. Guess again.\n"; return false; } } int main() { int min = 1; int max = 100; int number = generateRandomNumber(min, max); std::vector<int> guesses; std::cout << "Welcome to the Number Guessing Game!\n"; while (true) { std::cout << "Guess a number between " << min << " and " << max << ": "; int guess; std::cin >> guess; if (handleGuess(guess, number, guesses)) { break; } } return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值