Note01

本文介绍了C++编程中的一些实用技巧,包括文件读取、输出精度控制、使用exit函数提前退出程序、生成随机数、获取当前时间的不同方法、四舍五入操作、结构体与字符串的拼接、vector容器的应用、重载操作符[]以及友元函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 相对路径是针对项目的主目录文件夹而言的;
  2. 判断文件是否读完:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    string next;
    fstream inputStream;
    inputStream.open(".\\Debug\\player.txt");
    //判断文件是否结束
    while (inputStream >> next)
    {
        cout << next << endl;
    }
    inputStream.close();
    system("pause");
    return 0;
}
  1. 设置输出小数的精度:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double input;
    double result;
    cin >> input;
    result = sqrt(input);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << result << endl;
    system("pause");
    return 0;
}
  1. exit函数:
    exit函数定义在cstdlib库中
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    cout << "Hello Out There!\n";

    exit(1);
    cout << "This statement is pointless,\n"
        << "because it will never be executed.\n"
        << "This is just a toy program to illustrate exit.\n";
    return 0;
}
  1. 随机函数
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
int main()
{
    cout << "随机生成十个随机数:\n";
    for (int i = 0; i < 10; i++)
    {
        cout << rand() << endl;
    }
    cout << "使用种子产生随机数:\n";
    //使用种子保证产生的随机数不唯一
    time_t now_time;
    now_time = time(NULL);
    //种子不同,产生的随机数也不同
    //使用当前时间作为种子,以保证产生的随机数不同
    cout << "种子是当前的时间;种子为: " << now_time << endl;
    srand(now_time);
    cout << rand() << endl;

    system("pause");
    return 0;
}
  1. 获得当前时间
//方案— 优点:仅使用C标准库;缺点:只能精确到秒级
#include <time.h> 
#include <stdio.h> 
int main( void ) 
{ 
    time_t t = time(0); 
    char tmp[64]; 
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j%z",localtime(&t) ); 
    puts( tmp ); 
    return 0; 
}
/*
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根据格式字符串生成字符串。
struct tm *localtime(const time_t *timer);
取得当地时间,localtime获取的结果由结构tm返回
返回的字符串可以依下列的格式而定:
%a 星期几的缩写。Eg:Tue 
%A 星期几的全名。 Eg: Tuesday
%b 月份名称的缩写。 
%B 月份名称的全名。 
%c 本地端日期时间较佳表示字符串。 
%d 用数字表示本月的第几天 (范围为 0031)。日期
%H24 小时制数字表示小时数 (范围为 0023)。 
%I12 小时制数字表示小时数 (范围为 0112)。 
%j 以数字表示当年度的第几天 (范围为 001366)。 
%m 月份的数字 (范围由 112)。
%M 分钟。 
%p''AM''''PM'' 表示本地端时间。 
%S 秒数。 
%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。
%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。 
%w 用数字表示本周的第几天 ( 0 为周日)。 
%x 不含时间的日期表示法。 
%X 不含日期的时间表示法。 Eg: 15:26:30
%y 二位数字表示年份 (范围由 0099)。 
%Y 完整的年份数字表示,即四位数。 Eg:2008
%Z(%z) 时区或名称缩写。Eg:中国标准时间 
%% % 字符。
方案二 优点:能精确到毫秒级;缺点:使用了windows API 
*/
#include <windows.h> 
#include <stdio.h> 
int main( void ) 
{ 
    SYSTEMTIME sys; 
    GetLocalTime( &sys ); 
    printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek); 
    return 0;
}
//方案三,优点:利用系统函数,还能修改系统时间
//此文件必须是c++文件
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
    system("time");
}
//方案四,将当前时间折算为秒级,再通过相应的时间换算即可
//此文件必须是c++文件
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    time_t now_time;
    now_time = time(NULL);
    cout<<now_time;
    return 0;
}
//方案五:
#include <afx.h>
CTime t = CTime::GetCurrentTime(); //获取系统日期
int d=t.GetDay(); //获得几号
int y=t.GetYear(); //获取年份
int m=t.GetMonth(); //获取当前月份
int h=t.GetHour(); //获取当前为几时
int mm=t.GetMinute(); //获取分钟
int s=t.GetSecond(); //获取秒
  1. 四舍五入
#include<iostream>
using namespace std;
int fun(double i);
int main()
{
    double input;
    cin >> input;
    cout << fun(input) << endl;
    system("pause");
    return 0;
}
int fun(double i) {
    return static_cast<int>(i + 0.5);
}
  1. struct与字符串的拼接
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
struct Student
{
    string name;
    int age;
    double grade;
};
int main()
{   
    Student students[10];
    stringstream sst;
    for (int i = 0; i < 10; i++)
    {
        //清空上次的内容
        sst.str("");
        sst << (i + 1);
        //拼接字符串
        students[i].name = "lotus " + sst.str();
        students[i].age = rand() % 30;
        students[i].grade = rand() % 100;
    }
    for (int i = 0; i < 10; i++)
    {
        cout << students[i].name << "\t" << students[i].age << "\t" << students[i].grade << endl;
    }
    system("pause");
    return 0;
}
  1. vector
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> data;
    for (int i = 0; i < 1000;i++)
        data.push_back(rand() % 1000);
    cout << "the total size of the vector " << data.size() << endl;
    for (int i = 0; i < data.size(); i++)
    {
        cout << data[i] << "\t";
        if (i>10 && i / 10 == 0)
        {
            cout << endl;
        }
    }
    system("pause");
    return 0;
}
  1. 重载操作符[]
#include<iostream>
#include <cstdlib>
using namespace std;
class Chair
{
private:
    char first;
    char second;
public:
    Chair() {}
    Chair(int firstValue, int secondValue) 
        :first(firstValue), second(secondValue)
    {}
    char& operator[](int index);
};
int main()
{
    Chair chair(1,2);
    chair[1] = 'A';
    chair[2] = 'B';
    cout << chair[1] << "\t" << chair[2] << endl;
    cin >> chair[1] >> chair[2];
    cout << chair[1] << "\t" << chair[2] << endl;
    system("pause");
    return 0;
}
char& Chair::operator[](int index)
{
    if (index == 1) 
    {
        return first;
    }
    else if(index == 2)
    {
        return second;
    }
    else
    {
        cout << "illegal value.\n";
        exit(1);
    }
}
  1. 友元函数:可以访问函数的私有变量;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值