【C/C++】C++中字符串的输入与循环输入与find查找

【C/C++】C++中字符串的输入与循环输入


1、 字符串的输入

cin、 cin.get() 、cin.getline()、getine()、gets()、getchar()

1.1 cin>>

 1、输入数字,最常用、最基本的

#include <iostream>
using namespace std;
int main()
{
    
    
	int a;
	int b;
	cin >> a >> b;
	cout << a + b << endl;
	return 0;
}

输入:2 3

输出:5

 2、 cin>>输入字符串

  ab之间“空格”、“回车”皆可以,若是要接收一个字符串,cin遇到“空格”、“回车”、“tab”都会结束

#include <iostream>
using namespace std;
int main()
{
    
    
	char a[20];
	cin >> a;
	cout << a << endl;
	return 0;
}

输入:hello world

输出:hello

1.2 cin.get()

 1、 cin.get() 获取单个字符

int main()
{
    
    
	char ch;
	ch = cin.get(); //都只能获取一个字符
	//cin.get(ch);
	cout << ch << endl;
	return 0;
}

输入:hello

输出:h

 2、 cin.get(数组名),可接收多个单词,且以空格隔开

int main()
{
    
    
	char a[20];
	cin.get(a, 5); //可以输入多个单词,中间空格隔开。
	cout << a << endl;
	return 0;
}

输入:i am fun

输出:i am

   这里只接收到了5个字符,其中最后一个为’\0’,所以只看到4个字符输出;

1.3 cin.getline()

 1、cin.getline()接收一个字符串,且以空格隔开

int main ()
{
    
    
	char m[20];
	cin.getline(m,5); //与上面cin.get基本相同。
	cout<<m<<endl;
    return 0;
}

补充:cin.getline()实际上有三个参数,cin.getline(接受字符串到m,接受个数5,结束字符),当第三个参数省略时,系统默认为’\0’ 或‘/n’

 2、cin.getline()使用在多维数组中

int main ()
{
    
    
	char m[3][20];
	//循环输入
	for(int i=0;i<3;i++)
	{
    
    
		cout<<"\n请输入第"<<i+1<<"个字符串:"<<endl;
		cin.getline(m[i],20);
	}
	cout<<endl;//回车
	//循环输出
	for(int j=0;j<3;j++)
		cout<<"输出m["<<j<<"]的值:"<<m[j]<<endl;
	return 0;
}

1.4 getline()

  getline()用来结束一个字符串,可以接收空格并输出,需包含#include

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

int main()
{
    
    
	string str;
	getline(cin, str);//可以接收单个字符,也可以接收字符串,可以识别空格
	cout << str << endl;
	return 0;
}

  getline()函数可以让我们很方便的输入一串字符串。getline()不仅简单,而且安全,因为全局函数 getline() 会帮你处理缓冲区用完之类的麻烦。常见的getline()函数语法有两条:

istream& getline ( istream& is, string& str, char delim );

istream& getline ( istream& is, string& str );

  其中:  is 是进行读入操作的输入流

​        str 是存储读入的内容

​        delim 是终结符,第二个函数默认的是’\n’

1.5 gets()

  gets()函数可以接受一个字符串,可以接收空格并输出,需包含#include

int main ()
{
    
    
	char m[20];
	gets(m); 
	cout<<m<<endl;
    return 0;
}

输入:hello world

输出:hello world

  gets类似cin.getline()里面的例子一样,gets()同样可以用在多维数组里面

1.6 getchar()

  getchar()接收一个字符,需包含#include

int main ()
{
    
    
	char ch;
	ch=getchar(); //不能写成getchar(ch);
	cout<<ch<<endl;
	return 0}

输入:hello

输出:h

2、 循环输入输出

2.1 cin>>循环输入

while(cin>>n)
{
    
    
	//……  输入数据的处理
    cout<<n<<endl;
}

2.2 scanf()循环输入

while(scanf("%d %d",&lift,&right)!=EFO)
{
    
    
	//……  输入数据的处理
    cout<<lift<<" "<<right<<endl;
}

int scanf ( const char * format, … );

scanf的返回值是int型,返回成功读取的项目数,读取失败,返回EFO。

EFO是一个宏常量,实质是-1

2.3 getline()循环输入

	string str;
	while (getline(cin,str))
	{
    
    
		//……  输入数据的处理
		cout << str << endl;
	}
	

3、find查找

 1、algorithm中的函数,其调用形式为

find(start,end,value)

  start搜寻的起点,end搜寻的终点,要寻找的value值

 2、对于Vector容器的表示方法

find(a.begin(),a.end(),value)

 3、对于数组的表示方法

find(a,a+length,val)

 4、对于返回迭代器的查找, 通过判断是否等于.end()来判断元素是否存在,相等说明没有找到该元素

find(a.begin(),a.end(),value)== a.end() 

 5、对于string类,通过判断是否等于string::npos来判断元素是否存在,相等说明没有找到该元素

string.find(value)==string::npos

猜你喜欢

转载自blog.csdn.net/Sunnyside_/article/details/114660822