关于string类的构造函数及部分方法

1、构造函数
string(const char* s); 将string对象初始化为s字符串
string(size_type n,char c); 创建一个包含N个元素的string对象,其中每个元素都为c
string(const string & str);将一个string对象初始化为string对象str(复制构造)
string()创建一个默认的string对象,长度为0(默认构造)
string(const char *s ,size_type n)将string对象初始化为S指向的前n个字符
template<class Iter>  string(Iter begin,Iter end) 将string对象初始化为区间 begin end内的字符,其中begin和end类似指针,用于指定位置,包含begin不包含end
string(const string &str,string size_type pos=0,size_type n=npos)将一个string对象初始化为对象sr中从位置pos开始到结尾的字符 npos为最大位置,或从位置位置开始的n个字符

#include<iostream>
using namespace std;
int main(void)
{
        char tests[]="teststr";
        string a("test1");
        string b(10,'a');
        string c(a);
        string d;
        string e("test5",4);
        string f(tests+0,tests+5);
        string g(a,1,3);
        cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f<<endl<<g<<endl;
}


2、读取getline string的getline是独立的函数(友元?)
 24     string name1;
 25     string name2;
 26     getline(cin,name1,':');                                                                                                                                        
 27     getline(cin,name2);
 28     cout<<name1<<endl<<name2<<endl;


其特点
1、到达文件尾,eofbit设置,fail()和eof()设置为ture;
2、遇到分界符(默认为\n),在输入流中删除分解字符,不存储它
3、读取字符数达到最大值(string::npos和克分配内存字节数中较小的一个),在这种情况下
    将设置输入流的failbit,这说明fail()返回为ture;
string类重载了>>,当遇到空白字符就停止如空格、换行、制表符。


3、比较
  string类重载了 < == != > 等比较运算符,可以实现
  C字符串和string之间的比较,当然也可以string和sring进行比较
  如:
  int main(void)
{
        string a("abc");
        string b("abc");
        char c[20]="abm";
        char d[20]="abc";


        if(a == b)
        {
                cout<<"a b Y"<<endl;
        }
        if(a != c)
        {
                cout<< "a c N"<<endl;
        }
        if(b == d)
        {
                cout<<"b d Y"<<endl;
        }
}


4、find方法
string::npos 是字符串可以存储的最大字符,通常是无符号的int或者无符号的long的最大值
--size_type find(const string& str,size_type pos=0)const
从字符串的pos位置开始,查找子字符串str。如果找到,则返回字符串首次出现时其首字符的索引,否则返回string::npos
--size_type find(const char* s,size_type pos = 0) const
从字符串的pos位置开始,查找子字符串s,如果找到,则返回该字符串首次出现时其首字符的索引,否则返回string::npos
--size_type find(const char* s,size_type pos=0,size_type n)
从字符串的pos位置开始,查找s的前N个字符组成的子字符串,如果找到,则返回该子字符串首次出现是其字符的索引,否则返回sting::npos
--size_type find(char ch,size_type pos=0) const
从字符串的pos位置开始,查找字符ch,如果找到则返回该字符首次出现的位置,否则返回string::npos


如:
string b = "test";
int n = b.find('s',1);
cout<<n<<endl;


5、capacity(),size(),reserve(n)
capacity() 查看string的内存容量字节
size()     查看string实际数据大小字节
reserve(n) 将string的内存容量更改为指定的n字节


如:
        string b = "test";
        string c;
        string d = "ttttttttttttttttttttttttttt";
        cout<<b.capacity()<<endl; //输出4
        cout<<b.size()<<endl;     //输出4
        cout<<c.capacity()<<endl; //输出0
        cout<<c.size()<<endl;     //输出0
        cout<<d.capacity()<<endl; //输出27
        cout<<d.size()<<endl;     //输出27
        c.reserve(50);            //设置string c内存为50字节
        cout<<c.capacity()<<endl; //输出50
        cout<<c.size()<<endl;   //输出0


6、c_str()
c_str() 返回一个c const char*类型指针,这一点非常有用

  #include<iostream>
  using namespace std;
  
  int main(void)
  {
      string c ="test";
      const char* b;
      b = c.c_str();
      cout<<b<<endl;
  }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2122602/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2122602/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值