标准库类型string表示可变长的字符序列,其定义在头文件<string>
中。作为标准库的一部分,string定义在命名空间std中。要使用string类型,需在程序中包含如下代码:1)using namespace std;
,或2)using std::string;
。
定义和初始化string对象
如何初始化类的对象是由类本身决定的。一个类可以定义很多初始化对象的方式,只不过这些方式之间必须有所区别:或者是初始值的数量不同,或者是初始值的类型不同。
初始化string对象的方式:
string s1; //默认初始化一个string对象,s1是一个空字符串
string s2(s1); //s2是s1的副本
string s2=s1; //等价于s2(s1),s2是s1的副本
string s3("hello");//s3是字符串字面值"hello"的副本,除了字面值最后的那个空字符外
string s3="hello"; //等价于s3("hello"),s3是字面值"hello"的副本
string s4(n,'c'); //把s4初始化为由连续n个字符c组成的串
string s4(10,'c'); //s4的内容是10个c:cccccccccc
string对象上的操作
一个类除了要规定初始化其对象的方式外,还要定义对象上所能执行的操作。其中,类既能定义通过函数名调用的操作,就像Sales_item类的isbn函数那样,也能定义"<<、+"等各种运算符在该对象上的新含义。
string的操作
os<<s //将s写到输出流os当中,返回os
is>>s //从is中读取字符串赋给s,字符串以空白分隔,返回is
getline(is,s) //从is中读取一行赋给s,返回false
s.empty() //s为空返回true,否则返回false
s.size() //返回s中字符的个数
s[n] //返回s中第n个字符的引用,位置n从0计起
s1+s2 //返回s1和s2连接后的结果
s1=s2 //用s2的副本代替s1中原来的字符
s1==s2 //如果s1和s2中所含的字符完全一样,则它们相等;
//string对象的相等性判断对字母的大小写敏感
s1!=s2
<,<=,>,>= //利用字符在字典中的顺序进行比较,且对字母的大小写敏感
两个string对象相加
两个string对象相加得到一个新的string对象,其内容是把左侧的运算对象与右侧的运算对象串接而成。也就是说,对string对象使用加法运算符(+
)的结果是一个新的string对象,它所包含的字符由两部分组成:前半部分是加号左侧string对象所含的字符、后半部分是加号右侧string对象所含的字符。另外,复合赋值运算符(+=
)负责把右侧string对象的内容追加到左侧string对象的后面。
string s1="hello,",s2="world!\n";
string s3=s1+s2; //s3的内容是hello,world\n
s1+=s2; //等价于s3=s1+s2
cout<<"s3:"<<s3<<endl;
cout<<"s1:"<<s1<<endl;
为string对象赋值
一般来说,在设计标准库类型时都力求在易用性上向内置类型看齐,因此大多数库类型都支持赋值操作。对于string类而言,允许把一个对象的值赋给另外一个对象。
//S1的内容是10个c:cccccccccc,s2是一个空字符串
string s1(10,'c'),s2;
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
//赋值:用s2的副本替换s1的内容,此时s1和s2都是空字符串
s1=s2;
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
字符串字面值和string对象相加
把数值类型转换为字符串string类型
(1)使用stringstream
要使用stringstream类型,需在程序中包含如下代码:
#include <sstream>
using namespace std;
代码清单
double a = 123.32;
string res;
stringstream ss; //定义流ss
ss << a; //将数字a转化成流ss
ss >> res; //将流ss转化成字符串
cout<<res<<endl;
(2)使用函数模板
+ostringstream
使用函数模板将内置数据类型(整型、字符型、实型、布尔型)转换成string。
代码清单
//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型
//ostringstream只支持<<操作符
template<typename Type>
string toString(const Type& t){
ostringstream oss; //创建一个格式化输出流
oss<<t; //把值传递如流中
return oss.str();
}
cout<<toString(14.2)<<endl; //实型->string:输出14.2
cout<<toString(12301)<<endl; //整型->string:输出12301
cout<<toString(123456789785)<<endl;//长整型->string:输出123456789785
cout<<toString(true)<<endl; //布尔型->string:输出1
(3)使用标准库函数std::to_string()
std命令空间下有一个C++标准库函数std::to_string()
,可用于将数值类型转换为string。使用时需在程序中包含如下代码:using namespace std;
to_string()
函数的原型如下:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
代码清单
string s;int val=100;
s=to_string (val);
cout<<s<<endl;
把string类型字符串转换为数值类型
(1)使用函数模板
+istringstream
代码清单
#include <iostream>
#include <sstream> //使用stringstream需要引入这个头文件
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str){
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[]){
string str("00801");
int a=stringToNum<int>(str);
cout << a << endl;
return 0;
}
(2)使用C++标准库函数
使用C++11引入的C++库函数将string转换为数值类型,相应的库函数申明于头文件<string>
中。
名称 | 函数原型 | 描述 |
stoi | int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to integer (function template ) |
stol | long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long int (function template) |
stoul | unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned integer (function template) |
stoll | long long stoll (const string& str, size_t* idx = 0, int base = 10); long long stoll (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long long (function template) |
stoull | unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10); unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned long long (function template) |
stof | float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); | Convert string to float (function template ) |
stod | double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); | Convert string to double (function template ) |
stold | long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0); | Convert string to long double (function template) |
形参说明:
str:重载了string和wstring版本,表示被转换的字符串。
idx:表示一个size_t*的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符的下标。一般情况下,因为它是直接char型指针把最后非数值字符的地址值和起始地址值相减,所以也表示成功转换的字符数量,如”10”转成功为数值10时,*idx的值为2。
base:表示转换基准,默认是10进制。