C++语法基础--标准库类型--string(2)

1.从标准输入读取string
  *忽略开头所有的空白字符(空格,换行符,制表符)
  *直至再次遇到空白符,读取终止
 eg:
    string s;
    cin>>s;  
 //假设输入"    hello    "
    cout<<s;    //输出hello(注意,开头和结尾并没有空格)


2.getline
  原型:
      (1)    istream& getline (istream& is, string& str, char delim); //遇到分割符后文件结束符才停止读取
      (2)      istream& getline (istream& is, string& str);   //正常读取模式
  eg:
    string s;
    getline(cin,s,',');
 //假设输入hello  how,are you
    cout<<s;   //输出hello   how
    注:
         *getline和string类型的输入对空白符的处理区别:getline不会忽略行开头的空白符,读取字符直至遇到换行符,读取终止并丢弃换行符
  eg:
      int main()
    {
     string str1;
int count=0;
while(cin>>str1)    
//一次性输入"hello how are you
{
++count;
cout<<count<<": "<<str1<<'\t';   //输出1:hello  2:how 3:are 4:you  可见每遇到一处空白符count就加1


}



return 0;
     }


 eg:
    int main()
   {
     string str;


getline(cin,str);
//输入"   hello how are you";注意前面的空白符,引号并不在包含在内,下同
cout<<str<<endl;//输出"   hello how are you"
return 0;
   }
   


3.stirng类的常用方法
   *size;
       原型:
          size_t size() const;
//size_t is the type returned by the sizeof operator  
    eg:
       int main ()
    {
          std::string str ("hello");
          std::cout << "The size of str is:" << str.size();
//The size of str is:5
          return 0;
     }


   *empty:
       原型:
           bool empty() const;

    eg:
         int main ()
     {
           std::string str;
           if(str.empty())
            {
                std::cout << "The str is empty." ;
//The str is empty.
             }
         return 0;
      }


4.成员类型
   *size_type:
          string类的配套类型,它在不同的机器上,长度是可以不同的,并非固定的长度,事实上许多库类型都定义了一些配            套类型
    eg:
        int main ()
      {
          string str("hello");
          string::size_type len=str.size();
          cout<<len;
//5
          return 0;
       }


5.关系操作符(非成员函数):
   按字典顺序比较两个字符串 
   +,==,!=,<,>,<=,>=
   eg:
      int main()
     {
       string str1("hello");
string str2("nihao");
cout<<(str1>str2)<<endl;  
//0
cout<<(str1==str2)<<endl;//0
cout<<(str1!=str2)<<endl;//1
cout<<(str1<str2)<<endl;  //1
cout<<(str1<=str2)<<endl; //1
cout<<(str1>=str2)<<endl; //0
return 0;
     }
     
6.string对象的赋值
  operator=:
      原型:
         basic_string& operator=( const basic_string& str );
         basic_string& operator=( const CharT* s );
basic_string& operator=( CharT ch )

 eg:
   int main()
 {
    string str1("nihao");
   string str2("hello");
   str1=str2;
   cout<<str1<<endl;
//hello
   str1="tanks";
   cout<<str1<<endl;//thanks
   str1='A';
   cout<<str1<<endl;//A
return 0;
  }


6.string对象相加(非成员函数)
  operator+:
    原型:
        template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
        operator+( const basic_string<CharT,Traits,Alloc>& lhs,
                   const basic_string<CharT,Traits,Alloc>& rhs );

       template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
        operator+( const CharT* lhs,
                   const basic_string<CharT,Traits,Alloc>& rhs );


       template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
        operator+( CharT lhs,
                   const basic_string<CharT,Traits,Alloc>& rhs );

        template< class CharT, class Traits, class Alloc >
basic_string<CharT,Traits,Alloc>
        operator+( const basic_string<CharT,Traits,Alloc>& lhs,
                   const CharT* rhs );

        template<class CharT, class Traits, class Alloc>
basic_string<CharT,Traits,Alloc>
        operator+( const basic_string<CharT,Traits,Alloc>& lhs,
                    CharT rhs );

  eg:
    int main()
  {
     string str1("hello,");
     string str2("nihao,");
     cout<<str1+str2<<endl;
//hello,nihao,
     cout<<"Hi,"+str2<<endl; //Hi,nihao,
     cout<<'A'+str2<<endl; //Anihao,
     cout<<str1+"nihao,"<<endl; //hello,nihao,
     cout<<str1+'B'<<endl; //hello,B
    return 0;
  }


7.下标操作符:
   访问指定的字符
   operator[]
      原型:
          reference       operator[]( size_type pos );
          const_reference operator[]( size_type pos ) const;

    eg:
       int main ()
     {
          std::string str ("hello");
          for (int i=0; i<str.size(); ++i)
           {
              std::cout << str[i];
//hello
            }
       return 0;
    }


8.字符的处理(#include<cctype>)

    *cctype常用函数:



  int main ()
{
  string str ("hello,how are you!");
  int count=0;
  for (int i=0; i<str.size(); ++i)
  {
 if(ispunct(str[i]))
 {
++count;
 }
str[i]=toupper(str[i]);
  }
  cout<<str<<endl;
//HELLO,HOW ARE YOU!
  cout<<"punctuation count: "<<count;//punctuation count:2
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值