首页 > 其他分享 >#include<algorithm>函数

#include<algorithm>函数

时间:2022-09-04 20:26:47浏览次数:47  
标签:include 函数 int namespace main string cout

1、查找函数(n.find("="))

从前往后找:n.find();

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
string n;
int main(){
	cin>>n;
	int len=n.find('=');
	cout<<len;//len为"="的位置 
	return 0;
}

从后往前找:n.rfind();

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
string n;
int main(){
	cin>>n;
	int len=n.rfind('=');
	cout<<len;//len为最后一个"="的位置 
	return 0;
}

2、截取函数(n.substr(n,m))

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
string n="LiXuesen";
int main(){
	cout<<n.substr(2,3);//从2开始,向后截三个字符 
	return 0;
}

3、替换函数(n.replace(n,m,"lin"))

#include<bits/stdc++.h>
using namespace std;
string n="LiXuesen";
int main(){
  n.replace(5,3,"lin");//将sen替换为lin 
  cout<<n;
  return 0;
}

4、插入函数

#include<bits/stdc++.h>
using namespace std;
string n,str;
int a;
int main(){
  cin>>n>>a>>str;
  cout<<n.insert(a,str);
  return 0;
}//在文档中第a个字符前面插入字符串str

举例:

#include<bits/stdc++.h>
using namespace std;
string n,n1,n2;
int main(){
  cin>>n>>n1>>n2;
  if(n.find(n1)!=-1){
  	n.replace(n.find(n1),n1.size(),n2);
  }
  cout<<n;
  return 0;
}//如果n中有n1,将n1替换为n2 

标签:include,函数,int,namespace,main,string,cout
From: https://www.cnblogs.com/hnzzlxs01/p/16655910.html

相关文章

  • python中的内置函数
    内置函数#1.abs函数print(abs(-1))#绝对值方法#2.all函数print(all([1,'aaa','']))#falseprint(all([]))#true#all方法里面是一个可迭代对象,all会自动将这......
  • Oracle中行转列(pivot)函数解析(二)
    Oracle行转列就是把某一个字段的值作为唯一值,然后另外一个字段的行值转换成它的列值。案例原始数据如下:  方法一:利用groupby实现selectt.mr_sl_id,sum(......
  • Oracle中行转列函数(一)
    1、wm_concat(列名)解析:该函数可以把列值以“,”号分割起来,并显示成一行。例:selectwm_concat(item_code)fromhdrg.qcs_dict_item_detailwheretable_name='d......
  • 函数当作参数
    1<!DOCTYPEhtml>2<html>3<head>4<metacharset="utf-8">5<title></title>6</head>7<body>8<script>9......
  • python-常用内置函数
    数学相关的内置函数abs:取绝对值divmod:求两个数相除的商和余数max:求最大数min:求最小数pow:幂运算round:四舍五入保留到指定小数位sum:用来求和可迭代对象相关......
  • 函数f(m,n)算法设计
    题目:设m,n均为自然数,m可表示为一些不超过n的自然数之和,f(m,n)为这种表示方式的数目。例f(5,3)=5,有5种表示方式:3+2,3+1+1,2+2+1,2+1+1+1,1+1+1+1+1。以下是该函数的程序段,请将......
  • 函数的声明和调用
    1<script>2/*3把一段相对独立的具有特定功能的代码封装起来(写到一个地方),形成一个独立实体,就是函数,起个名字(函数名);4......
  • 【Python基础】内置函数filter详解
    filter,顾名思义,就是一个过滤器。其作用是从列表(或其他序列类型)中筛选出满足条件的子列表,filter是python的内置函数,无须import即可直接使用。1filter的基础用法对于列表(或......
  • Vue的钩子函数(路由守卫,keep-alive,生命周期)
    说到Vue的钩子函数,可能很多人只停留在一些很简单常用的钩子(created,mounted),而且对于里面的区别,什么时候该用什么钩子,并没有仔细的去研究过生命周期钩子:这一比较简单但......
  • 普通函数、参数、匿名函数、高阶函数、递归函数、闭包、装饰器
    函数定义#定义函数deffn():print("这是函数内部")#调用fn()fn()#区分fn:这是真正意义上的函数本身fn():这是调用函数参数形参实参函数参数可有......