cin scanf cin.getline() cin.get() gets() getchar() get.line() 字符串基础简单输入。

1. cin 和 scanf :

只能接收一字符串遇到,空格,Tab,回车都结束

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[20];
	cin >> str;
	cout << str;
	return 0;
}
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[20];
	scanf("%s" ,&str);
	printf("%s",str);
	return 0;
}

输入:    Hello world!

输出: Hello

2. cin.get()

用法一: 只用来接收一个字符 可以接收空格和Tab 但是不接受回车

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str;
	cin.get(str); //cin.get()
	cout << str << endl;
	return 0;
}

输入    Hello world!

输出    H

用法二:cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格和Tab

这个里面我也有个疑问,某位大佬看到是否能解惑呢

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[20];
	cin.get(str,20);
	cout << str << endl;
	return 0;
}

输入: Hello world!

输出 :Hello world!

输入: qwerqwerqwerqwerqwerqwer   输入大于 20 个时

扫描二维码关注公众号,回复: 4120662 查看本文章

输出:qewrqewrqwerqwerqwe +'\0'   只能读取 19 个 最后一个存为‘\0’

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[20];
	cin.get(str,5);
	cout << str << endl;
	return 0;
}

传递的参数小于数组的长度,是可以的这时是(4个字符+'\0');

传递的参数大于数组的长度时,但是应用就会出问题(我试了一下,是可以输入输出的,但是计算机跑的速度明显下降)原理我也不知道所以大家尽量不要这样用 可以读取 (49个字符+'\0')但是速度变慢了

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[20];
	cin.get(str,50);
	cout << str << endl;
	return 0;
}

用法三:cin.get(无参数),和getchar()都可以 用于舍弃输入流中的不需要的字符,或者舍弃回车,弥补cin.get(字符数组名,接收字符数目)的不足.

还有一个就是 可以输出二维数组

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char str[2][5];
	
	for(int i = 0; i < 2; i++)
	cin.get(str[i],5);
	
	for(int i = 0; i < 2; i++)
	for(int j = 0; j < 5; j++) 
	cout << str[i][j];
	return 0;
}

输入:12345678

学了cin.get() 想一下 会输出什么呢?

3. cin.getline():

cin.getline与cin.get的区别:cin.get( , , )遇到终止字符停止读取后,指针不向后移动;(把回车留在了缓冲区)

cin.getline( , , )遇到终止字符结束后,指针移到终止字符后。(把回车从缓冲区拿走)。

下面给出两个程序 你们跑一下就理解了

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
	char str[5];
	char s[5];
	cin.getline(s,5);
	cin.get(str,5);
	cout << str;
	cout << s;
	return 0;
}

这个是先 cin.getline() 再cin.get()

输入:1234

           5678

输出:12345678

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
	char str[5];
	char s[5];
	cin.get(str,5);
	cin.getline(s,5);

	cout << str;
	cout << s;
	return 0;
}

这个先输入 cin.get(),然后cin.getline()

输入 1234

直接就是输出了,因为get把换行符留在了缓冲区,而cin.getline()遇到换行吃掉然后结束。

其他用法与cin.get() 类似。

注意 : cin.get() 和cin.getline()其实都是传递3个参数,

cin.getline(字符数组(或指针),字符个数n,终止字符)

cin.get(字符数组,字符个数n,终止字符)

4、getline()     // 接受一个字符串,可以接收空格并输出,需包含“#include<string>”

#include<iostream> 
#include<string> 
using namespace std; 
main () 
{ 
	string str; 
	getline(cin,str); 
	cout<<str<<endl; 
	
}

和cin.getline()类似,但是cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数

getline(cin,) 别忘了+cin 哦。

5.getchar()

吃回车   吃空格   吃Tab(读走缓冲区的字符) 一次只能读取一个。 一般有多余的空格或者回车 都用getchar 吃掉就行了。

#include<iostream> 
#include<string> 
#include<stdio.h>
using namespace std; 
main () 
{ 
	string str; 
	getchar();
	cin >> str; 
	cout<<str<<endl; 
	
}

输入:空格 1234

输出 :空格1234;

6.gets()

需要头文件 #include<stdio.h>

gets()  和  puts()  输入输出一次只能输入一个字符串,

吃空格吃Tab 不吃回车(遇到回车结束)

#include<iostream> 
#include<string> 
#include<stdio.h>
using namespace std; 
main () 
{ 
	char str[20]; 
	gets(str);
	cout << str << endl;
}

puts()输出的字符串中可以包含转义符

#include<iostream> 
#include<string> 
#include<stdio.h>
using namespace std; 
main () 
{ 
	char str[] = "China\nBeijing"; 
	puts(str);
}

输出 :China

         Beijing.

猜你喜欢

转载自blog.csdn.net/Harington/article/details/83544175
今日推荐