跳到正文

个人记录

笔记:洛谷网校

#SUSPENDED#约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年1月31日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 11 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    /* ---------------------------------------------- */
    // vector
    vector<int> v = {1, 7, 2, 8, 9, 2};
    cout << v.size() << endl; // 6
    vector<int> v1 = v;
    v1.clear();
    cout << v1.size() << endl; // 0
    v1.push_back(115);
    cout << v1.size() << " " << v1.empty() << endl; // 1 0
    cout << v[3] << endl; // 8
    v.pop_back();
    cout << v.size() << endl; // 5
    v.insert(v.begin() + 2, 999);
    cout << v[2] << " " << v[3] << endl; // 999 2
    v.erase(v.begin() + 2, v.begin() + 3 + 1);
    cout << v[2] << endl; // 8
    v.erase(v.begin());
    cout << v[0] << endl; // 7 

    

    // v.begin() 是 v 的第一个元素的位置
    // v.end() 是 v 的最后一个元素的后面的位置
    // v.front() 是第一个元素本身
    // v.back() 是最后一个元素本身

    // &v[0] = v.begin()
    // &v[v.size()] = v.end()
    // v[0] = v.front()
    // v[v.size() - 1] = v.back()
    // rbegin, rend

    v.front() = 329742;
    cout << v[0] << endl;

    /* ---------------------------------------------- */
    
    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // deque
    deque<int> dq = deque<int>({1, 7, 2, 8, 9, 2});
    cout << dq.front() << " " << dq.back() << endl; // 1 3
    cout << *dq.begin() << " " << *(--dq.end()) << endl; // 1 3
    dq.pop_front(), dq.pop_back();
    cout << dq.front() << " " << dq.back() << endl; // 7, 9
    dq.push_front(168123), dq.push_back(619582);
    cout << dq.front() << " " << dq.back() << endl; // 7, 9
    auto dqit = dq.begin();
    dqit++; dqit++; dqit++;
    dq.insert(dqit, -777777);
    cout << *dqit << " " << *(--dqit) << endl; // -777777 2

    // operator[]
    // (相对于其他)占用资源太多
    
    /* ---------------------------------------------- */
    
    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // stack
    stack<int> s = stack<int>({1, 7, 2, 8, 9, 3});
    cout << s.top() << endl; // 3
    s.pop();
    s.push(4);
    cout << s.top() << endl; // 4
    /* ---------------------------------------------- */
    
    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // queue
    queue<int> q = queue<int>({1, 7, 2, 8, 9, 3});
    cout << q.front() << endl; // 1
    q.pop();
    q.push(4);
    cout << q.front() << endl; // 7
    /* ---------------------------------------------- */
    
    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // list
    list<int> l = list<int>({1, 7, 2, 8, 9, 3});
    cout << l.front() << " " << l.back() << endl; // 1 3
    cout << *l.begin() << " " << *(--l.end()) << endl; // 1 3
    l.pop_front(), l.pop_back();
    cout << l.front() << " " << l.back() << endl; // 7, 9
    l.push_front(168123), l.push_back(619582);
    cout << l.front() << " " << l.back() << endl; // 7, 9
    auto lit = l.begin();
    lit++; lit++; lit++;
    l.insert(lit, -777777);
    cout << *lit << " " << *(--lit) << endl; // 8 -777777
    /* ---------------------------------------------- */
    
    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // map
    map<string, int> mp;
    mp["XiaoMing"] = 9999999;
    mp["XiaoHong"] = 8888888;
    cout << mp["XiaoMing"] << endl; // 9999999
    cout << mp["XiaoGang"] << endl; // 0
    mp.erase("XiaoMing");
    cout << mp["XiaoMing"] << endl; // 0
    /* ---------------------------------------------- */
    
    // find, count

    cout << "------------------------------------------" << endl;

    /* ---------------------------------------------- */
    // set 集合,维护不重复元素的、一直是有序的序列
    set<int> st = {1, 5, 2, 11, 10, 8};
    cout << *(st.find(10)) << endl; // 10
    st.insert(4);
    cout << *(st.lower_bound(4)) << endl; // 4
    cout << *(st.upper_bound(4)) << endl; // 5
    st.erase(10);
    cout << *(st.upper_bound(9)) << endl; // 11
    /* ---------------------------------------------- */
    return 0;
}

bfs的实现;以及struct的详解;高精度的原理/实现(加减乘除)/以及封装为结构体;

  • bfs
  • struct // class
    • struct 来自于 C 语言, C ++ 中struct 实际上定义了 “类”
    • 类 - OI Wiki (oi-wiki.org)
    • 可以重载运算符, 定义成员函数。
  • 高精度
    • 原理 高精度计算 - OI Wiki (oi-wiki.org)
    • 除法部分, 可以模拟减法, 也可以二分
    • 二分, 在每一位只有 10 的时候, 10, ln(10) = 3, 4
    • 压位高精度除法
      • 10000
      • log(10000) 10000
  • inline 是干什莫用的
  • T334919
    • 可以得到最小的数就是提取 xx​ 所有质数因子, 然后乘起来。
  • 优化程序的方法·
    • 优化算法。
      • 去找, 你的代码做了什么事, 你具体花了多少时间用来做这个事, 能不能用更少的时间来做事。
      • n^4 , 在你的脑海里, 你记得, 这个问题只要 n^3 , 你就可以开始对着这个部分思考, 能不能降低复杂度。
    • 优化常数。
      • register 不要用, 已经没有用了, 在c++11/14之后没用了。
      • 寻址连续
      • 循环展开 for (i = 1; i <= n; i ++ ) || for (i = 1; i <= n; i += 2) for (i = 2; i <= n; i += 2) // 4~8
      • endl;
        • 缓冲区 : 输出的时候, 如果每次cout一次就打印到终端或者是文件, 这样子要频繁与文件或者终端交互。很耗费时间。
        • endl慢的原因就是每次 cout << t << endl;
        • printf, cout << t << ‘\n’;
        • ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); 关闭流同步
  • 输入
    • linux \n
    • windows \n\r
    • cin >> x; \r getline (s, cin);
原文
存档评论8 条抓取于