C++ string使用详解

#include<iostream>
#include<string.h>
using namespace std;

void Test()
{//1.初始化  & size 
	string str="987654321";//1初始化  大小为0  size() 为9  
	string str2(8,'1');//2.初始化
	string str3;//3初始化  
	cout<<"str3 "<<str3<<endl;	//空
	cout<<str3.size()<<endl;//大小为0 
	
	cout<<"before given,the compare result:"<<str2.compare(str)<<endl;
	str=str2;//4初始化  
	
	cout<<"after given,the size of str:"<<str.size()<<endl;	//8
	cout<<"after given,the  str:"<<str<<endl;	//8  完全是str2
	cout<<str2.compare(str)<<endl;
	return ;
}

void Test2()
{//compare  ?  怎么计算的?
	string str="12345678";//1初始化  大小为0  size() 为9  
	 string str2="12345678";
	cout<<str2.compare(str)<<endl;//完全相同 返回0
	
	 str2="012345678";
	cout<<str2.compare(str)<<endl;//-1
	
	 str2="12345679";
	cout<<str2.compare(str)<<endl;//256   1*2^8
	
	 str="abcdef";
	 str2="abcdefg";
	cout<<str2.compare(str)<<endl;//1
//string的比较:
/*
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;
//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0  
	*/
	
	if(str2==str) cout<<"equal"<<endl;
	else cout<<"different"<<endl;
	cout<<str.compare(0, str.size(),str2,0,str.size())<<endl;//0 equal
	return ;

}
void Test3()
{//c_str()    data()   copy()
	string str="1234567890";
	char *pStr=new char[str.size()+1];
	strcpy(pStr,str.c_str());
	cout<<pStr<<endl;
	cout<<"pStr strlen :"<<strlen(pStr)<<endl;
	//cout<<"pStr size :"<<sizeof(pStr)<<endl;  //指针大小  根据系统决定  64:8* 8  \								 
	
	strcpy(pStr,str.data());
	cout<<"str.data()"<<endl;
	cout<<pStr<<endl;//1234567890
	cout<<"pStr strlen :"<<strlen(pStr)<<endl;
	//当pStr[10]按理说应当访问越界,但结果返回值为0 ?
	//cout<<"pStr[10]="<<pStr[10]<<endl;
	
	//int copy(char *s, int n, int pos = 0) const;
	//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
	char chArr[100]={0};
	int ret=str.copy(chArr,10,0);
	if(ret!=10){
		cout<<"copy  Error!"<<endl;
		return ;
	}
	cout<<"after copy,the chArr:"<<chArr<<endl;
	return ;

}

void Test4()
{//清空 clear

	string str="1234567890";
	str.clear();
	cout<<"after clear,the str is"  << str<<endl;
	cout<<str.size()<<endl;//大小为0 
	return ;

}
void Test5()
{//at (pos)   or  []   字符操作
	string str="123456789";
	cout<<str.at(1)<<endl;//2
	cout<<str.at(0)<<endl;//2
	cout<<str[1]<<endl;
	cout<<str[10]<<endl;
	//cout<<str.at(10)<<endl;   //会出现访问越界错误
	//operator[]和at()均返回当前字符串中第n个字符的位置,\
	但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
	return ;
}

void Test6()
{//features
		string str="123456789";
		cout<<"capacity:"<<str.capacity()<<endl;//9  //返回当前容量(即string中不必增加内存即可存放的元素个数)
		cout<<"maxsize:"<<str.max_size()<<endl;    //返回string对象中可存放的最大字符串的长度:4611686018427387897
		cout<<"size:"<<str.size()<<endl;//9 //返回当前字符串的大小
		cout<<"len:"<<str.length()<<endl;      //返回当前字符串的长度  9
		
		if(str.empty()) cout<<"str is empty!"<<endl;        //当前字符串是否为空
		else cout<<"str is not empty!"<<endl;
		
		str.resize(13,'a');//把字符串当前大小置为13,并用字符'a'填充不足的部分
		cout<<"after resize(20,'a'),the str is:"<<str<<endl;//123456789aaaa
		return ;
}

void Test7()
{
	//string类的输入输出操作  
	string str;
	cin>>str;
	cout<<str<<endl;   //1  >> <<
	string str2;
	cin>>str2;
	//getline(cin,str2);//用于从输入流in中读取字符串到s中,以换行符'\n'分开。  ?
	cout<<str2<<endl;   //
	return ;
}

void Test8()
{//string的赋值:
	string str;
	char *pStr=new char[10];
	strcpy(pStr,"12345678");
	char szBuf[]="87654321";
	str.assign(pStr);//用c类型字符串s赋值   
	cout<<str<<endl;  
	str.assign(szBuf);//用c类型字符串s赋值
	cout<<str<<endl;  
/*similar
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
*/
	delete pStr;
	return ;	
}


void Test9()
{
	//string的连接:
	/*
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾 
 */
 	string str,str2;
 	str="123";
 	str2="789";
 	str+=str2;
 	str+='a';
 	cout<<str<<endl; //123789a
 	char szBuf[]="456";
 	str.append(szBuf);//123789a456
 	cout<<str<<endl;  
	return ;
}

void Test10()
{	
//string的子串:
//string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
	string str="1234567890";
	string str2=str.substr(0,5);
	cout<<"after substr,the str and the str2 are:"<<str<<endl<<str2<<endl;
	
//string的交换:
//void swap(string &s2);    //交换当前字符串与s2的值
	str.swap(str2);
	cout<<"after swap,the str and the str2 are:"<<str<<endl<<str2<<endl;
	return ;
}

void Test11()
{

 string str="1234123456";
 //1.查询函数使用
 //pos  value
 //0      1
 //1      2
 //2	  3
 //3	  4
 
 //4	  1
 //5	  2
 //6	  3
 //7	  4
 //8	  5
 //9 	  6
 cout<<str.find('2',2)<<endl; //如果找到返回位置0-size()-1,否则返回值挺大!
 
 cout<<str.rfind('2',2)<<endl;//1 从后开始搜寻,但返回值还是它的位置
 cout<<str.rfind('3',4)<<endl;//test :2  /
 
 cout<<str.find_first_of('1',0)<<endl;//如果找到返回位置0-size()-1,否则返回值挺大!
 cout<<str.find_first_of('1',1)<<endl;//test :4 /
 
 cout<<str.find_last_of('5',3)<<endl;//如果找到返回位置0-size()-1,否则返回值挺大!  找不到!值大
 cout<<str.find_last_of('1',3)<<endl;//test: 0
 
 cout<<str.find_last_not_of('2',3)<<endl;//3 从pos(3)开始“1234”【从后向前】查找{最后一个}不是字符'2'的字符在原来串中的位置
 //'4':在原字符串中位置pos(4)
 cout<<str.find_last_not_of('2',2)<<endl;//2  从pos(2)开始“123”   【从后向前】查找{最后一个}不是字符'2'的字符在原来串中的位置
 //'3':在原字符串中位置pos(2)
 cout<<str.find_last_not_of('2',1)<<endl;//0  从pos(1)开始 "12"  【从后向前】查找{最后一个}不是字符'2'的字符在原来串中的位置
 //'1':在原字符串中位置pos(0)
 
 cout<<str.find_last_not_of('2',0)<<endl;//0  从pos(0)开始 "1"【从后向前】查找{最后一个}不是字符'2'的字符在原来串中的位置
 //'1':在原字符串中位置pos(0)
 
 cout<<str.find_first_not_of('2',5)<<endl;//6    从pos(5)开始“23456”【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置
 //'3':在原字符串中位置pos(6)
 cout<<str.find_first_not_of('2',4)<<endl;//4    从pos(4)开始“123456”【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置 
 //'1':在原字符串中位置pos(4)
 cout<<str.find_first_not_of('2',3)<<endl;//3    从pos(3)开始"4123456"【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置	
 //'4':在原字符串中位置pos(3)
 cout<<str.find_first_not_of('2',2)<<endl;//2    从pos(2)开始"34123456"【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置	
 //'3':在原字符串中位置pos(2)
 cout<<str.find_first_not_of('2',1)<<endl;//2   从pos(2)开始"234123456"【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置	
 //'3':在原字符串中位置pos(2)
 cout<<str.find_first_not_of('2',0)<<endl;//0   从pos(0)开始"1234123456"【从前往后】查找{第一个}不是字符'2'的字符在当前串中的位置	
  //'1':在原字符串中位置pos(0)
 
 
 return;

}
 
 

void Test12()
{//2.替换函数
//string类的替换函数:
/*
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);
*/
//把[first0,last0)之间的部分替换成[first,last)之间的字符串

	string str="123789";
	string rstr="456";
	str.replace(3,3,rstr);
	cout<<"after replace ,the str is:"<<str<<endl;//123456
	return ;
}



void Test13()
{
/*
//string类的插入函数:
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
*/
	string str="123789";
	string istr="456";	
	str.insert(3,istr);
    cout<<"after insert ,the str is:"<<str<<endl;//123456789
	return;
}


void Test14()
{
/*

string类的删除函数
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置!
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置!
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串
*/
      string str="123789";
      str.erase(0,3);
      cout<<"after erase 2,the str is:"<<str<<endl;//123456789

	  return ;
}

void Test15()
{
/*
string类的迭代器处理:
string类提供了【向前】和【向后】遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
===========
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin();                //返回string的起始位置
const_iterator end()const;
iterator end();                    //返回string的最后一个字符后面的位置
===========
const_iterator rbegin()const;
iterator rbegin();                //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend();                    //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现
*/
	string str="0123456";
	string::iterator  it1=str.begin();//指向开始字符'0'
	it1++;
	str.erase(it1);//删除了字符'1'
	it1=str.end();//end()返回string的最后一个字符后面的位置!直接删除报错!
	it1--;
	str.erase(it1);//删除了最后一个字符'6'
	//cout<<it1<<endl;//error!
	cout<<"after erase() ,the str is:"<<str<<endl;//123456789
	return ;
}




int  main(int argc,char**argv)
{
	Test15();
	return 0;
}

posted @ 2015-09-10 20:41  cloudren2020  阅读(146)  评论(0编辑  收藏  举报