介绍
string是专门的字符串操作的一个类,非常强大。字符串CString,QString。string 是一个类, 这个类将以上的内容封装到一起,使得字符串的操作更灵活,方式更多,管理更合理。string这个类使用的时候不用考虑内存的分配与释放,也不用担心越界崩溃,因为前辈在封装string的时候,已经把几乎所有情况都考虑到并处理了。
头文件< string >,注意要using namespace std;
构造函数
- string str;
string str;
str = "abcd";
cout << str << endl;
- string( size_type length, char ch );
string str(4,'a');
cout << str << endl;
- string( const char *str );
string str("abcd");
cout << str << endl;
- string( const char *str, size_type length );
string str("abcde",2); //前两个即ab
cout << str << endl;
- string( string &str, size_type index, size_type length );
string str1 = "abcdef";
string str2(str1, 1, 2); //索引为1开始长度为2的,即bc
cout << str2 << endl;
- string(const string& str);
string str1 = "abcdef";
string str2(str1);
cout << str2 << endl;
属性
容量
- 容量capacity()
VS下默认容量15个,少于15个就申请15个,多余15个以后多于增加16个
VC++6.0默认申请31个,以后每次增加32个
string str1 = "abcdef";
cout << str1.capacity() << endl; //15
string str2(16, 'a');
cout << str2.capacity() << endl; //31
string str3(32, 'a');
cout << str3.capacity() << endl; //47
- 修改容量
reserve()函数,只能变大,不能变小,区间变化
string str1 = "abcdef";
cout << str1.capacity() << endl; //15
str1.reserve(16);
cout << str1.capacity() << endl; //31
长度
length()
string str1 = "abcdef";
cout << str1.length() << endl;
字符个数
- 字符个数size()
和length()效果一样
string str1 = "abcdef";
cout << str1.length() << endl;
- 修改字符个数resize()
重新设置字符个数,容量不变
string str(16, 'a');
cout << str << endl; //16个a
cout << str.capacity() << endl; //32
str.resize(4);
cout << str << endl; //4个a(截断)
cout << str.capacity() << endl; //32
输出
输出全部
- <<对象
string str = "abcdef";
cout << str << endl;
- c_str()函数
深拷贝,str.c_str()获得字符串首地址,然后深拷贝
string str = "abcdef";
cout << str.c_str() << endl; //输出abcdes
输出单个字符
- []下标
越界时崩溃
string str = "abcde";
cout << str[1] << endl;
- at()
越界时抛出异常
string str = "abcde";
cout << str.at(1) << endl;
修改
修改指定元素
- []
- at()
中间插入
- basic_string &insert( size_type index, const basic_string &str );
index位置插入str
string str1 = "abcd";
string str2 = "123";
str1.insert(2, str2);
cout << str1 << endl; //ab123cd
-
basic_string &insert( size_type index, const char *str );
index位置插入一个字符串
-
basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
在index1的位置插入另一个对象的某一段
string str1 = "abcd";
string str2 = "123";
str1.insert(1,str2,1,2);
cout << str1 << endl; //a23bcd
- basic_string &insert( size_type index, const char *str, size_type index1 );
插入第二个的索引号为index2后面的所有的
string str1 = "abcd";
string str2 = "123";
str1.insert(1,str2,1);
cout << str1 << endl; //a23bcd
- basic_string &insert( size_type index, size_type num, char ch );
插入num个ch
尾部插入
- +或者=拼接
- append()
重新赋值
- =赋值
- cin>> 输入
- assign()
删除earse()
string str1 = "abcde";
str1.erase(1, 2); //删除指定索引出指定长度
cout << str1 << endl; //ade
操作函数
比较
- 直接用重载的><=
- 用compare函数
大就返回1,等于返回0,小于返回-1
int compare( const basic_string &str );
比较两个对象
*int compare( const char str );
比较一个对象和字符串
int compare( size_type index, size_type length, const basic_string &str );
本对象的一段,跟参数3的对象比较
int compare( size_type index, size_type length, const basic_string &str, size_type index2,size_type length2 );
两个字符串比较中间的某一段
复制
size_type copy( char *str, size_type num, size_type index );
将对象中的某一段复制进一个字符数组中
(注意区分其函数整数第一个参数是长度不是开始索引)
string str1 = "abcde";
str1.copy(str, 4, 0); //4个从0开始
cout << str << endl; //结果为abcd
查找字串(重要)
- size_type find( const basic_string &str, size_type index );
从指定位置开始查找str,找到就返回起始位置,找不到就返回4294967295,强转为int为-1 - size_type find( char ch, size_type index );
从指定位置开始查找字符
string str1 = "abcde";
string str2 = "de";
string str3 = "ef";
cout << (int)str1.find(str2) << endl; //3
cout << (int)str1.find(str2,1) << endl; //3
cout << (int)str1.find(str3, 1) << endl; //-1
cout << (int)str1.find('a', 0) << endl; //0
返回子串
substr( size_type index, size_type num = npos );
返回指定位置的子串
string str1 = "abcde";
cout << str1.substr(0, 2) << endl; //ab
交换
swap( basic_string &str );
交换两个对象的内容
迭代器(iterator)
定义string迭代器
string::iterator ite
就相当于一个指向string对象元素的指针
本质上相当于 一个char* 的指针;
使用string迭代器
- 通过迭代器遍历string类的元素
string str1 = "abcde";
string::iterator ite;
for (ite = str1.begin(); ite != str1.end(); ite++)
cout << *ite ;
cout<<endl;
为什么不定义成char*呢, 因为迭代器是要跟算法连接的,它适用于所有的容器,即一个通用类型的指针,或者类似叫智能指针
迭代器失效
string重新申请空间的时候,迭代器会失效,因为重新申请空间时候要重新分配内存,迭代器不再指向
string str1 = "abcde";
string::iterator ite;
ite = str1.begin();
str1.append(15, 'k');
ite = str1.begin();
常用的算法
遍历
for_each();
#include < algorthm >
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
void fun(char c);
int main()
{
string str = "abcde";
for_each(str.begin(), str.end(), fun);
cout << endl;
system("pause");
return 0;
}
void fun(char c)
{
cout << c;
}
排序
- sort(str.begin(),str.end());
从小到大 - sort(str.begin(),str.end(),greater< char >() );
从大到小,其中greater()函数头文件为< funcional >
#include<iostream>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
int main()
{
string str = "abcde";
sort(str.begin(), str.end());
cout << str << endl;
sort(str.begin(), str.end(), greater<char>());
cout << str << endl;
system("pause");
return 0;
}
总结(该记的函数)
- c_str()
- capacity()
- reserve() //改变容量
- append()
- size()
- length()
- resize()
- at()
- insert()
- assign() //重新赋值
- erase() //删除
- compare //比较
- copy()
- find()
- substr() //返回子串
- swap()
- greater() //要加头文件