Functions and string-like objects
foreword
Impetuous people tend to ask: what should I learn? Don't ask, just learn.
C-style strings and string objects have almost the same purpose, but compared with arrays, string objects can implement the functions of passing and copying.
1. A small example
#include<iostream>
#include<string>
using namespace std;
const int SIZE = 5;
void display(const string sa[], int n);
int main()
{
string list[SIZE];
cout << "Enter " << SIZE << " favorite food: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << i + 1<<":";
getline(cin, list[i]);//用getline函数可捕获空格
}
cout << "Your list:" << endl;
display(list,SIZE);
return 0;
}
void display(const string sa[], int n)//该函数用来显示列表内容
{
int i;
for (i = 0; i < n; i++)
{
cout << i + 1 << ":" << sa[i] << endl;
}
}
Among them, each element in the array is a string object, the formal parameter sa is a pointer to the string object, and sa[i] is a string object.