240 likes | 448 Views
第九章. 标准库类型-1. 回顾. 运算符重载的概念 以成员函数的方式重载运算符 以友元函数的方式重载运算符 流插入和流提取运算符的重载 一般单目和双目运算符的重载 赋值运算符重载 类型转换运算符重载. 目标. string 类型的介绍 string 的输入和输出 string 的成员函数 vector 类型的介绍 vector 类型的构造函数. 标准库 string 类型. string 类型支持长度可变的字符串, C++ 标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作
E N D
第九章 标准库类型-1
回顾 • 运算符重载的概念 • 以成员函数的方式重载运算符 • 以友元函数的方式重载运算符 • 流插入和流提取运算符的重载 • 一般单目和双目运算符的重载 • 赋值运算符重载 • 类型转换运算符重载
目标 • string类型的介绍 • string的输入和输出 • string的成员函数 • vector类型的介绍 • vector类型的构造函数
标准库string类型 • string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作 • typedef basic_string<char> string; • typedef basic_string<wchar_t> wstring; • 要使用string类型对象,必须包含相关头文件 • #include <string> • using namespace std::string;
string对象的定义和初始化 • string s1; //默认构造函数,s1为空串 • string s2(s1); //将s2初始化为s1的一个副本 • string s3(“value”); //将s3初始化为一个字符串字面值副本 • string s4(n, ‘c’); //将s4初始化为字符‘c’的n个副本 • 字符串常量与标准库string类型不是同一种类型
string对象的读写 • 使用用cin的“>>”提取运算符进行读操作,使用cout的“<<”输入运算符进行输出操作 #include <iostrem> #include <string> using namespace std; void main(){ string str; cout<<“input your name:” ; cin>>str; cout<<“your name is ”<<str<<endl; }
用getline读取整行文本2-1 • 函数功能:从输入流的读取一行,并保存读取的内容到string对象中,但不包括换行符 • 函数原型: • template<class _E, class _TYPE, class _A> inline basic_istream<_E, _TYPE>& getline( basic_istream<_E, _TYPE>& Istream, basic_string<_E, _TYPE, _A>& Xstring, const _E _D=_TYPE::newline()); • 函数参数:1 输入流对象;2 string对象;3 结束符 • 函数返回值:输入流对象
用getline读取整行文本2-2 #include <string> #include <iostream> using namespace std ; void main() { string s1; cout << "Enter a sentence (use <space> as the delimiter):"; getline(cin,s1, ' '); cout << "You entered: " << s1; }
常用的成员函数2-2 #include <string> #include <iostream> using namespace std; void main(){ string strinfo=" //*---Hello Word!......------"; strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int first = strinfo.find_first_of(strset); if(first == string::npos) cout<<"not find any characters"<<endl; int last = strinfo.find_last_of(strset); if(last == string::npos) cout<<"not find any characters"<<endl; cout << strinfo.substr(first, last - first + 1)<<endl; }
string::size_type类型2-1 • string类类型和许多其他库类型都定义了一些配套类型(companion type) • 通过这些配套类型,库类型的使用就能与机器无关(machine-independent) • 与unsigned型(unsigned int或unsigned long)具有相同的含义,可以保证足够大能够存储任意string对象的长度 • 成员函数中很多参数或者返回值都是string::size_type类型
string::size_type类型2-2 #include <iostream> #include <string> using namespace std; void fun(const string str){ if(str.empty()) { //str.size()==0 return;// } string::size_type len=str.size(); cout << "The size of " << str << "is " << len<<endl; } void main(){ string str("The expense of spirit\n"); fun(str); }
string的关系运算符2-1 • string类重载了关系运算符 • 关系操作符用来比较两个string值的大小 • string对象比较操作是区分大小写的 • ==操作符来测试两个string对象是否相等 • 两个string对象相等是指它们的长度相同,且含有相同的字符 • !=操作符来测试两个string对象是否不等 • 关系操作符<,<=,>,>=分别用于测试一个string对象是否小于、小于或等于、大于、大于或等于另一个string对象
string的关系运算符2-2 #include <iostream> #include <string> using namespace std; void main() { string big = "big", small = "small"; string s1 = big; if (big > small) cout<<“big > small”; if (big == s1) cout<<“big == s1”; }
string对象的赋值 • 可以把一个string对象赋值给另一个string对象 • 例如: • string st1, st2 = "The expense of spirit"; • st1 = st2; • 赋值操作后,st1就包含了st2串所有字符的一个副本 • 先把st1占用的相关内存释放掉,然后再分配给st1足够存放st2副本的内存空间,最后把st2中的所有字符复制到新分配的内存空间
两个string对象相加 • string对象的加法被定义为连接concatenation • 通过使用加操作符+或者复合赋值操作符+= • 例如: • string s1(“hello, ”),s2("world\n"); • string s3 = s1 + s2; • s1 += s2; • 和字符串常量的连接时,+操作符的左右操作数必须至少有一个是string类型 • 例如: • s3 = s1 + “, ”; //正确 • s3 = “hello” + “, ”; // 错误 • s3 = s1 + “, ” + “world”; // 正确 • s3 = “hello” + “, ” + s2; // 错误
从string对象获取字符 • string类型通过下标操作符([])来访问string对象中的单个字符 • 下标操作符需要取一个size_type类型的值 • 使用方式和字符数组类似 #include <string> #include <iostream> using namespace std; void main() { string str("some string"); for (string::size_type ix = 0; ix != str.size(); ++ix) cout << str[ix] << endl; }
标准库的vector类型 • vector是同一种类型的对象的集合 • vector的数据结构很像数组,能非常高效和方便地访问单个元素 • vector是一个类模板(class template) • 要使用vector必须包含相关头文件 • #include <vector> • using std::vector;
vector的定义 • vector保存何种对象的类型,通过将类型放在类模板名称后面的尖括号中来指定类型 • vector的定义语法: • vector<类型名> 对象名称; • 例如: • vector<int> ivec; • vector<Sales_item> Sales_vec;
vector对象的初始化 • vector类定义了好几种构造函数 • vector<T> v1; //vector保存类型为T的对象。默认构造函数v1为空 • vector<T> v2(v1);// v2是v1的一个副本 • vector<T> v3(n, i); //v3包含n个值为i的元素 • vector<T> v4(n); //v4含有值初始化的元素的n个副本
vector元素值的初始化 • 如果没有指定元素的初始化式,那么标准库将自行提供一个元素初始值进行值初始化(value initializationd)。初始值将用来初始化容器中的每个元素,具体取决于存储在vector中元素的数据类型 • 例如: vector<int> fvec(10); //10个int元素,初始化为0 • 如果vector保存的是含有构造函数的类类型(如string)的元素,标准库将用该类型的默认构造函数创建元素初始值 • 例如: vector<string> svec(10); // 10个string元素,初始化为NULL
vector成员函数 • vector标准库提供了许多类似于string对象的操作
vector使用 #include <string> #include <vector> #include <iostream> using namespace std; void main(){ string word; vector<string> text; for(int i=0;i<5;i++){ cout<<“input string:”; cint>>word; text.push_back(word); } cout<<text.size()<<“ element” <<endl; }
总结 • string的定义 • string的输入和输出 • string常用的成员函数 • vector的基本介绍 • vector的定义