标准库类型(一)
--命名空间using与string类型
引:
标准库类型是语言组成部分中更基本的哪些数据类型(如:数组、指针)的抽象!
C++标准库定义的是高级的抽象数据类型:
1、高级:因为其中反映了更复杂的概念;
2、抽象:因为我们在使用时不需要关心他们是如何表示的,我们只需要知道这些抽象数据类型支持哪些操作就可以了。
正文:
一、命名空间的using声明
1、 using std::cin;
::运算符的作用含义是右操作数的名字可以在左操作数的作用域中找到。
格式:
using namespace::name;
//一旦使用了using声明,我们就可以直接引用名字,而不需要再引用该名字的命名空间!
示例:
#includeusing std::cin; using std::cout; using std::endl; int main() { cout << "Enter two numbers:" << endl; int v1, v2; cin >> v1 >> v2; cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl; return 0; }
2、在一种情况下,必须总是使用完全限定的标准库名字:在头文件中。
通常,头文件中应该只定义确实必要的东西。请养成这个好习惯!
二、标准库string类型
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作。
#includeusing std::string;
1、string对象的定义和初始化
//四种定义及初始化方式
string s1;
string s2(s1);
string s3("value");
string s4(n,'c');
2、string与字符串字面值的异同
1)都是以'\0'结尾
string s1("value");
for (string::size_type i = 0;s1[i] != '\0'; ++i)
cout << s1[i] << ' ';
cout << endl;
2)字符串字面值与string根本就不是一个类型!
string SI = "ABC"; char SII[] = "CDE"; string tmp = SI; SI = string(SII); strcpy(SII,tmp.c_str()); cout << SI << endl; cout << SII << endl;
3、getline读取整行文本
1)、getline函数从输入流的下一行读取,并保存读取内容到string中,但是不包括换行符!
2)、getline函数将stream参数作为返回值!
3)、由于getline在返回时丢弃换行符,所以换行符将不会保存在string对象中!
//P72 习题3.5
int main()
{
string line;
while (getline(cin,line))
{
cout << line << endl;
}
return 0;
}
//3.5(2)
int main()
{
string word;
while (cin >> word)
{
cout << word << endl;
}
return 0;
}
4、string对象的操作 size与empty
//例程1
string st("The expense of spirit\n");
cout << "The size of " << st << "is " << st.size()
<< " characters, including the newline" << endl;
//例程2
string se;
if (se.empty())
{
cout << "The string is empty!" << endl;
}
else
{
cout << "The size of " << se << "is " << se.size()
<< " characters, including the newline" << endl;
}
5、string::size_type类型
string类类型和许多其他库类型都定义了一些配套类型,通过这些配套类型,库类型的使用就能与机器无关(machine-independent)。string::size_type类型可以保证足够大到能够存储任意string对象的长度。
string str = "Hello World";
string::size_type length = str.size();
6、string关系操作符
==,!=,<,<=,>=,>
string big = "big",small = "small";
if (big == small) //big <= small,big >= small,big != small,...
{
//...
}
else
{
//...
}
7、string对象的赋值
string s1,s2 = “value”; s1 = s2;
赋值操作需要做一些操作:它必须把s1占用的相关内存释放掉,然后再分配给s1足够存放s2副本的内存空间,最后把s2中所有的字符都复制到新分配的内存中!
8、两个string对象相加
string s1("hello, ");
string s2("world\n");
string s3 = s1 + s2;
cout << s3 << endl;
string s4 = s2 + s1;
cout << s4 << endl;
string s5 = s3 + "! " + "I`m a programmer!";
cout << s5 << endl;
9、string对象str下标的取值范围:0~str.size()-1
string str ("Some thing!");
for (string::size_type i = 0; i != str.size(); ++i)
{
cout << str[i];
}
cout << endl;
10、string对象下标可以用作左值
string str ("Some thing!");
for (string::size_type i = 0; i != str.size(); ++i)
{
str[i] = '*';
}
cout << str << endl;
11、string对象中的字符处理函数
isalnum,isalpha,iscntrl,isdigit,isgraph,islower,isprint,ispunct,isspace,isupper,isxdigit,tolower,toupper...
//P78 习题3.7
//(1)
int main()
{
string s1,s2;
cin >> s1 >> s2;
if (s1 > s2)
{
cout << s1 << " is bigger!" << endl;
}
else if (s1 < s2)
{
cout << s2 << " is bigger!" << endl;
}
else
{
cout << s1 << " is equal to " << s2 << endl;
}
return 0;
}
//(2)
int main()
{
string s1,s2;
cin >> s1 >> s2;
if (s1.size() > s2.size())
{
cout << s1 << " is longer!" << endl;
}
else if (s1.size() < s2.size())
{
cout << s2 << " is longer!" << endl;
}
else if (s1.empty() && s2.empty())
{
cout << s1 << " and " << s2 << " is empty!" << endl;
}
else
{
cout << s1 << " is equal to " << s2 << endl;
}
return 0;
}
//习题 3.8
//(1)
int main()
{
string sub,str;
while (cin >> sub)
{
str += sub;
}
cout << str << endl;
return 0;
}
//(2)
int main()
{
string sub,str;
cin >> sub;
str = sub;
while (cin >> sub)
{
str += ' ';
str += sub;
}
cout << str << endl;
return 0;
}
//习题 3.9 下面的程序是否合法,合法的话会输出什么值?
int main()
{
string s;
cout << s[0] << endl;
s[1] = '*';
cout << s[1] << endl;
return 0;
}