您的位置:首页 > 其它

string类成员函数的使用方法(二)

2016-04-12 17:00 281 查看
成员函数:

1.append

功能:添加一个字符串或者字符到源字符后面

返回值:返回字符串本身地址

//添加三个字符到s1后面
string s1("hello");
s1.append(3, 'A');
cout <<"s1: " <<s1 << endl;

//添加一个字符串到s2后面
string s2("hello");
s2.append(" word");
cout << "s2: " << s2 << endl;

//从字符串中指定要添加字符的个数
string s3("hello");
s3.append(" word",2);
cout << "s3: " << s3 << endl;

//1是偏移量,4是截取的字符个数
string s4("hello");
s4.append(" word",1,4);
cout << "s4: " << s4 << endl;

string str(" word");
string s5("hello");
s5.append(str);
cout << "s5: " << s5 << endl;

//利用迭代器添加
string str1(" word");
string s6("hello");
s6.append(str.begin(),str.end());
cout << "s6: " << s6 << endl;

//利用c字符串添加
char* p = " word";
string s7("hello");
s7.append(p);
cout << "s7: " << s7 << endl;


2.assign

功能:将字符串的内容重新赋值

返回值:返回字符串本身地址

//用字符串覆盖原先内容
string s1("hello");
s1.assign("word");
cout << "s1: " << s1 << endl;

//用n个字符覆盖原先内容
string s2("hello");
s2.assign(3,'a');
cout << "s2: " << s2 << endl;

//截取一部分来覆盖原先内容
string s3("hello");
s3.assign("word",3);
cout << "s3: " << s3 << endl;

//1是偏移量,3是截取的字符个数
string s4("hello");
s4.assign("word",1,3);
cout << "s4: " << s4 << endl;

string str("word");
string s5("hello");
s5.assign(str);
cout << "s5: " << s5 << endl;

//利用迭代器
string str1("word");
string s6("hello");
s6.assign(str1.begin(),str1.end());
cout << "s6: " << s6 << endl;

//利用指针
char* p="word";
string s7("hello");
s7.assign(p,p+4);
cout << "s7: " << s7 << endl;


3.at

功能:返回字符串中指定元素的地址

operator[]比at函数读写更快更高效,但是at函数的优点在于可以检查参数是否合法,不合法抛出异常

返回值:指向第(n+1)个元素的地址

string s1("hello");
const string s2("hello");

cout << "s1[1] = " << s1.at(1) << endl;
cout << "s2[1] = " << s2.at(1) << endl;


4.back

功能:返回字符串的最后一个字符

string s("hello");
cout << s.back() << endl;//只是返回并不删除


5.begin

功能:返回第一个元素的迭代器地址

string s("hello");
string::iterator iter = s.begin();
string::const_iterator const_iter = s.begin();//常量迭代器不能修改元素

cout << "iter: " << *iter << endl;
*iter = 'e';
cout << "s: " << s << endl;
cout << "const_iter: " << *const_iter << endl;


6.c_str

功能:把string对象转化为c风格的以空结尾的字符串

返回值:返回指向C字符串首地址的指针

string s("hello");
cout << "C++string: " << s << endl;
cout << "Cstring: " << s.c_str() << endl;


7.data

功能:将一个string对象的内容转化为一个字符串列

string s("hello");
const char* p = s.data();//返回的字符串列不能被修改
cout << p << endl;


8.capacity

功能:basic_string容器不再分配更多内存的情况下,计算能容纳的元素个数.

返回值:返回该容器所剩下的空间

string s("hello");
int iCap = s.capacity();
int iSize = s.size(); //size和length是一样的效果,现在一般用size
int iLen = s.length();
cout << "iCap: " << iCap << endl;
cout << "iSize: " << iSize << endl;
cout << "iLen: " << iLen << endl;


9.cbegin

功能:返回第一个元素的常量迭代器地址

string s("hello");
string::const_iterator const_iter = s.cbegin();
cout << *const_iter << endl;


10.clear

功能:清空字符串内容

string s("hello");
s.clear();
cout << s << endl;


11.compare

功能:两个字符串全比较或者部分比较

//全比较
string s1("ABC");
string s2("ABD");
int iComp1 = s1.compare(s2);
if (iComp1 < 0) cout << "s1 < s2" << endl;
if (iComp1 == 0) cout << "s1 = s2" << endl;
if (iComp1 > 0) cout << "s1 > s2" << endl;

//一个字符串的一部分和另一个字符串的全部比较
string s3("ABDE");
string s4("DE");
int iComp2 = s3.compare(2,2,s4);//s3的后面两个和s4比较
if (iComp2 < 0) cout << "s3 < s4" << endl;
if (iComp2 == 0) cout << "s3 = s4" << endl;
if (iComp2 > 0) cout << "s3 > s4" << endl;

//两个字符串的某一部分比较
string s5("ABDE");
string s6("DE");
int iComp3 = s5.compare(3, 1, s6,1,1);//s5的最后1个和s6的最后一个比较
if (iComp3 < 0) cout << "s5 < s6" << endl;
if (iComp3 == 0) cout << "s5 = s6" << endl;
if (iComp3 > 0) cout << "s5 > s6" << endl;

//和C字符串比较
char* p = "DEF";
string s7("ABC");
int iComp4 = s7.compare(p);
if (iComp4 < 0) cout << "s7 < p" << endl;
if (iComp4 == 0) cout << "s7 = p" << endl;
if (iComp4 > 0) cout << "s7 > p "<< endl;


12.copy

功能:从一个字符串复制到另一个字符串中

返回值:返回最终被拷贝的字符个数

//p一点要先分配好空间,否则复制会出错
string s1("hello");
char p[10] ="word";
s1.copy(p, 2,2);//从s1中的第三个字符开始复制两个字符到p的前面,会覆盖p中内容
cout << s1 << endl;
cout << p << endl;


13.empty

功能:判断字符串是否为空

string s;
if (s.empty())
{
cout<<"s字符串为空"<<endl;
}


14.erase

功能:删除一个或指定范围的元素

string s("hello word");
string::iterator iter = s.begin() + 2;
//删除某个元素
s.erase(iter);
cout << s << endl;

//第一个参数是从第几个位置开始删除,第二个是删除多少个
s.erase(1, 2);
cout << s << endl;

//删除指定范围元素
s.erase(s.begin() + 1, s.end() - 1);
cout << s << endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: