c++小知识_string类

前言

   本文将总结c++种string类型的用法。
内容摘自:https://blog.csdn.net/weixin_38481963/article/details/79261952
https://blog.csdn.net/cny901111/article/details/7769314

一、构造字符串

构造函数描述
string(const char * s)将string对象初始化为s指向的NBTS(NBTS:以空字符结束的字符串------传统的C字符串)
string(size_type n,char c)创建一个包含n个元素的string对象,其中每个元素都被初始化为字符c
string(const string & str)将一个string对象初始化为string对象str(复制构造函数)
string()创建一个默认的string对象,长度为0(默认构造函数)
string(const char* s,size_type n)将string对象初始化为s指向的NBTS的前n个字符,即使n超过了NBTS的结尾
string(Iter begin,Iter end)将string对象初始化为区间[begin,end)内的字符,其中begin和end为迭代器,作用类似于指针
string(const string & str,string size_type pos=0,size_type n=npos)将一个string对象初始化为对象str中从位置pos开始到结尾的字符,或从位置pos开始的n个字符
string(string && str) noexceptc++11新增,将一个string对象初始化为string对象str,并可能修改str(移动构造函数)
string(initializer_list i1)将一个string对象初始化为列表i1中的字符

  

#include <iostream>
#include <string>
 
int main()
{
	using namespace std;
	cout<<"string类的六种构造方式:"<<endl;
 
	//0.创建一个长度为0的字符串 sting();
	string zero;
	cout<<zero<<endl;
 
	//1. string(const char *s)
	string one("Lottery Winner");
	cout<<one<<endl;
 
	//2.string(size_type,char c)
	string two(20,'s');
	cout<<two<<endl;
	
	//3.string(const string & str,string size_type n = npos)
	//复制全部
	string three(one); 
    //位置从n = 7开始复制字符
	cout<<three<<endl;
	string three1(one,7);
	cout<<three1<<endl;
 
	//重载操作符 +=
    one += " Oops!";
	cout<<one<<endl;
 
	//重载操作符 =
	two = "Sorry! That was";
	three[0] = 'P';
 
	string four;
	
	four = two + three;
	cout<<four<<endl;
 
	char alls[] = "All's well that ends well";
	//4.string(const char *s,size_type n);
	//n在范围内
	string five(alls,20);
	cout<<five<<endl;
	//n超出
	string five1(alls,40);
	cout<<five1<<endl;
 
	//5.template<class Iter> string(Iter begin,Iter end)
	//这里Iter为char *
	string six(alls+6,alls+10);
	cout<<six<<endl;
	string seven(&five[6],&five[10]);
	cout<<seven<<"...\n";
 
	return 0;
}

二、操作符

  ==    
  >
  <
  >=
  <=
  !=
  +
  +=
  []

示例:

string str1="abcd";
string str2="defg";
string str3=str1;
string str4=str1+str2;

if(str3==str1)  cout<<"true";  //true
if(str2>str1)   cout<<"true";  //true

//输出str4
//法一
cout<<str4;
//法二
for(int i=0;i<str4.length();i++)
{
    cout<<str4[i];
}

  可以用==,>,<,>=,<=,!=比较两个字符串。可以用+,+=连接两个字符串。可以用[]来取特定的字符,就像数组一样。

三、重要函数

length();  //ength()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.
size();    //与length()作用相同
begin();   //begin()函数返回一个迭代器,指向字符串的第一个元素.
end();     //end()函数返回一个迭代器,指向字符串的最后一个元素的下一个位置
c_str();  //c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.
          //注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
 /*======用法示例===========*/
//将string字符串转换为字符串数组
char c[20];
string str="abcd";
strcpy(c,str.c_str());
/*=========================*/

data();    //data()函数返回指向自己的第一个字符的指针
		   //用法:const char *data();
copy()//复制函数
/*===用法示例========*/
string str1="this is a nice day!";
char str2[20];
str1.copy(str2,4,10);
//输出
for(int i=0;i<4;i++)
{
    cout<<str2[i];
}
/*=====================*/

empty();   //如果字符串为空则empty()返回真(true),否则返回假(false).
max_size();  //max_size()函数返回字符串能保存的最大字符数
resize();    //resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充
substr();   //substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
basic_string substr( size_type index, size_type num = npos );
/*======用法实例=======*/
string str1="it is a nice day";
string str2=str1.substr(8,4); //str2="nice";
/*===================*/ 

swap()//swap()函数把str和本字符串交换
void swap( basic_string &str );

compare()函数:

int compare( const basic_string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );

例子:

string str1="this is a nice day!";
string str2="nice";
string str3="it is nice!";
char s[]="nice";
cout<<str1.compare(str2)<<endl;   //1
cout<<str2.compare(s)<<endl;       //0
cout<<str1.compare(10,4,str2)<<endl;  //0
cout<<str1.compare(10,4,str3,6,4);    //0

compare()函数以多种方式比较本字符串和str:

返回值情况
小于零this< str
this=str
大于零this>str

不同的函数:

  • 比较自己和string类的str 比较自己和指针类型的str,例如:字符串数组

  • 比较自己的子串和str,子串以index索引开始,长度为length

  • 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己

  • 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值