C/C++语法、库的使用

文件操作

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	ifstream fin("in.txt");
	char c;
	fin>>c;
	cout<<c;
	fin.close(); 
	return 0;
}
 

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

int main()
{
	freopen("in.txt","r",stdin);
	char c;
	cin>>c;
	cout<<c; 
	fclose(stdin);
	return 0;
}
 
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	FILE * fin;
	fin=fopen("in.txt","rb");
	//fin=stdin;
	char c;
	fscanf(fin,"%c",&c);
	cout<<c; 
	fclose(stdin);
	return 0;
}
 

string.find()

https://www.cnblogs.com/wkfvawl/p/9429128.html

s.find(str);返回第一个起始下标
s.rfind(str);从右边找,返回右边第一个子串位置的起始下标
s.find_first_of(str);返回第一个起始下标 ==s.find()
s.find_last_of(str);返回最后一个子串的末尾字符下标
	string s="0123456789";
	cout<<s.find("789")//7
	cout<<s.rfind("789")//7
	cout<<s.find_first_of("789");//7
	cout<<s.find_last_of("789");//9

string.replace

s1.replace(s1.find(s2),s2.size(),"",0,0);
s.replace(0,1,"abc",0,2);
s.replace(0,1,"abc");
5个参数:s1开始下标 s1被替换的长度  s2  s2开始下标  s2的去替换的长度

string.substr

s1.substr(2, 4);//s1下标从2开始的4个字符长度的子串
s1.substr(2);//s1下标从2开始到末尾的子串

string.erase

s1.erase(1, 3);//删除s1下标1开始的3个字符
s1.erase(1);//删除s1下标1开始到末尾

string.insert

s1.insert(3, s2);  //在下标 2 处插入 s2 
s1.insert(3, 5, 'X');注意插入的是字符了

STL::find()

posted @ 2022-11-17 23:04  林动  阅读(15)  评论(0编辑  收藏  举报