C++基础教程面向对象(学习笔记(95))

std :: string构造和销毁

在本课中,我们将介绍如何构造std :: string的对象,以及如何创建字符串,反之亦然。

字符串构造

字符串类有许多可用于创建字符串的构造函数。我们将在这里看看他们。

注意:string :: size_type解析为size_t,这与sizeof运算符返回的无符号整数类型相同。其实际尺寸因环境而异。出于本教程的目的,将其视为unsigned int。

string::string()
这是默认构造函数。它创建一个空字符串。
示例代码:

std::string sSource;
cout << sSource;
Output:

string::string(const string& strString)
这是复制构造函数。此构造函数创建一个新字符串作为strString的副本.
示例代码:

string sSource("my string");
string sOutput(sSource);
cout << sOutput;
Output:

my string
string::string(const string& strString, size_type unIndex)
string::string(const string& strString, size_type unIndex, size_type unLength)
此构造函数创建一个新字符串,该字符串最多包含strString中的unLength字符,从索引unIndex开始。如果遇到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;
Output:

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;
Output:

my string
string::string(const char *szCString, size_type unLength)
此构造函数从C样式字符串szCString中的第一个unLength字符创建一个新字符串.
如果结果大小超过最大字符串长度,则抛出length_error异常.
警告:仅对于此函数,NULL不被视为szCString中的字符串结尾字符!这意味着如果unLength太大,可以读取字符串的结尾。小心不要溢出字符串缓冲区!
示例代码:

const char *szSource("my string");
string sOutput(szSource, 4);
cout << sOutput << endl;
Output:

my s
string::string(size_type nNum, char chChar)
此构造函数创建一个由字符chChar的nNum出现初始化的新字符串.
如果结果大小超过最大字符串长度,则抛出length_error异常.
示例代码:


string sOutput(4, 'Q');
cout << sOutput << endl;
Output:

QQQQ
template<class InputIterator> string::string(InputIterator itBeg, InputIterator itEnd)
此构造函数创建一个由range [itBeg,itEnd]字符初始化的新字符串.
如果结果大小超过最大字符串长度,则抛出length_error异常.
没有这个示例代码。它很模糊,你可能永远不会使用它.

string::~string()
String destruction

这是析构函数。它会破坏字符串并释放内存.
此处没有示例代码,因为未明确调用析构函数.

从数字构造字符串

std :: string类中一个值得注意的bug是缺乏从数字创建字符串的能力。例如:

string sFour(4);

产生以下错误:

c:vcprojectstest2test2test.cpp(10):错误C2664:‘std :: basic_string <_Elem,_Traits,_Ax> :: basic_string(std :: basic_string <_Elem,_Traits,_Ax> :: _ Has_debug_it)’:无法转换参数1 ‘int’到’std :: basic_string <_Elem,_Traits,_Ax> :: _ Has_debug_it’
还记得我所说的字符串类会产生可怕的错误吗?这里的相关信息是:

无法将参数1从’int’转换为’std :: basic_string
换句话说,它试图将您的int转换为字符串但失败了。

将数字转换为字符串的最简单方法是使用std :: ostringstream类。std :: ostringstream已经设置为接受来自各种来源的输入,包括字符,数字,字符串等…它还能够输出字符串(通过提取operator>>或通过str()函数) 。有关std :: ostringstream的更多信息,请参见字符串的Stream类。

这是从各种类型的输入创建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
一个
请注意,此解决方案省略了任何错误检查。将tX插入oStream可能会失败。如果转换失败,则适当的响应是抛出异常。

将字符串转换为数字

与上述解决方案类似:

#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; // extract value into tX, return success or not
}

这是一些测试它的示例代码:

int main()
{
    double dX;
    if (FromString("3.4", dX))
        cout << dX << endl; 
    if (FromString("ABC", dX))
        cout << dX << endl; 
}

并输出:

3.4
请注意,第二次转换失败并返回false。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值