STL用法

1.vector数组从尾部插入,尾部删除。相关用法代码如下:

复制代码
 1         //1.尾部插入及删除数字
 2     vec2.push_back(1);  //尾部插入元素
 3         vec2.pop_back()     //删除尾部元素
 4  
 5     //2.使用下标访问元素,
 6     cout << vec2[0] << endl; //记住下标是从0开始的。
 7  
 8     //3.使用迭代器访问元素.
 9     vector<int>::iterator it;
10     for (it = vec2.begin(); it != vec2.end(); it++)
11         cout << *it << endl;
12  
13     //4.插入元素:    
14     vec2.insert(vec2.begin() + i, a); //在第i + 1个元素前面插入a;
15  
16     
17     //5.删除元素:    
18     vec2.erase(vec2.begin() + 2); //删除第3个元素
19  
20     vec2.erase(vec2.begin() + i, vec2.end() + j); //删除区间[i, j - 1]; 区间从0开始
21  
22     //6.求数组大小:
23     vec2.size();
24  
25     //7.清空 : 
26     vec2.clear();
Code
复制代码

例题:Problem - B - Codeforces

该题利用两个vector分别存入小写和大写字母的下标,并且出现b和B的时候删除,最后按照下标从小到大输出

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4  
 5 const int N = 210,inf = 1e9;
 6 int n,m; 
 7 int _;
 8  
 9 int main()
10 {
11     ios::sync_with_stdio(0);
12     cin.tie(0);
13     cout.tie(0);
14     cin >>_;
15     while(_ --)
16     {
17       string s;
18       cin >> s;
19       int n = s.size();
20       vector<int> a,b; //用vector数组来存储大小写字母的下标
21       for (int i = 0; i < n; i ++ )
22       {
23           if(s[i] == 'b')
24           {
25               if(a.size()) a.pop_back(); //从尾部删除
26           }else if(s[i] == 'B'){
27               if(b.size()) b.pop_back();
28           }else if(s[i] >= 'a' && s[i] <= 'z'){
29               a.push_back(i);//尾部插入
30           }else {
31               b.push_back(i);
32           }
33       }
34       
35       int k1 = 0,k2 = 0; //定义两个指针
36       while(k1 < a.size() || k2 < b.size()){
37           if(k1 < a.size() &&(k2 >= b.size() || a[k1] < b[k2])) {//比较下标哪个小输出哪个
38               cout << s[a[k1]] ;
39               k1 ++;
40           }else 
41           {
42               cout << s[b[k2]];
43               k2 ++;
44           }
45       }
46       cout << endl;
47     }
48     return 0;
49 }
Code
复制代码

 2.vector + 哈希映射 Problem - 1890A - Codeforces

复制代码
 1     #include <bits/stdc++.h>
 2     using namespace std;
 3     
 4     bool ok(string s) {
 5       for (size_t i = 1; i < s.length(); ++i)
 6         if (s[i] == s[i - 1])
 7           return false;
 8       return true;
 9     }
10     
11     
12     string s;
13     void solve() {
14       int n;
15       cin >> n;
16       cin >> s;
17       int cnt0 = 0, cnt1 = 0;
18       for (int i = 0; i < s.length(); ++i) {
19         cnt0 += s[i] == '0';
20         cnt1 += s[i] == '1';
21       }
22       if (cnt0 != cnt1) {
23         cout << -1 << std::endl;
24         return;
25       }
26       vector<int> z;
27       deque<char> q;
28       for (int i = 0; i < s.length(); ++i)
29         q.push_back(s[i]);
30       
31       int d = 0;
32       while (!q.empty()) {
33         if (q.front() == q.back()) {
34           if (q.front() == '0') {
35             q.push_back('0');
36             q.push_back('1');
37             z.push_back(n - d);
38           } else {
39             q.push_front('1');
40             q.push_front('0');
41             z.push_back(0 + d);
42           }
43           n += 2;
44         }
45         while (!q.empty() && q.front() != q.back()) {
46           q.pop_back();
47           q.pop_front();
48           ++d;
49         }
50       }
51     
52       cout << z.size() << endl;
53       for (int i = 0; i < z.size(); ++i) {
54         cout << z[i];
55         if (i + 1 == z.size()) cout << endl;
56         else cout << " ";
57       }
58     }
59     
60    int main() {
61       int t;
62       cin >> t;
63       while (t--) solve();
64       return 0;
65     }
Code
复制代码

 3.用vector 来存储偏移量(向量)Problem - A - Codeforces

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long 
 4 #define cy cout << "Yes" << endl
 5 #define cn cout << "No" << endl
 6 
 7 const int N = 1e5 + 10,inf = 1e9;
 8 const int mode = 1e9 + 7;
 9 int _;
10 int n,m;
11 
12 int main(){
13     cin >> _;
14     while(_ -- ){
15     int a, b;
16     cin >> a >> b;
17     int xK, yK;
18     cin >> xK >> yK;
19     int xQ, yQ;
20     cin >> xQ >> yQ;
21     vector<int> dx = {a, a, -a, -a, b, b, -b, -b};
22     vector<int> dy = {b, -b, b, -b, a, -a, a, -a};
23     int ans = 0;
24     for (int i = 0; i < 8; i ++){
25       for (int j = 0; j < 8; j ++){
26         if (xK + dx[i] == xQ + dx[j] && yK + dy[i] == yQ + dy[j]){
27           ans++;
28         }
29       }
30     }
31     if (a == b){
32       ans /= 4;
33     }
34     cout << ans << endl;
35   }
36 }
Code
复制代码

 4.string 用法 Problem - F - Codeforces

首先用n个字符表示n1对 01字符串,然后在第一个0前面加n1个零构成n1对00字符串,在第一个1前面加n2个1构成n2个11字符串

然后特殊情况特判一下就好了,这里用string(n,'0')的用法

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define endl '\n'
 4 #define ll long long
 5 #define cy cout << "YES" << endl
 6 #define cn cout << "NO" << endl
 7 int _,n,m;
 8 const int N = 1e3 + 10,inf = 1e9;
 9 const int mod = 1e9 + 7;
10 int a[N],s[N],t[N];
11 
12 int main()
13 {
14     cin >> _;
15     while(_ -- )
16     {
17         int n0,n1,n2;
18         cin >> n0 >> n1 >> n2;
19         if(n1 == 0){
20             if(n0 != 0) cout << string(n0 + 1,'0') << endl;
21             else cout << string(n2 + 1,'1') << endl;
22             continue;
23         }
24 
25         string ans;
26         for (int i = 0; i < n1 + 1; i ++ ){
27             if(i & 1) ans += '0';
28             else ans += '1';
29         }
30         ans.insert(1,string(n0,'0'));
31         ans.insert(0,string(n2,'1'));
32         
33         cout << ans << endl;
34     }
35     return 0;
36 }
Code
复制代码

 

posted @   rw156  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示