<cstring>、<string.h>和<string>用法区别总结
来源:互联网 发布:刘吉桦陈铭 知乎 编辑:程序博客网 时间:2024/05/20 05:10
我相信很多人和我一样,在用到这三个头文件的时候总是稀里糊涂的,现总结如下,具体有什么疑问可查询MSDN。
总体来说:
<string.h>是旧的C 头文件,对应的是基于char*的字符串处理函数;
<cstring>是对应于旧C 头文件的std 版本,主要用在MFC中;
<string>是包装了std 的C++头文件,是新的string 类,其分两种对应的是char和wchar_t,其对应的命名空间分别是using std::string;using std::wstring;当然两者可以统一用 using namespace std;
一句话:<string.h>和<cstring>基本没有分别;<string.h>和<string>是完全两个不同的头文件,只是像而已。
为了更好的使用<string.h>和<string>,下面看下这个头文件各包含哪些函数:
这是MSDN下的<string.h>的成员函数,具体可参见http://msdn.microsoft.com/en-us/library/tk97t156(v=vs.90).aspx
#if <TRADITIONAL C HEADERS> #include <string.h> namespace std { using ::memchr; using ::memcmp; using ::memcpy; using ::memmove; using ::memset; using ::size_t; using ::strcat; using ::strchr; using ::strcmp; using ::strcoll; using ::strcpy; using ::strcspn; using ::strerror; using ::strlen; using ::strncat; using ::strncmp; using ::strncpy; using ::strpbrk; using ::strrchr; using ::strspn; using ::strstr; using ::strtok; using ::strxfrm; } #endif下面是<string>的成员函数,具体可参见http://msdn.microsoft.com/en-us/library/5zz6weyz(v=vs.71).aspx
Typedefs
A type that represents the type of characters stored in a string.Member Functions
Operators
综上,如果你想用strcat函数你就要添加<string.h>头文件,如果你想用到find函数你就要添加<string>的头文件了。
不过,相信你想这儿会想到CString类,它和上面提到的String类有什么区别呢?
CString是属于MFC的类,string类是属于标准c++库的,而且使用CString时要添加头文件"afx.h",不过通常添加“stdafx.h"即可。
CString中含有一些更加强大的功能,具体可看下http://msdn.microsoft.com/en-us/library/aa315043(v=vs.60).aspx#_mfc_construction_cstring
Construction
CStringConstructs CString objects in various ways.The String as an Array
GetLengthReturns the number of characters in a CString object. For multibyte characters, counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.IsEmptyTests whether a CString object contains no characters.EmptyForces a string to have 0 length.GetAtReturns the character at a given position.operator []Returns the character at a given position — operator substitution forGetAt.SetAtSets a character at a given position.operator LPCTSTRDirectly accesses characters stored in a CString object as a C-style string.Assignment/Concatenation
operator =Assigns a new value to a CString object.operator +Concatenates two strings and returns a new string.operator +=Concatenates a new string to the end of an existing string.Comparison
operator == <, etc.Comparison operators (case sensitive).CompareCompares two strings (case sensitive).CompareNoCaseCompares two strings (case insensitive).CollateCompares two strings (case sensitive, uses locale-specific information).CollateNoCaseCompares two strings (case insensitive, uses locale-specific information).Extraction
MidExtracts the middle part of a string (like the Basic MID$ function).LeftExtracts the left part of a string (like the Basic LEFT$ function).RightExtracts the right part of a string (like the Basic RIGHT$ function).SpanIncludingExtracts a substring that contains only the characters in a set.SpanExcludingExtracts a substring that contains only the characters not in a set.Other Conversions
MakeUpperConverts all the characters in this string to uppercase characters.MakeLowerConverts all the characters in this string to lowercase characters.MakeReverseReverses the characters in this string.ReplaceReplaces indicated characters with other characters.RemoveRemoves indicated characters from a string.InsertInserts a single character or a substring at the given index within the string.DeleteDeletes a character or characters from a string.FormatFormat the string as sprintf does.FormatVFormats the string as vsprintf does.TrimLeftTrim leading whitespace characters from the string.TrimRightTrim trailing whitespace characters from the string.FormatMessageFormats a message string.Searching
FindFinds a character or substring inside a larger string.ReverseFindFinds a character inside a larger string; starts from the end.FindOneOfFinds the first matching character from a set.Archive/Dump
operator <<Inserts a CString object to an archive or dump context.operator >>Extracts a CString object from an archive.Buffer Access
GetBufferReturns a pointer to the characters in the CString.GetBufferSetLengthReturns a pointer to the characters in the CString, truncating to the specified length.ReleaseBufferReleases control of the buffer returned by GetBuffer.FreeExtraRemoves any overhead of this string object by freeing any extra memory previously allocated to the string.LockBufferDisables reference counting and protects the string in the buffer.UnlockBufferEnables reference counting and releases the string in the buffer.Windows-Specific
AllocSysStringAllocates a BSTR from CString data.SetSysStringSets an existing BSTR object with data from a CString object.LoadStringLoads an existing CString object from a Windows resource.AnsiToOemMakes an in-place conversion from the ANSI character set to the OEM character set.OemToAnsiMakes an in-place conversion from the OEM character set to the ANSI character set.另外需要说明的是,CString类和String类通常需要相互转化来调用对应的函数:CStringcstr(str.c_str());//或者CString cstr(str.data());初始化时才行
cstr=str.c_str();或者cstr=str.data();
str=cstr.GetBuffer(0); //CString -> string
cstr.format("%s", str.c_str()); //string->CString
cstr.format("%s", str.data()); //string->CString
str = LPCSTR(cstr); //CString->string
/*c_str()和data()区别是:前者返回带'/0'的字符串,后者则返回不带'/0'的字符串*/
CStringstr;
str.format("%d",i);//int->CString 其他的基本类型转化类似
i=atoi(str);//CString->int 还有(atof,atol)
char* ptemp=cstr.getbuffer(0);
char* str;
strcpy(str,ptemp);//CString->char*
cstr.releasebuffer(-1);
char*str="lovesha";
CStringcstr=str;//char*->CString string类型不能直接赋值给CString
Reference:
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html
http://msdn.microsoft.com/en-us/library/tk97t156(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/5zz6weyz(v=vs.71).aspx
http://blog.csdn.net/bitxinhai/article/details/2292014
http://msdn.microsoft.com/en-us/library/aa315043(v=vs.60).aspx#_mfc_construction_cstring
- <cstring>、<string.h>和<string>用法区别总结
- 整理:CString类的完美总结&&CString 成员函数用法大全&&string 与 CString 转化&&<string><string.h> 和<cstring>的区别
- string和string.h和cstring 区别
- string string.h cstring 区别
- <string.h> <string> 和<CString>
- <string>、<cstring>和<string.h>
- string、string.h与cstring 的作用和区别
- <string> <string.h> <cstring>的区别和关联
- C++:string,string.h和cstring的区别
- string 与 string.h 和 cstring 的区别
- 头文件 string.h cstring string 区别
- 头文件 string.h cstring string 区别
- 头文件 string.h cstring string 区别
- <string> 与<string.h>、<cstring>的区别
- <string> 与<string.h>、<cstring>的区别
- 头文件 string.h cstring string 区别
- 头文件 string.h cstring string 区别
- 头文件 string.h cstring string 区别
- 线段树成段跟新个人总结
- 黑马程序员———Java 8 最新版本
- 使用Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)判断两个集合是否相同
- Windows驱动_文件系统过滤驱动之三
- 计算机科学中最重要的32个算法
- <cstring>、<string.h>和<string>用法区别总结
- 黑马程序员———Eclipse快捷键大全
- Window 窗口属性及嵌入窗口到D3D渲染窗口顶层
- Linux内核的配置与编译(X86平台)
- 计算机中的端口
- MBA最全的面试训练
- PAT_1006: Sign In and Sign Out
- 黑马程序员———Java的三种流程控制
- 黑马程序员---银行管理系统