C++ basic review: C++ method of processing strings - string class and string variable

foreword

C++ provides a new data type - string type (string type). The string type is not a basic type of the C++ language itself. It is a string class declared in the C++ standard library , and objects can be defined with this type.

Definition and Reference of String Variables

  1. define string variable
string str1;
string str2=“China”;

Note: When using the definition variable of the string class, the string header file in the C++ standard library must be included at the beginning of this file, that is, add#include <string>

  1. assign value to string variable
str1=“China”;		//str1是字符串变量,不是字符数组名
str1=str2;		//字符串变量之间也可互相赋值
str1[2]='Z'	//可以对字符串变量中的某一个字符进行赋值,结果就成了“ChZna”
  1. Input and output of string variables
cin>>str1;		//将str1输入
cout<<str1;		//将str1输出

Operations on String Variables

When using character arrays to store strings, string operations should use string functions, such as strcat connection, strcmp comparison, and strcpy copying. For string class objects, these functions can be used instead of simple operators.

  1. String copy directly with symbols
str1=str2;
作用同“strcpy(str1,str2);
  1. String concatenation directly uses the + sign
string str1="A";;
string str1="B";
string str;
str=str1+str2;		//则str="AB"
  1. String comparisons directly use relational operators
    You can directly use relational operators to compare strings. For example, equal to (==), greater than (>), greater than or equal to (>=), not equal to (!=).

array of strings

Not only can you define string variables with string, but you can also use string to define string arrays.

string name[5];
string name[5]={
    
    "W","A","N"};		//那每一个元素就相当于一个字符串变量。

Examples of String Operations

  1. Input three strings, and request to output the strings in order of the size of the first letter from small to large, and the strings are separated by spaces.

Topic analysis:
First of all, it is necessary to define the variable first;
then assign a value to the variable;
then it is the comparison process: first compare 23 to ensure that 2 is less than 3 , then use 1 to compare with 23 to determine the output string order, This is much easier. The output is always in the order of 123 but it is already a sorted string.

code:

//输入三个字符串,要求将字符串按照**首**字母的大小由小到大的顺序输出字符串,字符串之间用空格隔开。
#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
	string str1,str2,str3,temp;
	cin >> str1 >> str2 >> str3;
	if(str2>str3)
	{
    
    
		temp=str2;str2=str3;str3=temp; //先保证str2是小于str3的然后再判断str1与这俩的大小
	}
	if(str1<=str2)
	cout << str1+' ' << str2+' ' <<str3;
else if(str1<=str3)
	cout << str2+' ' << str1+' ' << str3;
else
	cout << str2+' ' << str3+' ' << str1;

	return 0;
}

Test Results:
insert image description here

  1. There are n students in a class, and the simple materials (name and student number) of each student need to be input into the computer and saved. You can then look up information about a student by entering their name. After inputting a name, the program will check whether there is such a student in the class, if yes, then output his name and student number, if not found, then output "there is no such person in this class".
    To solve this problem, two functions can be written separately. The function input_datais used to input the names and student numbers of n students, and the function searchis used to find out whether the student you are looking for is in the class.

Problem analysis:
In addition to the main function, the program of this problem includes two sub-functions for calling.

code:

#include <iostream>
#include <string>
using namespace std;
//定义全局变量
string name[50],num[50];
int n;//实际的学生数

int main()
{
    
    	
	//函数声明
	void input_data();
	void search(string find_name);
	string find_name;
	//输入学生人数和信息
	cout <<"请输入本班学生的人数:";
	cin >> n;
	input_data();
	//查询
	cout << "请输入要查询的学生姓名:";
	cin >> find_name;
	search(find_name);

	return 0;
}

void input_data()
{
    
    
	int i;
	for(i=0;i<n;i++)
	{
    
    
		cout<<"请输入第"<< i+1 <<"个学生的姓名和学号:";
		cin >> name[i] >> num[i];
	}
}

void search(string find_name)
{
    
    
	int i;
	bool flag = false;//用来判断数据是否有效
	for(i=0;i<n;i++)
	{
    
    
		if(name[i] == find_name)//遍历之前输入的学生信息,若没有找到相关的学生信息则flag一直保持false。
		{
    
    	cout << name[i] << "的学号是:" << num[i] << endl;
			flag = true;			
		}
		break;//跳出本次循环,继续下一次循环
	}

	if(flag == false)
		cout << "本班无此人";
}

expand:

#include <iostream>
#include <string>
using namespace std;
//定义全局变量
string name[50],num[50],sex[50],age[50],chinese[50],math[50],english[50];
int n;//实际的学生数

int main()
{
    
    	
	//函数声明
	void input_data();
	void search(string find_name);
	string find_name;
	//输入学生人数和信息
	cout <<"请输入本班学生的人数:";
	cin >> n;
	input_data();
	//查询
	cout << "请输入要查询的学生姓名:";
	cin >> find_name;
	search(find_name);

	return 0;
}

void input_data()
{
    
    
	int i;
	for(i=0;i<n;i++)
	{
    
    
		cout<<"请输入第"<< i+1 <<"个学生的姓名、学号、性别、年龄、以及语数英三门成绩:"<< endl;
		cout<<"姓名 ";cin >> name[i];
		cout<<"学号 ";cin >> num[i];
		cout<<"性别 ";cin >> sex[i];
		cout<<"年龄 ";cin >> age[i];
		cout<<"语文成绩 ";cin >> chinese[i];
		cout<<"数学成绩 ";cin >> math[i];
		cout<<"英语成绩 ";cin >> english[i];
	}
}

void search(string find_name)
{
    
    
	int i;
	bool flag = false;//用来判断数据是否有效
	for(i=0;i<n;i++)
	{
    
    
		if(name[i] == find_name)//遍历之前输入的学生信息,若没有找到相关的学生信息则flag一直保持false。
		{
    
    	cout << name[i] << "的学号是 " << num[i]<< "\t性别 " << sex[i] << "\t年龄 " << age[i]<< endl;
			cout << name[i] << "成绩分别为:\n" <<"语文成绩 "<< chinese[i]<<"\t数学成绩 "<< math[i] <<"\t英语成绩 "<< english[i]<< endl;
			flag = true;			
		}
		break;//跳出本次循环,继续下一次循环(去掉好像也可以)
	}

	if(flag == false)
		cout << "本班无此人";
}

Result test:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43136158/article/details/105815464