c/c++ 函数(一) setw()、isdigit()、isalpha()、atoi()、itoa()

目录

1、setw(int n)         <iomanip>

2、int isdigit(char ch)         <ctype.h>

3、int isalpha(int c)         <ctype.h>

4、int atoi(const char* str)        <stdlib.h>

5、char* itoa(int num)                <stdlib.h>


1、setw(int n)          #include<iomanip>

setw(int n)   设置域宽为n,只对紧接着的输出运算有关

#include<iostream>
using namespace std;
#include<iomanip>

int main()
{
	cout << 123456 << endl;
	cout << 11122 << setw(12) << endl;
	cout << 123 << setw(5) << 456 << setw(5) << endl;
	cout << 12  << setw(3) << 34  << setw(3) << 56 << setw(3) << endl;
	return 0;
}

① 设置域宽当n小于字段长度时不进行操作,当n大于字段长度时,对字段左部分补0,类似于printf语句中的符号%nd 起到设置n个域宽的作用

② 主要是这个紧接着的特性,体会一下 下方123使用到了第二行setw(12)所设置的域宽就可以理解了。

2、int isdigit(char ch)          #include<ctype.h>

如果是数字返回非0, 不是数字返回0

是数字即为真

#include<iostream>
using namespace std;
#include<ctype.h>
#include<string>

int main()
{
	cout << isdigit('a') << endl;
	cout << isdigit('1') << endl;
	string s("abc123def456");
	for (auto& e : s)
	{
		if (isdigit(e))
			cout << "is number" << endl;
		else
			cout << "is not number" << endl;
	}
	return 0;
}

 3、int isalpha(int c)          #include<ctype.h>

template<class E>
    bool isalpha(E c, const locale& loc) const;

 和上面用法一样 为字母返回非零值,不是字母返回0

是字母即为真

 4、int atoi(const char* str)        #include<stdlib.h>

                将一串字符转换为int型

5、char* itoa(int num)                #include<stdlib.h>

                将数字转为一串字符串

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值