第1章(字符串的使用)
我们将了解输入及string库的知识
1.1(输入)
//请求某人输入其姓名,然后向这个人发出问候
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
#include<string>
int main()
{
std::cout << "please enteryour first name:";
std::string name;
std::cin >>name;
std::cout << "Hello,"<< name <<" !"<<std::endl;
return 0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
为了读入输入,必须找个地方来存它,这样的地方就成为变量。变量是一个具有名称的对象,(对象和变量的区别就在于此,因为有些对象是可以没有名字的,另外局部变量有限的生存期也是区分变量和对象的一个重要依据)对象则是计算机中一段具有类型的内存空间。
隐含在对象类型中的还有他的接口,——对于某种类型的对象来说,接口就是可实现的操作的集合。
为了减少对应于每一个输出请求的血操作的系统开销,库使用了缓冲区来积累待写的字符,而且只是在有必要时,它才会将缓冲区的内容写到输出装置中从而刷新缓冲区。
有三种事件会导致系统刷新缓冲区:第一种是缓冲区已经满了,这样库会自动刷新它。
第二种是请求库从标准输入流中读数据。第三种是我们明确要求刷新,系统会刷新缓冲区(cout<<endl;语句)
1.2(将姓名装框)
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
#include<string>
int main()
{
std::cout<< "please enter your first name:";
std::stringname;
std::cin>>name;
conststd::string greeting = "Hello, "+name + " !";
conststd::string spaces(greeting.size() , ' ');
conststd::string second = "*" + spaces + "*";
conststd::string first(second.size() ,'*');
std::cout<< std::endl;
std::cout<< first << std::endl;
std::cout<< second << std::endl;
std::cout<< "*" <<greeting<< "*" <<std::endl;
std::cout<< second << std::endl;
std::cout << first << std::endl;
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
这段程序的执行结果我们可以看到,姓名被装到了一个由星号组成的框里。
新的知识点:
一:第九行用到了字符串的加法,(我们可以将一个字符串和一个字符串字面量连接起来,也可以连接两个字符串,但是不可以连接两个字符串字面量)
二:第十行用到了一种新的字符串变量的初始化方法。是用括号而不是平常的等于号。第一个参数是一个数字(这里是调用一个函数返回值是一个数字,第二个参数时一个字符)他们表示将变量初始化为第一个参数个第二个参数的字符串。
第三:greeting.size()是调用函数,求greeting的长度。
第四:const 表示常量,我们告诉程序这个值将不会被改变。
变量定义的三种形式:
std::stringhello = “hello”; //用明确的初始化值定义变量
std::string starts(100,’*’); //根据类型和给定的表达式构造一个变量
std::string name; //定义一个变量,不指定初始化值,变量的初始化值取决于它的类型。
第二章练习:
1-1
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
using namespace std;
int main()
{
conststd::string hello= "hello";
conststd::string message = hello + ",world" + "!";
cout<< message << endl;
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
定义有效
1-2
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
using namespace std;
int main()
{
conststd::string exclam= "!";
conststd::string message =" hello" + ",world" + exclam;
cout<< message << endl;
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
实验证明,定义无法通过编译。因为加操作不能连接两个字符串字面量
1-3
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
using namespace std;
int main()
{
{
conststd::string s= "a string";
std::cout<< s << std::endl;}
{
conststd::string s =" another string";
cout<<s << endl;}
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
实验证明有效,因为两个s的作用域不同在第二个s执行之前第一个s已经被销毁了
1-4
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
#include<string>
intmain()
{
{
conststd::string s= "a string";
std::cout<< s << std::endl;}
{
conststd::string s =" another string";
cout<<s << endl;}
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
题目中的三种改法竟然都能通过编译并输出!!!我也不知道为啥了。。。。。。求解!!!
为什么没有变量名冲突呢?
1-5
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
using namespace std;
int main()
{
{
conststd::string s= "a string";
{
conststd::string x = s +" another string";
cout<<s << endl;}
cout<< x << endl;
}
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
实验证明,无效。因为x已经被销毁了无法输出。可以将里面的大括号去掉
1-6
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream>
#include<string>
int main()
{
std::cout<< "please enter your first name:";
std::stringname;
std::cin>>name;
std::cout<< "Hello ,"<< name <<"!"<<std::endl<<"and what is yours ?";
std::cin>> name;
std::cout<< "hello, " << name
<<";nice to meet you too!"<< std::endl;
return0;
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
如果程序输入了两个名称,他会将第二个名称直接用于第二次的输入。