为了账号安全,请及时绑定邮箱和手机立即绑定

如果不用using namespace std;如何使用cstring头文件中的函数?

如果不用using namespace std;如何使用cstring头文件中的函数?

C++ C
30秒到达战场 2021-12-03 05:01:39
还有不用using namespace std;如何使用string类。还有#include"time.h"在使用using namespace std;时如何声明。我刚学C++,对#include"XXXXXX.h"与#include<XXXXXX> using namespace std;不是不很了解。
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

以下是源代码,基于MFC的控制台程序,如果需要cpp文件的话,请告诉我你的邮箱。

// timeRead.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "timeRead.h"
#include <fstream>
#include <iomanip>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

/*
* @Func : 往文件流out中写入信息,格式为时间:: 内容
*/
void write(ostream &out, const CTime &t)
{
static int cnt = 0; // 当前记录条数
CString szTmp;
// 在时间和内容之间插入一标识符::,起分隔作用,方便提取时间对应的内容
szTmp.Format("%s:: 于%s写入 第%i条记录", t.Format("%Y%m%d"), t.Format("%Y年%m月%d日 %H时%M分%S秒"), ++cnt);
out << LPCSTR(szTmp) << endl;
}

/*
* @Func : 解析格式信息,时间:: 内容,并返回信息
*
* @Parm [in ] szRcdInfo : 记录信息,格式为:时间:: 内容
* @Parm [out] szTime : 时间部分
* @Parm [out] szInfo : 内容部分
*
* @Ret : true格式正确,false格式不正确
*/
bool getInfo(const CString &szRcdInfo, CString &szTime, CString &szInfo)
{
int index = szRcdInfo.Find("::", 0);
if (index == -1) return false;
szTime = szRcdInfo.Left(index);
szInfo = szRcdInfo.Mid(index+2);

return true;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
ofstream out("output.txt");
if (!out)

for (int i = 0; i < 10; ++i)
write(out, CTime::GetCurrentTime()); // CTime::GetCurrentTime()用于获取当前系统时间
out.close();

// 读取文件
ifstream in("output.txt");
if (!in)
CString szRcdInfo, szTime, szInfo;
char buffer[100];
while(in.getline(buffer, 100))
{
szRcdInfo = buffer;
getInfo(szRcdInfo, szTime, szInfo);
cout.flags(cout.flags()|ios::left); // 设置左对齐,默认为右对齐
cout << "[时间] "<< setw(10) << LPCSTR(szTime) << "[内容] " << LPCSTR(szInfo) << endl;
}
}

return nRetCode;
}

以下是我电脑上的输出结果:
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第1条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第2条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第3条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第4条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第5条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第6条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第7条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第8条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第9条记录
[时间] 20100830 [内容] 于2010年08月30日 15时21分08秒写入 第10条记录



查看完整回答
反对 回复 2021-12-07
?
皈依舞

TA贡献1851条经验 获得超3个赞

1、不用using namespace std;如何使用string类,可以单独声明:using std::string;想使用ctring头文件中的函数,直接#include <cstring>就行了。
2、如果在C++中要使用C库中的内容,可以直接使用C头文件的格式,即time.h,在C++中推荐使用ctime,即几乎每个C的头文件在C++里面都把.h去掉,在前面加上c。
3、#include"XXXXXX.h" 代表编译的时候先去用户指定的目录去查找,然后再到系统库去寻找。
与#include<XXXXXX.h>代表直接去系统库去查找,#include<XXXXXX>一般是C++中一些常用类库(新增的)。
4、using namespace std代表把std命名空间中所有定义的东西都引用进来,若要单独使用某个定义在std里面的内容,使用using std::XXX就可以了。

查看完整回答
反对 回复 2021-12-07
  • 2 回答
  • 0 关注
  • 328 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信