不好意思,昨天又很忙写不了,今天我们来详细讲一下输入的函数.
1.Cin这个之前讲过了,就详细不展开了(注意该函数会滤除不可见字符如(空格,回车等))。
如果用cin读入字符串,小心,cin读入时遇到空格就会结束,也就是说如果一段字符中含有空格就会导致只读入一部分字符而已。
2.cin.get(字符数组名,接收字符数目)
顺带讲一下for语句(有限次(当然也能写成死循环),次数固定的循环结构)
For ( 定义(初始值); , 判断句; , 赋值(次数减一) )执行语句(单句);
如果执行语句有多句则需要{}括起来
For ( 定义(初始值); , 判断句; , 赋值(次数减一) )
{
执行语句1;
执行语句2;
。。。。。。
}
这是一个结合字符串数组,cin.get,for语句的程序
#include<iostream.h>
void main(void)
{
char a[10];// 定义一个可以存储11个字符的字符串数组(注意a[0]是可以存值的)
cout<<"请输入十个字符(不要分开)"<<endl;//如 1234567890
cin.get(a,11);
//注意虽然我们输入的是10个字符但当我们敲下回车键表示输入结束时//会自动生成一个结束符'\0'(占一个字符)
for (int i=0;i<=10;i++)
{
cout<<a[i]<<endl;
}
cout<<"over"<<endl;
}
3.cin.getline() 读入包括空格的字符串(用字符串数组存储 char a[xxx],不需要调用头文件函数)
cin.getline(a,n) a表示字符串数组,n表示要读入的字符数
(注意:cin.getline(a,n)表示读入n - 1个字符的字符串)(’\0’占一个字符)
代码跟cin.get相似,就不写了。
4.getline 读入包括空格的字符串(用字符串变量存储string a,需要调用<string.h>)
#include<iostream>
#include<string>//调用string
using namespace std; //用上这句话,就不用写include<xxxx.h>的.h
void main(void)
{
string st;//定义字符串
int strlong,strsize;
cout<<"请输入字符串(字符数<255)"<<endl;
getline(cin,st);//从cin获取字符串st
strlong = st.length();//字符串的长度
strsize = st.size(); //字符数
cout<<st<<" 该字符串含有 "<<strsize<<" 个字符"<<" 长度:"<<strlong<<endl;
}
Size和length 根据我的了解好像没啥区别……..
我们在码代码经常要调用头文件,so我在网上找了一下,C++大多数的头文件就在下面了
不用背,用多了就能记住了。
1、传统 C++
#include <assert.h> //设定插入点
#include <ctype.h> //字符处理
#include <errno.h> //定义错误码
#include <float.h> //浮点数处理
#include <fstream.h> //文件输入/输出
#include <iomanip.h> //参数化输入/输出
#include <iostream.h> //数据流输入/输出
#include <limits.h> //定义各种数据类型最值常量
#include <locale.h> //定义本地化函数
#include <math.h> //定义数学函数
#include <stdio.h> //定义输入/输出函数
#include <stdlib.h> //定义杂项函数及内存分配函数
#include <string.h> //字符串处理
#include <strstrea.h> //基于数组的输入/输出
#include <time.h> //定义关于时间的函数
#include <wchar.h> //宽字符处理及输入/输出
#include <wctype.h> //宽字符分类
-----------------------------------------
2、标准 C++(同上的不再注释)
#include <algorithm> //STL 通用算法
#include <bitset> //STL 位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL 双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL 定义运算函数(代替运算符)
#include <limits>
#include <list> //STL 线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include <iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL 队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL 堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL 通用模板类
#include <vector> //STL 动态数组容器
#include <cwchar>
#include <cwctype>
-------------------------------------
3、C99(一个C++语言标准版本)增加
#include <complex.h> //复数处理
#include <fenv.h> //浮点环境
#include <inttypes.h> //整数格式转换
#include <stdbool.h> //布尔环境
#include <stdint.h> //整型环境
#include <tgmath.h> //通用类型数学宏
先写到这里,今晚尽量再补一章