VC++——CString

(1)CString介绍

首先,CString没有基类。

一个CString对象由一个可变长度的字符序列组成。CString使用类似于Basic的语法提供函数和操作符。连接和比较运算符以及简化的内存管理使CString对象比普通字符数组更容易使用。

CString基于TCHAR数据类型。如果符号_UNICODE是为您的程序定义的,则TCHAR被定义为类型wchar_t,一个16位字符类型; 否则,它被定义为char,即8位字符的正常类型。在Unicode下,CString对象由16位字符组成。没有Unicode,它们由8位字符类型组成。

不使用_UNICODE时CString为多字节字符集(MBCS,也称为双字节字符集,DBCS)启用。请注意,对于MBCS字符串,CString仍然会根据8位字符对字符串进行计数,返回和操作,并且您的应用程序必须自行解释MBCS前导字节和尾字节。

CString对象还具有以下特征:

  • CString对象可以由于连接操作而增长。

  • CString对象遵循“值语义”。将CString对象视为实际字符串,而不是指向字符串的指针。

  • 可以自由地将CString对象替换const char \ *和LPCTSTR函数参数。

  • 转换运算符可以直接访问字符串的字符,作为只读字符数组(C风格的字符串)。

提示 *尽可能在框架上分配CString对象,而不是在堆上分配这可以节省内存并简化参数传递。

CString通过允许两个字符串共享相同的值来共享相同的缓冲区空间来帮助您节省内存空间。但是,如果尝试直接更改缓冲区的内容(不使用MFC),则可以无意中更改这两个字符串。CString提供了两个成员函数CString :: LockBufferCString :: UnlockBuffer来帮助你保护你的数据。当你调用LockBuffer时,您创建一个字符串的副本,然后将引用计数设置为-1,从而“锁定”缓冲区。当缓冲区被锁定时,其他字符串不能引用该字符串中的数据,并且锁定的字符串不会引用另一个字符串。通过将字符串锁定在缓冲区中,确保字符串对数据的独占保持不变。完成数据后,调用UnlockBuffer将引用计数重置为1。

头文件:#include <afx.h>

https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa300688(v=vs.60)

(2)CString 函数介绍


(1)CString构造函数

CString();
CString(const CString& stringSrc);
throw(CMemoryException);
Parameters://参数介绍
stringSrc
从已有的CString对象拷贝到新建的CString对象中

CString(TCHAR ch, int nRepeat = 1);
throw(CMemory Exception);
Parameters://参数介绍
ch
单个字符 ch 重复 nRepeat 次.换句话说就是nRepeat个ch
nRepeat
重复次数

CString(LPCTSTR lpch, int nLength);
throw(CMemory Exception);
Parameters://参数介绍
lpch
指向长度为 nLength 的字符数组的指针,不是空值 - 终止。
nLength
pch中的字符长度.

CString(const unsigned char \psz); *
throw(CMemory Exception);
Parameters://参数介绍
psz
复制一个空终止的字符串到新建CString对象中.

CString(LPCWSTR lpsz);
throw(CMemory  Exception);
Parameters://参数介绍
lpsz
复制一个空终止的字符串到新建CString对象中.

CString(LPCSTR lpsz);
throw(CMemory Exception);
Parameters://参数介绍
lpsz
复制一个空终止的字符串到新建CString对象中.

备注:
每个构造函数用特定的数据初始化一个新CString对象

因为构造函数会将输入数据复制到新分配的存储中,所以应该意识到可能会导致内存异常。
请注意,其中一些构造函数充当转换函数。这可以替换例如预期会使用CString对象的LPTSTR。

几种形式的构造函数有特殊用途:
CString(LPCSTR lpsz)从 ANSI 字符串构造 Unicode CString。
CString(LPCWSTRlpsz)从 Unicode 字符串构造 Unicode CString。
CString(const unsigned char\psz) 从无符号字符指针构造CString。

CString构造的例子:

// example for CString::CString
CString s1;                    // Empty string
CString s2( "cat" );           // From a C string literal
CString s3 = s2;               // Copy constructor
CString s4( s2 + " " + s3 );   // From a string expression

CString s5( 'x' );             // s5 = "x"
CString s6( 'x', 6 );          // s6 = "xxxxxx"

CString s7((LPCSTR)ID_FILE_NEW); // s7 = "Create a new document"

CString city = "Philadelphia"; // NOT the assignment operator

(2)成员函数

int GetLength() const;

返回值
字符串长度.

备注
返回值不包含终止位;

对于多字节字符集(MBCS),GetLength计算每个8位字符; 也就是说,一个多字节字符中的首尾字节计为两个字节.

例子:

//判断 s 的长度是不是 6
CString s( "abcdef" );
ASSERT( s.GetLength() == 6 );

BOOL IsEmpty() const;

返回值
CString长度为0,返回真(非0值);长度不为0,返回假(0)。
备注

测试一个CString对象是否为空.

例子:

// example for CString::IsEmpty
CString s;
ASSERT( s.IsEmpty() );//s 非空报错

void Empty();

备注:

强制使CString对象为空(恰当的释放内存)

例子:

// example for CString::Empty
CString s( "abc" );
s.Empty();
ASSERT( s.GetLength( ) == 0 );//s 非空报错

TCHAR GetAt(int nIndex)const;

返回值
nIndex位置上的TCHAR
参数介绍
int nIndex
CString对象中字符的从零开始的索引.nIndex参数必须大于或等于0并小于GetLength返回的值.
Microsoft基础类库的调试版验证nIndex的边界; 发布版本没有。

备注

可以将CString对象视为一个字符数组。GetAt成员函数返回由索引号指定的单个字符。重载的下标([])运算符是GetAt的方便别名。

例子:

// example for CString::GetAt
CString s( "abcdef" );
ASSERT( s.GetAt(2) == 'c' );//如果第3位不是c报错

TCHAR operator[](int nIndex)const;

和TCHAR GetAt(int nIndex)const;的功能一样。

void SetAt(int nIndex, TCHAR ch);

参数介绍
int nIndex
CString对象中字符的从零开始的索引.nIndex参数必须大于或等于0并小于GetLength返回的值.
TCHAR ch
要插入的字符.
备注
可以将CString对象视为一组字符。SetAt成员函数覆盖由索引号指定的单个字符。如果索引超出现有字符串的范围,则不会放大该字符串。

operator LPCTSTR()const;

返回值
指向字符串数据的指针.
备注
这个有用的转换运算符提供了一种有效的方法来访问包含在CString对象中的以空值结尾的C字符串。
只有一个指针返回。请小心操作这个操作符。

如果在获取字符指针后更改CString对象,则可能会导致内存重新分配,导致指针无效

例程

/ If the prototype of a function is known to the compiler,
// the LPCTSTR cast operator may be invoked implicitly

CString strSports(_T("Hockey is Best!"));
TCHAR sz[1024];

lstrcpy(sz, strSports);

// If the prototype isn't known, or is a va_arg prototype,
// you must invoke the cast operator explicitly. For example,
// the va_arg part of a call to sprintf() needs the cast:

sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);

// while the format parameter is known to be an LPCTSTR and
// therefore doesn't need the cast:

sprintf(sz, strSports);

// Note that some situations are ambiguous. This line will
// put the address of the strSports object to stdout:

cout << strSports;

// while this line will put the content of the string out:

cout << (LPCTSTR) strSports;

CString::operator =

const CString& operator=(const CString& stringSrc);
throw(CMemory Exception);
const CString& operator=(TCHAR ch);
throw(CMemory Exception);
const CString& operator=(const unsigned char \psz); *
throw(CMemory Exception);
const CString& operator=(LPCWSTR lpsz);
throw(CMemory Exception);
const CString& operator=(LPCSTR lpsz);
throw(CMemory Exception);
Remarks
CString赋值( = )运算符用新数据重新初始化现有CString对象。
如果目标字符串(即左侧)已足够大以存储新数据,则不会执行新的内存分配。

应该知道 内存异常可能会在使用赋值运算符时发生,因为通常会分配新的存储空间来保存生成的CString对象。

例程

// example for CString::operator =
CString s1, s2;        // Empty CString objects

s1 = "cat";            // s1 = "cat"
s2 = s1;               // s1 and s2 each = "cat"
s1 = "the " + s1;      // Or expressions
s1 = 'x';              // Or just individual characters

CString::operator +

friend CString operator + (const CString& string1, const CString& string2);
throw(CMemory Exception);
friend CString operator + (const CString& string, TCHAR ch);
throw(CMemory Exception);
friend CString operator + (TCHARch, constCString& string);
throw(CMemory Exception);
friend CString operator + (const CString& string, LPCTSTR lpsz);
throw(CMemory Exception);
friend CString operator + (LPCTSTR lpsz, const CString& string);
throw(CMemory Exception);
返回值
CString对象是串联的临时结果。该返回值可以在同一个表达式中组合多个串联。
参数介绍
CString& string, string1, string2
要连接的CString对象。
TCHAR ch
连接字符到字符串的字符.
LPCTSTR lpsz
指向空终止字符串的指针。
备注
+ 连接运算符连接两个字符串并返回一个CString对象。两个参数字符串中的一个必须是CString对象。另一个可以是字符指针或字符。
应该知道,每当使用串联时都会发生内存异常 因为新的存储可能被分配来保存临时数据。

例程

// example for CString::operator +
CString s1( "abc" );
CString s2( "def" );
ASSERT( (s1 + s2 ) == "abcdef" );
CString s3;
s3 = CString( "abc" ) + "def" ; // Correct
s3 = "abc" + "def";
// Wrong! The first argument must be a CString.





猜你喜欢

转载自blog.csdn.net/thecentry/article/details/80638872
今日推荐