C++ 刷算法题笔记

string转int

string s;
int num;
//将s从第i个切m个字符串
//.c_str()变指针再转int
num = atoi(s.substr(i,m).c_str());

定义结构体,sorted比较

#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
struct node{
	string t;
	int value;
}; 
bool cmp(const node &a, const node &b){
	return a.value!=b.value ? a.value>b.value:a.t<b.t;
}
int main(){
    vector<node> ans(n);
    sort(ans.begin(), ans.end(), cmp);
    return 0;
}

读取输入文件

freopen("input.txt","r",stdin);

string t

截取

t.substr(5,10) //截取下标为5的后面10位字符串

printf("%s", t.c_str()); //打印

unordered_map

            unordered_map<string, int> m;
			for(int j=0;j<n;j++){
				if(v[j].t.substr(4,6) == s)
					m[v[j].t.substr(1,3)]++;
			}
			for(auto it : m)
				ans.push_back({it.first, it.second});
发布了67 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38603360/article/details/103698366