StupidBeauty
Read times:3404Posted at: - no title specified

C++教程翻译:17.2 — std::string构造和析构,17.2 — std::string construction and destruction

在这个教程中,我们会学习如何构造std::string 对象,如何以数字为来源创建字符串,以及反过来的操作

字符串的构造

string类,有多个构造函数,可用于创建字符串。下面一一研究。

注意:string::size_type这个类型实为size_t,也就是由sizeof 操作符所返回的那个无符号整数类型。它的实际尺寸会依环境而不同。在这个教程中,我们就认为它是unsigned int就行了。

string::string()

  • •.这是默认的构造函数。它会创建一个空字符串。

示例代码

std :: string sSource ;

cout << sSource ;

输出

string::string(const string& strString)

  • •.这是复制构造函数。它以 strString 为蓝本,创建出一个新的字符串。

示例代码

string sSource ( "my string" );

string sOutput ( sSource );

cout << sOutput ;

输出

my string

string::string(const string& strString, size_type unIndex)
string::string(const string& strString, size_type unIndex, size_type unLength)

  • •. 这个构造函数,会创建一个新的字符串,其中,包含着strString 中从下标unIndex 开始的最多unLength 个字符。如果遇到了NULL,则,字符串的复制过程结束,即便未达到unLength 也会停止。

  • •.如果未指定unLength,那么,会使用从unIndex 开始的所有字符。

  • •.如果unIndex的值比给定字符串的尺寸还要大,那么,会抛出out_of_range 异常。

示例代码

string sSource ( "my string" );

string sOutput ( sSource , 3 );

cout << sOutput << endl ;

string sOutput2 ( sSource , 3 , 4 );

cout << sOutput2 << endl ;

输出

string

stri

string::string(const char *szCString)

  • •.这个构造函数,从C语言风格的字符串szCString 中复制字符,以创建一个新的字符串,一直复制到NULL 终止符为止,但不包含该终止符。

  • •.如果复制产生的尺寸超过了字符串长度的最大值,则,会抛出length_error 异常。

  • •. 警告:szCString必须不为空(NULL)。

示例代码

const char * szSource ( "my string" );

string sOutput ( szSource );

cout << sOutput << endl ;

输出

my string

string::string(const char *szCString, size_type unLength)

  • •.这个构造函数,通过复制C语言风格字符串szCString 中的前面unLength 个字符来创建一个新的字符串。

  • •.如果复制产生的尺寸超过了字符串长度的最大值,则,会抛出length_error 异常。

  • •. 警告: 这个函数是唯一一个不会将szCString 中的NULL当成字符串终止符处理的函数!这意味着,如果unLength 太大,那么,妳可能会读取到字符串结尾之后的内容。注意,不要引起妳的字符串缓冲区溢出了!

示例代码

const char * szSource ( "my string" );

string sOutput ( szSource , 4 );

cout << sOutput << endl ;

输出

my s

string::string(size_type nNum, char chChar)

  • •.这个构造函数,创建一个新的字符串,并且利用chChar 字符的nNum 个实例来初始化。

  • •.如果产生的尺寸超过了字符串长度的最大值,则,会抛出length_error 异常。

示例代码

string sOutput ( 4 , 'Q' );

cout << sOutput << endl ;

输出

QQQQ

template<class InputIterator> string::string(InputIterator itBeg, InputIterator itEnd)

  • •.这个构造函数,创建一个新字符串,并且使用[itBeg, itEnd)范围内的字符将它初始化。

  • •.如果产生的尺寸超过了字符串长度的最大值,则,会抛出length_error 异常。

没有示例。显然,妳极有可能用不上这个。

string::~string()

字符串构造函数

  • •.这是析构函数。它会销毁该字符串,并且释放它的内存。

也没有示例代码,因为,析构函数不是显式调用的。

利用数字来构造字符串

std::string 类中一个令人侧目的遗憾就是,不能利用数字来创建字符串。例如:

string sFour ( 4 );

会产生以下错误:

c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'

还记得吗?我说过,字符串类,其编译错误信息看起来狠恐怖。此处,关键的问题是:

cannot convert parameter 1 from 'int' to 'std::basic_string

换句话说,它尝试将此处的int转换为一个string,然而失败了。

将数字转换成字符串,最容易的方法是,使用std::ostringstream 类。std::ostringstream,可以接受多种来源的输入,包括字符、数字、字符串等等。它还能够输出字符串(通过抽取操作符operator>>,或者通过str()函数)。欲知更多关于std::ostringstream 的信息,则参考课程13.4 —— 针对字符串 的流式类

以下示例,展示的是,如何利用不同类型的输入来创建std::string:

#include <iostream>

#include <sstream>

#include <string>

template < typename T >

inline std :: string ToString ( T tX )

{

std :: ostringstream oStream ;

oStream << tX ;

return oStream . str ();

}

以下是对应的测试代码:

int main ()

{

string sFour ( ToString ( 4 ));

string sSixPointSeven ( ToString ( 6.7 ));

string sA ( ToString ( 'A' ));

cout << sFour << endl ;

cout << sSixPointSeven << endl ;

cout << sA << endl ;

}

输出内容:

4

6.7

A

注意:这种解决方法,未提供错误检查功能。在向oStream 中插入tX 的过程中可能会失败。一种适当的处理方式是,当转换失败时,抛出一个异常。

将字符串转换成数字

与上面的解决方案类似:

#include <iostream>

#include <sstream>

#include <string>

template < typename T >

inline bool FromString ( const std :: string & sString , T &tX )

{

std :: istringstream iStream ( sString );

return ( iStream >> tX ) ? true : false ;

}

以下是一些测试代码:

int main ()

{

double dX ;

if ( FromString ( "3.4" , dX ))

cout << dX << endl ;

if ( FromString ( "ABC" , dX ))

cout << dX << endl ;

}

输出:

3.4

注意,第二个转换失败了,返回了假(false)。

孟茜

Luo Zilin

黄花杓兰

孙宁

Your opinions
Your name:Email:Website url:Opinion content:
Recent comments
2017年4月~2019年4月垃圾短信排行榜Posted at:Thu Sep 26 04:51:48 2024
Qt5.7文档翻译:QWebEngineCookieStore类,QWebEngineCookieStore ClassPosted at:Fri Aug 11 06:50:35 2023盲盒kill -9 18289 Grebe.20230517.211749.552.mp4