C++学习:最全标准库类型string的使用

来源:互联网 发布:运营的数据分析水平 编辑:程序博客网 时间:2024/06/05 00:19

C++学习:标准库类型string


摘要
string标准库是C++提供的抽象数据类型,也是一种非常重要的数据结构.主要用于字符串之间的操作
中文的字符编码标准是GB18030,一般的汉字占用的内存为两个字节
并且string类型是支持可变长度的字符串


版权说明

本文作者:章飞_906285288
作者博客:http://blog.csdn.net/qq_29924041
本文是参考C++api中的string类来总结的


使用:

C++标准库负责管理和存储字符串所占用的内存#include<string>using namespace::std;

string的对象的初始化的方法:

  string();  string( size_type length, char ch );  string( const char *str );  string( const char *str, size_type length );  string( string &str, size_type index, size_type length );  string( input_iterator start, input_iterator end );字符串的构造函数创建一个新字符串,包括: 以length为长度的ch的拷贝(即length个ch) 以str为初值 (长度任意), 以index为索引开始的子串,长度为length, 或者 以从start到end的元素为初值. 例如, string str0;string str1(s1);string str2( 5, 'c' );//将字符串初始化为字符'c'的n个副本string str3( "Now is the time..." );string str4( str2, 11, 4 );string str = "helloworld";

string的相关简单函数以及操作符号:

s.empty()  //判断字符串是否为空,返回true或者falses.size()   //返回字符串中字符的个数s[n]    //返回字符串中的第n个字符,下标从0开始s1+s2   //将s1和s2连接成一个新的字符串,返回新生成的字符串s1=s2   //将s1的内容替换成s2的内容v1==v2  //比较s1和s2的内容,判断其内容是否一样!=,<,<=,>,>=  //保持这些操作符号惯有的特性如:    string str;    if(str.empty()){        cout <<"str is empty"<<endl;    }

string类中的添加文本(append)的函数

asic_string &append( const basic_string &str );basic_string &append( const char *str );basic_string &append( const basic_string &str, size_type index, size_type len );basic_string &append( const char *str, size_type num );basic_string &append( size_type num, char ch );basic_string &append( input_iterator start, input_iterator end );append() 函数可以完成以下工作:在字符串的末尾添加str, 在字符串的末尾添加str的子串,子串以index索引开始,长度为len在字符串的末尾添加str中的num个字符,在字符串的末尾添加num个字符ch,在字符串的末尾添加以迭代器start和end表示的字符序列.如:string str1("hello");   string str2("world");   str1.append("hello"); //str1后面追加一个hello   str1.append(str2); //str1后面追加str2的字符   str1.append(10,"!"); //str1后面追加10个!   str1.append(str2,2,2);//从str2的第二位开始,添加2位,即添加

string类的赋值函数

basic_string &assign( const basic_string &str );basic_string &assign( const char *str );basic_string &assign( const char *str, size_type num );basic_string &assign( const basic_string &str, size_type index, size_type len );basic_string &assign( size_type num, char ch );函数以下列方式赋值:    用str为字符串赋值,    用str的开始num个字符为字符串赋值,    用str的子串为字符串赋值,子串以index索引开始,长度为len    用num个字符ch为字符串赋值.如:    string str1;    string str2("hello world");    str1.assign(str2);    str1.assign(str2,5,5);    str1.assign("world");    str1.assign("hello world",5,5);    str1.assign(15,"n");

string的at函数,返回下标对应的字符

  reference at( size_type index );  at()函数返回一个引用,指向在index位置的字符.    如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常。  比如下列代码:    string text = "ABCDEF";    char ch = text.at( 2 );

string的begin函数,返回的是一个指向第一个元素的迭代器

  iterator begin();  begin()函数返回一个迭代器,指向字符串的第一个元素.  如:  string str("12345678");  iterator it = str.begin();

string的c_str函数:

const char *c_str();返回:    返回一个指向正规C字符串的指针,内容与本字符串相同;    String str("helloworld");    char * char_str = str.c_str();//将字符串string转化成char*(字符指针)

string的capacity函数:

  size_type capacity();  capacity()函数返回在重新申请更多的空间前字符串可以容纳的字符数.  这个数字至少与size()一样大. string str("ni hao "); int num = str.capacity();

string的比较函数campare

int compare( const basic_string &str );int compare( const char *str );int compare( size_type index, size_type length, const basic_string &str );int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 );int compare( size_type index, size_type length, const char *str, size_type length2 ); compare()函数以多种方式比较本字符串和str,返回:返回值情况    小于零    this < str    零       this == str    大于零    this > str比较自己和str,比较自己的子串和str,子串以index索引开始,长度为length比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

string的拷贝函数

size_type copy( char *str, size_type num, size_type index );copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数string str("1234567");char * str2= "helloworld";int num = str.copy(str2,5,0);

string的data函数

const char* data();返回:    函数返回指向自己第一个字符的指针

string的empty()函数

bool empty();返回:    如果字符串为空则为true,否则为false

string的end函数

iterator end();返回:    end函数返回一个迭代器,指向这个字符串的尾部string str_1("i want to study c++");iterator it = str_1.end();

string的删除函数erase()

  iterator erase( iterator pos );  iterator erase( iterator start, iterator end );  basic_string &erase( size_type index = 0, size_type num = npos );删除pos指向的字符, 返回指向下一个字符的迭代器,删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置删除从index索引开始的num个字符, 返回*this.参数index 和 num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符

string的find函数:

  size_type find( const basic_string &str, size_type index );  size_type find( const char *str, size_type index );  size_type find( const char *str, size_type index, size_type length );  size_type find( char ch, size_type index );  find()函数:    返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,    返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,    返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos    如:        string str1( "Alpha Beta Gamma Delta" );        unsigned int loc = str1.find( "Omega", 0 );        if( loc != string::npos ){            cout<<loc<<endl;        }

string的查找函数:find_first_of,查找 字符串中与str某个字符相同的字符,返回它的下标

  size_type find_first_of( const basic_string &str, size_type index = 0 );  size_type find_first_of( const char *str, size_type index = 0 );  size_type find_first_of( const char *str, size_type index, size_type num );  size_type find_first_of( char ch, size_type index = 0 );find_first_of()函数:查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返string::npos,  查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。

string中查找第一个与str都不匹配的的字符find_first_not_of,返回它的位置

 size_type find_first_not_of( const basic_string &str, size_type index = 0 );  size_type find_first_not_of( const char *str, size_type index = 0 );  size_type find_first_not_of( const char *str, size_type index, size_type num );  size_type find_first_not_of( char ch, size_type index = 0 );返回:  在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops  

string函数的find_last_of以及find_last_not_of

如上所示:
既然有find_first_of肯定也就有对应的find_last_of,find_last_not_of
find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置
find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置

 size_type find_last_of( const basic_string &str, size_type index = npos );  size_type find_last_of( const char *str, size_type index = npos );  size_type find_last_of( const char *str, size_type index, size_type num );  size_type find_last_of( char ch, size_type index = npos );

find_last_of()函数:

在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

find_last_not_of

 size_type find_last_not_of( const basic_string &str, size_type index = npos );  size_type find_last_not_of( const char *str, size_type index = npos);  size_type find_last_not_of( const char *str, size_type index, size_type num );  size_type find_last_not_of( char ch, size_type index = npos );在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops 在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

string类的获取配置器的函数get_allocator

get_allocator语法:  allocator_type get_allocator();  get_allocator()函数返回本字符串的配置器.

string的插入(insert)函数

iterator insert( iterator i, const char &ch );  basic_string &insert( size_type index, const basic_string &str );  basic_string &insert( size_type index, const char *str );  basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );  basic_string &insert( size_type index, const char *str, size_type num );  basic_string &insert( size_type index, size_type num, char ch );  void insert( iterator i, size_type num, const char &ch );  void insert( iterator i, iterator start, iterator end );

insert()函数的功能非常多:

在迭代器i表示的位置前面插入一个字符ch,  在字符串的位置index插入字符串str,在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),在字符串的位置index插入字符串str的num个字符,在字符串的位置index插入num个字符ch的拷贝,在迭代器i表示的位置前面插入num个字符ch的拷贝, 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束.

string的长度length()函数

语法:  size_type length();  length()函数返回字符串的长度.  这个数字应该和size()返回的数字相同.

string的最大长度max_size

size_type max_size();max_size()函数返回字符串能保存的最大字符数。

string的获取逆向迭代器的函数rbegin和rend函数

const reverse_iterator rbegin();rbegin()返回一个逆向迭代器,指向字符串的最后一个字符。rend:  const reverse_iterator rend();  rend()函数返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。

string的替换函数replace

basic_string &replace( size_type index, size_type num, const basic_string &str );  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 );  basic_string &replace( size_type index, size_type num, const char *str );  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );  basic_string &replace( iterator start, iterator end, const basic_string &str );  basic_string &replace( iterator start, iterator end, const char *str );  basic_string &replace( iterator start, iterator end, const char *str, size_type num );  basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函数:

用str中的num个字符替换本字符串中的字符,从index开始用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符用str中的num个字符(从index开始)替换本字符串中的字符用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符用num2个ch字符替换本字符串中的字符,从index开始用str中的字符替换本字符串中的字符,迭代器start和end指示范围用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.如:tring s = "They say he carved it himself...from a BIGGER spoon";string s2 = "find your soul-mate, Homer.";s.replace( 32, s2.length(), s2 );

string的保留空间函数reverse函数
保留空间(reserve)
语法:

  void reserve( size_type num );  reserve()函数设置本字符串的capacity 以保留num个字符空间。 

string改变文本字符串的大小函数resize

  void resize( size_type num );  void resize( size_type num, char ch );  resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。

string逆向查找refind函数

  size_type rfind( const basic_string &str, size_type index );  size_type rfind( const char *str, size_type index );  size_type rfind( const char *str, size_type index, size_type num );  size_type rfind( char ch, size_type index );

rfind()函数:

返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos如:int loc;string s = "My cat's breath smells like cat food.";loc = s.rfind( "breath", 8 );if(loc != string::npos){}

string中返回字符串中当前有的字符数函数(size)

  size_type size();  size()函数返回字符串中现在拥有的字符数。

string的截取函数substr

  basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
例如:

string s("What we have here is a failure to communicate");string sub = s.substr(10,5);

string的交换函数swap()

  void swap( basic_string &str );      swap()函数把str和本字符串交换。  例如:    string first( "This comes first" );    string second( "And this is second" );    first.swap( second );

以上就是一个关于string类中的成员函数的一些用法.

原创粉丝点击