c++中string常用用法

在c语言中,也有string.h的库,不过相比于c++似乎没那么好用,c++所提供的功能非常的强大,下面就来看看

它的常见用法,记得包含string头文件

#include <string>
#include<iostream>
#include<algorithm>
//加上这个名字空间中的都可以使用
using namespace std;


int main(int argc, char const *argv[])
{
	//string对象,常用的几种方法
	string s1;
	s1="hello";
	/*string s2("hello");
	string s3(5,'a');
	// cout<<s1<<endl;
	// cout<<s2<<endl;
	// cout<<s3<<endl;

	//string 字符串字符操作,类似向量
	cout<<s1[0]<<endl;
	cout<<s1.at(0)<<endl;
	//将string转换为c字符串
	const char* cstr1=s1.c_str();
	cout<<cstr1<<endl;
	//拷贝从pos位置到n的字符
	char cstr2[255]={};
	s1.copy(cstr2,4,2);
	cout<<cstr2<<endl;
	
	//一些常用的特性
	cout<<s1.size()<<" "<<s1.length()<<endl;
	s1.resize(10,'a');
	cout<<s1<<endl;

	cout<<s1.empty()<<endl;
	

	//重载了<< >>以及 ,函数getline的用法
	//函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
	string ss;
	getline(cin,ss);
	cout<<ss<<endl;
	

	//string 的赋值操作assign函数,通过重载了很多种,我没列全

	s1="hello,world";
	cout<<s1<<endl;

	s1.assign("hello");
	cout<<s1<<endl;

	s1.assign("hello",2);
	cout<<s1<<endl;
	*/

	//string的append函数
	s1.append(2,'a');
	cout<<s1<<endl;

	s1.append("hello");
	cout<<s1<<endl;

	//剪切
	string s2=s1.substr(3,5);
	cout<<s2<<endl;

	//添加algorithm逆序
	reverse(s1.begin(),s1.end());
	cout<<s1<<endl;

	


	return 0;
}

这里我给出的只是一部分,它的功能远不止于此,还有很多功能,大家可以自己尝试,在使用时要勤于查找用法

string类的构造函数: string(const char *s);

//用c字符串s初始化 string(int n,char c);

//用n个字符c初始化 此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";

都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ; string类的字符操作:

const char &operator[](int n)const;

const char &at(int n)const; char &operator[](int n);

char &at(int n); operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

const char *data()const;//返回一个非null终止的c字符数组

const char *c_str()const;//返回一个以null终止的c字符串

int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目 string的特性描述:

int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)

int max_size()const; //返回string对象中可存放的最大字符串的长度

int size()const; //返回当前字符串的大小 int length()const; //返回当前字符串的长度

bool empty()const; //当前字符串是否为空

void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分 string类的输入输出操作:

string类重载运算符operator>>用于输入,同样重载运算符operator","=","时返回1,头文件中 例如: string input("hello,this is a test"); istringstream is(input); string s1,s2,s3,s4; is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" ostringstream os; os string;

string类的查找函数:

int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置

int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置

int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值

int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置

int rfind(const char *s, int pos = npos) const;

int rfind(const char *s, int pos, int n = npos) const;

int rfind(const string &s,int pos = npos) const; //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值

int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置

int find_first_of(const char *s, int pos = 0) const;

int find_first_of(const char *s, int pos, int n) const;

int find_first_of(const string &s,int pos = 0) const; //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回

string::npos int find_first_not_of(char c, int pos = 0) const;

int find_first_not_of(const char *s, int pos = 0) const;

int find_first_not_of(const char *s, int pos,int n) const;

int find_first_not_of(const string &s,int pos = 0) const; //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos int find_last_of(char c, int pos = npos) const;

int find_last_of(const char *s, int pos = npos) const;

int find_last_of(const char *s, int pos, int n = npos) const;

int find_last_of(const string &s,int pos = npos) const;

int find_last_not_of(char c, int pos = npos) const;

int find_last_not_of(const char *s, int pos = npos) const;

int find_last_not_of(const char *s, int pos, int n) const;

int find_last_not_of(const string &s,int pos = npos) const; //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值