1-0:编译 运行并测试本章中的程序
#include <iostream>
using namespace std;
int main()
{
cout << "请输入你的名字" << endl;
string name;
cin >> name;
cout << "hello:" << name << endl;
}
#include <iostream>
using namespace std;
int main()
{
cout << "please input your name";
string name;
cin >> name;
const string greeting = "hello," + name + "!";
string spaces(greeting.size(), ' '); //和问候语大小一致 不过全用空字符代替了
string second = "*" + spaces + "*"; //在问候语两端加上 *
string first(second.size(), '*'); //和second大小一致 不过全用 * 代替
cout << first << endl;
cout << second << endl;
cout << "*" << greeting << "*"<< endl;
cout << second << endl;
cout << first << endl;
return 0;
}
1-1 以下的定义有效吗?理由是什么?
第一行 有效 可以把字符串直接量赋值给字符串变量
第二行 无效 不可以用 + 号连接两个字符串直接量
1-2 以下定义是否有效?
第一行有效 可以把字符串直接量赋值给字符串变量
第二行无效 不可以用 + 号连接两个字符串直接量
1-3 以下程序是否有效?
有效 作用 输出字符串 a string \n another string
1-4
输出字符串 如果更改 效果不变
1-5
无效 最后一行输出 x 不在作用范围内 编译报错 修改如下
#include <iostream>
using namespace std;
int main()
{
{{
string s = "a string";
string x = s + ",really";
cout << s << endl;
cout << x << endl;
}}
return 0;
}
1-6 运行结果
what is your name? Samuel
Hello, Samuel
And what is yours?Beckett
Hello, Beckett ; nice to meet you!