概述
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常用函数:
{
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;
}
最后
以上就是谨慎小鸭子为你收集整理的C++语法基础--标准库类型--string(2)的全部内容,希望文章能够帮你解决C++语法基础--标准库类型--string(2)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复