【算法笔记自学】第 6 章 C++标准模板库(STL)介绍

news/2024/10/5 22:51:19/

6.1vector常见用法详解

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, x;scanf("%d", &n);vector<int> v;for (int i = 0; i < n; i++) {scanf("%d", &x);v.push_back(x);}for (int i = 0; i < (int)v.size(); i++) {printf("%d", v[i]);if (i < (int)v.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, k;scanf("%d%d", &n, &k);vector<int> v(n, k);for (int i = 0; i < (int)v.size(); i++) {printf("%d", v[i]);if (i < (int)v.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, k;scanf("%d%d", &n, &k);vector<int> v(n, 0);for (int i = 0; i < k; i++) {v.pop_back();}printf("%d", (int)v.size());return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, k;scanf("%d%d", &n, &k);vector<int> v(n, 0);v.clear();printf("%d", (int)v.size());return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, x, k1, k2;scanf("%d", &n);vector<int> v;for (int i = 0; i < n; i++) {scanf("%d", &x);v.push_back(x);}scanf("%d%d%d", &x, &k1, &k2);v.insert(v.begin() + k1, x);v.erase(v.begin() + k2);for (int i = 0; i < (int)v.size(); i++) {printf("%d", v[i]);if (i < (int)v.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, m, x;scanf("%d%d", &n, &m);vector<int> v1, v2;for (int i = 0; i < n; i++) {scanf("%d", &x);v1.push_back(x);}for (int i = 0; i < m; i++) {scanf("%d", &x);v2.push_back(x);}printf(v1 < v2 ? "Yes" : "No");return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, k, x;scanf("%d", &n);vector<int> vs[10];for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &x);vs[i].push_back(x);}}for (int i = 0; i < n; i++) {for (int j = 0; j < (int)vs[i].size(); j++) {printf("%d", vs[i][j]);if (j < (int)vs[i].size() - 1) {printf(" ");} else {printf("\n");}}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;int main() {int n, k, x;scanf("%d", &n);vector<vector<int> > vs(n, vector<int>());for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &x);vs[i].push_back(x);}}for (int i = 0; i < n; i++) {for (int j = 0; j < (int)vs[i].size(); j++) {printf("%d", vs[i][j]);if (j < (int)vs[i].size() - 1) {printf(" ");} else {printf("\n");}}}return 0;
}

6.2set的常见用法详解

#include <cstdio>
#include <set>
using namespace std;int main() {int n, x;scanf("%d", &n);set<int> s;for (int i = 0; i < n; i++) {scanf("%d", &x);s.insert(x);}for (set<int>::iterator it = s.begin(); it != s.end(); it++) {if (it != s.begin()) {printf(" ");}printf("%d", *it);}return 0;
}

#include <cstdio>
#include <set>
using namespace std;int main() {int n, x;scanf("%d%d", &n, &x);set<int> s;for (int i = 0; i < n; i++) {int a;scanf("%d", &a);s.insert(a);};set<int>::iterator it = s.find(x);if (it != s.end()) {s.erase(it);}for (set<int>::iterator it = s.begin(); it != s.end(); it++) {if (it != s.begin()) {printf(" ");}printf("%d", *it);}return 0;
}

#include <cstdio>
#include <set>
using namespace std;int main() {int n, x;scanf("%d%d", &n, &x);set<int> s;for (int i = 0; i < n; i++) {int a;scanf("%d", &a);s.insert(a);};s.erase(x);for (set<int>::iterator it = s.begin(); it != s.end(); it++) {if (it != s.begin()) {printf(" ");}printf("%d", *it);}return 0;
}

#include <cstdio>
#include <set>
using namespace std;int main() {int n, x;scanf("%d%d", &n, &x);set<int> s;for (int i = 0; i < n; i++) {int a;scanf("%d", &a);s.insert(a);};s.clear();printf("%d", (int)s.size());return 0;
}

6.3string的常见用法详解

#include <iostream>
#include <string>
using namespace std;int main () {string s;cin >> s;for (int i = 0; i < s.length(); i++) {cout << s[i];}return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s;getline(cin, s);for (int i = 0; i < s.length(); i++) {cout << s[i];}return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s1,s2;cin>>s1>>s2;string s3=s1+s2;for (int i = 0; i < s3.length(); i++) {cout << s3[i];}return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s1,s2;cin>>s1>>s2;if(s1>s2)printf("1");else if(s1==s2)printf("0");else printf("-1");return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s;cin>>s;printf("%d ",s.length());s.clear();printf("%d",s.length());return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s;cin >> s;int k1, k2;char c;cin >> k1 >> c >> k2;s.insert(s.begin() + k1, c);s.erase(s.begin() + k2);cout << s;return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s1,s2;cin >> s1>>s2;printf("%d",s1.find(s2));return 0;
}

#include <iostream>
#include <string>
using namespace std;int main () {string s1,s2;cin >> s1;int k,len;cin>>k>>len;cin>>s2;string s3=s1.replace(k,len,s2);cout<<s3;return 0;
}

6.4map的常见用法详解

#include <iostream>
#include <map>
using namespace std;int main() {int n, x;char c;cin >> n;map<char, int> mp;for (int i = 0; i < n; i++) {cin >> c >> x;mp[c] = x;}for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << it -> first << " " << it -> second << endl;}return 0;
}

#include <iostream>
#include <map>
using namespace std;int main() {int n, x;char c;cin >> n;map<char, int> mp;for (int i = 0; i < n; i++) {cin >> c >> x;mp[c] = x;}for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << it -> first << " " << it -> second << endl;}return 0;
}

#include <iostream>
#include <map>
using namespace std;int main() {int n, x;char c;cin >> n;map<char, int> mp;for (int i = 0; i < n; i++) {cin >> c >> x;mp[c] = x;}char k;cin>>k;if (mp.find(k) != mp.end()) {cout << mp[k];} else {cout << -1;}return 0;
}

#include <iostream>
#include <map>
using namespace std;int main() {int n, x;char c;cin >> n;map<char, int> mp;for (int i = 0; i < n; i++) {cin >> c >> x;mp[c] = x;}char k;cin>>k;mp.erase(k); for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << it -> first << " " << it -> second << endl;}return 0;
}

#include <iostream>
#include <map>
using namespace std;int main() {int n, x;char c;cin >> n;map<char, int> mp;for (int i = 0; i < n; i++) {cin >> c >> x;mp[c] = x;}cout<<mp.size()<<" ";mp.clear();cout<<mp.size();return 0;
}

#include <iostream>
#include <map>
using namespace std;int main() {int n;string str;cin >> n;map<string, int> mp;for (int i = 0; i < n; i++) {cin >> str;if (mp.find(str) == mp.end()) {mp[str] = 1;} else {mp[str]++;}}for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << it -> first << " " << it -> second << endl;}return 0;
}

6.5queue的常见用法详解

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x;cin >> n;queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}cout << q.front() << " " << q.back();return 0;
}

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x,k;cin >> n;cin>>k;queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}for (int i = 0; i < k; i++) {q.pop();}if (q.empty()) {cout << "empty queue";} else {cout << q.front() << " " << q.back();}return 0;
}

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x,k;cin >> n;cin>>k;queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}for (int i = 0; i < k; i++) {q.pop();}cout<<q.size();return 0;
}

6.6priority_queue的常见用法详解

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x;cin >> n;priority_queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}cout << q.top();return 0;
}

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x,k;cin >> n>>k;priority_queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}for (int i = 0; i < k; i++) {q.pop();}if(q.empty()){cout<<"empty priority queue";}else{cout<<q.top();}return 0;
}

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x,k;cin >> n>>k;priority_queue<int> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}for (int i = 0; i < k; i++) {q.pop();}cout<<q.size();return 0;
}

#include <iostream>
#include <queue>
using namespace std;int main() {int n, x;cin >> n;priority_queue<int, vector<int>, greater<int>> q;for (int i = 0; i < n; i++) {cin >> x;q.push(x);}cout<<q.top();return 0;
}

#include <iostream>
#include <queue>
using namespace std;
struct Fruit {string name;int price;Fruit(string _name, int _price) {name = _name;price = _price;}bool operator<(const Fruit& other) const {return price > other.price;}
};int main() {int n, price;string name;cin >> n;priority_queue<Fruit> q;for (int i = 0; i < n; i++) {cin >> name >> price;q.push(Fruit(name, price));}Fruit topFruit = q.top();cout << topFruit.name << " " << topFruit.price;return 0;
}

 6.7stack的常见用法详解

#include <iostream>
#include <stack>
using namespace std;int main() {int n, x;cin >> n;stack<int> s;for (int i = 0; i < n; i++) {cin >> x;s.push(x);}cout << s.top();return 0;
}

#include <iostream>
#include <stack>
using namespace std;int main() {int n, x,k;cin >> n>>k;stack<int> s;for (int i = 0; i < n; i++) {cin >> x;s.push(x);}for (int i = 0; i < k; i++) {s.pop();}if(s.empty()){cout<<"empty stack";}else{cout << s.top();}return 0;
}

#include <iostream>
#include <stack>
using namespace std;int main() {int n, x,k;cin >> n>>k;stack<int> s;for (int i = 0; i < n; i++) {cin >> x;s.push(x);}for (int i = 0; i < k; i++) {s.pop();}cout<<s.size();return 0;
}

6.8pair的常见用法详解

#include <iostream>
#include <utility>
using namespace std;int main() {string str;int k;cin >> str >> k;pair<string, int> p = make_pair(str, k);cout << p.first << " " << p.second;return 0;
}

#include <iostream>
#include <utility>
using namespace std;int main() {string str;int k1,k2,k3,k4;cin>>k1>>k2>>k3>>k4;pair<int, int> p1 = make_pair(k1, k2);pair<int, int> p2 = make_pair(k3, k4);if(p1<p2){cout<<"Yes";}else{cout<<"No";}return 0;
}

6.9algorithm头文件下的常用函数

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int a, b;scanf("%d%d", &a, &b);printf("%d", min(a, b));return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int a, b;scanf("%d%d", &a, &b);printf("%d", max(a, b));return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int a;scanf("%d", &a);printf("%d", abs(a));return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int a, b;scanf("%d%d", &a, &b);swap(a,b);printf("%d %d", a, b);return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int n,x;vector<int> v;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&x);v.push_back(x);}reverse(v.begin(), v.end());for (int i = 0; i < (int)v.size(); i++) {printf("%d", v[i]);if (i < (int)v.size() - 1) {printf(" ");}}return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int main() {string str;cin >> str;reverse(str.begin(), str.end());cout << str;return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;int main() {int n, x;scanf("%d", &n);vector<int> v;for (int i = 1; i <= n; i++) {v.push_back(i);}do {for (int i = 0; i < n; i++) {printf("%d", v[i]);if (i < n - 1) {printf(" ");} else {printf("\n");}}} while (next_permutation(v.begin(), v.end()));return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int n, k, a[100];scanf("%d%d", &n, &k);fill(a, a + n, k);for (int i = 0; i < n; i++) {printf("%d", a[i]);if (i < n - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;int main() {int n = 3, m = 5, k, a[3][5];scanf("%d", &k);fill(&a[0][0], &a[0][0] + n * m, k);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {printf("%d", a[i][j]);if (j < m - 1) {printf(" ");} else {printf("\n");}}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 10;
int a[MAXN];int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}sort(a, a + n);for (int i = 0; i < n; i++) {printf("%d", a[i]);if (i < n - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 10;
int a[MAXN];bool cmp(int a, int b) {return a > b;
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}sort(a, a + n, cmp);for (int i = 0; i < n; i++) {printf("%d", a[i]);if (i < n - 1) {printf(" ");}}return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;const int MAXN = 10;
string str[MAXN];int main() {int n;cin >> n;for (int i = 0; i < n; i++) {cin >> str[i];}sort(str, str + n);for (int i = 0; i < n; i++) {cout << str[i] << endl;}return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;struct Node {int x, y;Node(int _x, int _y) {x = _x;y = _y;}
};bool cmp(Node a, Node b) {if (a.x != b.x) {return a.x < b.x;} else {return a.y < b.y;}
}int main() {int n, x, y;scanf("%d", &n);vector<Node> v;for (int i = 0; i < n; i++) {scanf("%d%d", &x, &y);v.push_back(Node(x, y));}sort(v.begin(), v.end(), cmp);for (int i = 0; i < n; i++) {printf("%d %d\n", v[i].x, v[i].y);}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 10;
int a[MAXN];int main() {int n, k;scanf("%d%d", &n, &k);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}int pos = lower_bound(a, a + n, k) - a;printf("%d", pos + 1);return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 10;
int a[MAXN];int main() {int n, k;scanf("%d%d", &n, &k);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}int pos = upper_bound(a, a + n, k) - a;printf("%d", pos + 1);return 0;
}


http://www.ppmy.cn/news/1473678.html

相关文章

优化路由,优化请求url

1、使用父子关系调整下使其更加整洁 2、比如说我修改了下url,那所有的页面都要更改 优化&#xff1a;把这个url抽出来&#xff0c;新建一个Api文件夹用于存放所有接口的url&#xff0c;在业务里只需要关注业务就可以 使用时 导包 发请求 如果想要更改路径&#xff0c;在这里…

Go 语言多版本管理的最佳实践 —— Linux 和 Windows 专题20240702

Go 语言多版本管理的最佳实践 —— Linux 和 Windows 专题 引言 在软件开发的世界里&#xff0c;保持开发环境的最新和兼容至关重要。特别是 Go 语言&#xff0c;随着版本的更新&#xff0c;不同项目可能需要不同的 Go 版本。这时&#xff0c;如何在同一台机器上高效管理多个…

cpp http server/client

httplib 使用httplib库 basedemo server.cpp #include "httplib.h" #include <iostream> using namespace httplib;int main(void) {Server svr;svr.Get("/hello", [](const Request& req, Response& res) {std::cout << "lo…

Hi3861 OpenHarmony嵌入式应用入门--wifi hotspot

鸿蒙WiFi AP模式相关的API接口文件路径 foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot_config.h foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot.h 所使用的API接口有&#xff1a; API 接口说明 WifiErro…

邀请函 | 极限科技全新搜索引擎 INFINI Pizza 亮相 2024 可信数据库发展大会!

过去一年&#xff0c;在全球 AI 浪潮和国家数据局成立的推动下&#xff0c;数据库产业变革不断、热闹非凡。2024 年&#xff0c;站在中国数字经济产业升级和数据要素市场化建设的时代交汇点上&#xff0c;“2024 可信数据库发展大会” 将于 2024 年 7 月 16-17 日在北京悠唐皇冠…

Windows如何查看端口是否占用,并结束端口进程

需求与问题&#xff1a;前后端配置了跨域操作&#xff0c;但是仍然报错&#xff0c;可以考虑端口被两个程序占用&#xff0c;找不到正确端口或者后端接口书写是否规范&#xff0c;特别是利用Python Flask书写时要保证缩进是否正确&#xff01; Windows操作系统中&#xff0c;查…

昇思25天学习打卡营第16天|Diffusion扩散模型

导入必要的库函数 import math from functools import partial %matplotlib inline import matplotlib.pyplot as plt from tqdm.auto import tqdm import numpy as np from multiprocessing import cpu_count from download import downloadimport mindspore as ms import mi…

贵州建筑三类人员安全员2024年考试最新题库练习题

一、单选题 1.建设工程安全管理的方针是&#xff08;&#xff09;。 A.安全第一&#xff0c;预防为主&#xff0c;综合治理 B.质量第一&#xff0c;兼顾安全 C.安全至上 D.安全责任重于泰山 答案&#xff1a;A 2.安全生产管理的根本目的是&#xff08;&#xff09;。 A.…