string相关总结

一 <string>

   1 string类常见成员函数

     (1)属性相关

         s.empty()   s为空时返回true,否则返回false

         s.size()      返回s中字符的个数,不包括结尾标志字符

      (2)读取,设置字符串

         s.c_str()   返回c风格的字符串形式,注意:返回的字符指针是一个底层const指针

         basic_string substr( size_type pos = 0,size_type count = npos ) const;

                返回string对象的部分字符串 [pos, pos+count),默认为从第一个字符开始,最后一个字符结束

         s[n] 返回s中第n个字符的引用,n: [0,s.size()-1],超出范围的n将引发不可预知的结果

         s1 + s2  返回s1与s2连接后的结果(其中s1与s2至少有一个是string类型的,其他的可以是字符字面值或者字符串字面值)eg: string s6 = s1 + “,”+”world”这个语句是正确的,等价于string s6 = (s1 + “,”)+”world”

         s1 = s2 用s2的副本代替s1中原来的字符

 

      (3)比较字符串 s1== (!= > >= < <=)s2

         s1 == s2 : 长度相同,相同位置的字符也相同

         如果两个字符串的长度不相等,且较短的string对象的每个字符都与较长string对象对应位置的字符相同,就说较短string对象小于较长string对象

         如果两个string对象在某些对应位置上的字符不一致,则string对象的比较结果取决于第一个不相同的字符的asc2码值的大小比较

   2 该头文件中的其他工具函数(c++11)

int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );

int stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 );

long stol( const std::string& str, std::size_t* pos = 0, int base = 10 );

long stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 );

long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );

long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 );

 

unsigned long stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );

unsigned long stoul( const std::wstring& str, std::size_t* pos = 0, int base = 10 );

unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );

unsigned long long stoull( const std::wstring& str, std::size_t* pos = 0, int base = 10 );

 

float stof( const std::string& str, std::size_t* pos = 0 );

float stof( const std::wstring& str, std::size_t* pos = 0 );

double stod( const std::string& str, std::size_t* pos = 0 );

double stod( const std::wstring& str, std::size_t* pos = 0 );

long double stold( const std::string& str, std::size_t* pos = 0 );

long double stold( const std::wstring& str, std::size_t* pos = 0 );

 

std::string to_string( int value );

std::string to_string( long value );

std::string to_string( long long value );

std::string to_string( unsigned value );

std::string to_string( unsigned long value );

std::string to_string( unsigned long long value );

std::string to_string( float value );

std::string to_string( double value );

std::string to_string( long double value );

 

std::wstring to_wstring( int value );

std::wstring to_wstring( long value );

std::wstring to_wstring( long long value );

std::wstring to_wstring( unsigned value );

std::wstring to_wstring( unsigned long value );

std::wstring to_wstring( unsigned long long value );

std::wstring to_wstring( float value );

std::wstring to_wstring( double value );

std::wstring to_wstring( long double value );

 

二 <cctype>

        头文件中定义的处理字符的函数 →前12个可以跟正则表达式的12个特殊符号([:alnum:]等等)对比记忆

 

1 字母数字

   int isalnum( int ch ); [true: 0-9,a-z,A-Z]

   int isalpha ( int ch ); [true: a-z,A-Z]

   int isupper ( int ch ); [true: A-Z]

   int islower ( int ch ); [true: a-z]

   int isdigit ( int ch ); [true: 0-9]

   int isxdigit ( int ch ); [true: 0-9,a-f,A-F]

2 空白字符

    int isblank ( int ch ); [true: ‘ ’,‘ ’]

    int isspace ( int ch ); [true: ‘ ’, ‘f’,‘ ’,‘ ’, ‘v’, ‘ ’]

3 其他字符

    int iscntrl ( int ch ); [true:控制字符,即ASCII中[0,31]范围内,还有127的字符]

    int isprint ( int ch ); [true:可以打印的字符(字母, 数字, 标点符号, 空格),即ASCII中[32,126]范围内的字符]

    int isgraph ( int ch ); [true:除空格外的可打印字符,即ASCII中[33,126]范围内的字符]

    int ispunct ( int ch ); [true:标点字符,即graph中除了字母跟数字]

4 转换函数

    int tolower ( int ch );如果ch是大写字母,输出对应的小写字母,否则输出ch

    int touper ( int ch ); 如果ch是小写字母,输出对应的大写字母,否则输出ch

 

三 <cstring>

     该头文件里面有一些处理c风格字符串的函数,一般老项目或者面试笔试的时候面试官喜欢问,我刚毕业那会儿就被问了,而且没回答上来就被pass,哎,往事不堪回首……

    void* memcpy( void* dest, const void* src, std::size_t count );

        内存拷贝函数,把src指向的内容,按字节为单位复制到dest指向的内存区域.

        注意: 当两个指针指向的区域在count数量内有重叠的话会出问题

     void* memset( void* dest, int ch, std::size_t count );

         将dest指向的内存地址的前count个字符填充位ch

     char* strcpy( char* dest, const char* src );

         拷贝src指向的字符串到dest中,包括‘’

    std::size_t strlen( const char* str );

         返回str指向的字符串的长度,不包括‘’

 

四 <cstdlib>

    该头文件里面也还有一些处理c风格的字符串的函数。

         double atof( const char *str );

         int  atoi( const char *str );

         long atol( const char *str );

         long long atoll( const char *str );  //(since C++11)

 

 

 

原文地址:https://www.cnblogs.com/talenth/p/5807938.html