string容器
1.C++标准库定义了一种string类,定义在头文件"string"
char是一个指针 String是一个类。string封装了char,管理这个字符串,是一个char型的容器。
2.string封装了很多实用的方法:
查找、删除、插入、替换、拷贝
3.string管理char所分配的内存不用考虑内存释放和越界
二.String常用的操作
#define _CRT_SECURE_N0_WARNINGS
#include<iostream>
#include<string>
using namespace std;
/*
string 构造函数
string() 空字符串
string(const string& str) 使用一个string对象初始化另一个对象
string(const char*s) 使用字符串s初始化
string(int n,char c) 使用n个字符串c初始化
*/
void text01()
{
string str; //默认构造
string str2(str); //拷贝构造
string str3=str;
string str4="abcd";
string str5(10,'a');
cout<<str4<<endl;
cout<<str5<<endl;
//基本赋值
str="hello";
str2=str4;
//string& assign(const char *s,itn n)把字符串的前N个字符给当前的字符串
str3.assign("abcdef",4);
cout<<str3<<endl;
//将S从start开始n个字符赋值给字符串 string&assign(const string &s,int start,int n);
string str6;
str6.assign(str,1,3);
cout<<str6<<endl;
}
/*
string存取字符操作
char&operator[](int n); //通过[]方式取字符
char & at(int n) //通过at方法获取字符
*/
void text02()
{
string s="hello world";
for(int i=0;i<s.size();i++)
{
//cout<<s[i]<<endl;
cout<<s.at(i)<<endl;
**//[]和at区别【】访问越界 直接挂掉 at会抛出异常**
}
try
{
//cout<<s[100]<<endl;
cout<<s.at(100)<<endl;
}
catch (...)
{
cout<<"error越界异常"<<endl;
}
}
/*
string插入和删除操作
string& insert(int pos,const char *s);插入字符串
string& insert ( int pos,string & str);插入字符串
string& insert(int pos ,int n,char c);在指定位置插入n个字符
string& erase(int pos ,int n=npos);删除从pos开始的n个字符
*/
void text03()
{
string s1="hello";
s1.insert(1,"111");
cout<<s1<<endl;
//删除111
s1.erase(1,3);
cout<<s1<<endl;
}
/*
string拼接操作
string &operator+=(const string&str);//重载+=运算符
string&operator+=(const char*str) //重载+=运算符
string& append(const char *s)//把字符串s连接到当前字符串结尾
*/
void text04()
{
string s1="wo";
string s2="love beijing";
(s2+s1).append("天安门");
cout<<"s1+s2="<<s1+s2<<endl;
/*
//string查找和替换
int find(const string&str,int pos=0) const //查找str第一次出现的位置从pos开始查找
int rfind(const string&s,int pos=npos)const //查找str最后一次的位置从pos开始查找
int rfind(const char* s,int pos,int n)const //从pos查找s的前n个字符最后一次位置
string &replace(int pos.int n,const string&str)//替换从pos开始n个字符为字符串str
*/
string t="abcdefg";
int pos=t.find("bcf"); // 找不到返回-1
cout<<"pos="<<pos<<endl;
int pos2=t.rfind("bc");
cout<<"pos2="<<pos2<<endl;
//替换
string t2="hello";
t2.replace(1,3,"111");
cout<<t2<<endl;
}
/*
string比较操作
compare函数在>时返回1,<时返回-1,==时返回0;
比较区分大小写,比较时参考字典顺序,排越前面越小,大写的A比小写的a小
int compare(const string &s) const //与字符串s比较
int compare(const char *s) const //与字符串s比较
*/
void text05()
{
string s1="abc";
string s2="abc";
if(s1.compare(s2)==0)
{
cout<<"s1等于s2"<<endl;
}
else if(s1.compare(s2)==1)
{
cout<<"s1大于s2"<<endl;
}
else
{
cout<<"s1小于s2"<<endl;
}
}
int main()
{
//text01();
//text02();
//text03();
//text04();
text05();
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_N0_WARNINGS
#include
#include
using namespace std;
void fc(string s)
{
cout<<s<<endl;
}
/*
string和c-style字符串转换
1. string 转 char*
string str = “itcast”;
const char* cstr = str.c_str();
2.char* 转 string
char* s = “itcast”;
string str(s);
*/
void text01()
{
//string->const char *
string s=“abcd”;
const char *p=s.c_str();
fc(p);//隐形转换 const char*->string,string不能隐形转换为 const char*
//const char*->string
string s2(p);
}
int main()
{
text01();
system(“pause”);
return EXIT_SUCCESS;
}