zoukankan      html  css  js  c++  java
  • 一个简单的查找替换算法

    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    
    bool _insert_single_video(
                std::vector<int>* all_tmp_res,
                const int insert_pos,
                int single_video) {
        
        int cur_pos = 0;
        auto it = all_tmp_res->begin();
        auto it_last_pos = it;
        int insert_success = 0;
        while (it != all_tmp_res->end()) {
            if (cur_pos == insert_pos) {
    
                cout << "test--------1:"<< cur_pos << endl;
    
                it = all_tmp_res->insert(it, single_video);
                it_last_pos = it;
                ++it;
                ++cur_pos;
                insert_success += 1;
                continue;
            }
            if (*it == single_video) {
                
                cout << "test--------2:"<< *it<< endl;
                
                it = all_tmp_res->erase(it);
                it_last_pos = it;
                insert_success += 2;
                break;
            }
            else {
                cout << "test--------3:"<< *it<< endl;
                ++it;
                ++cur_pos;
            }
        }
        if (insert_success != 3) {
            cout << "test--------4:"<< insert_success<< endl;
            
            if (insert_success == 1) {
            
                cout << "test--------5:"<< insert_success <<":" << *it_last_pos<< endl;
                //Undo insert-
                all_tmp_res->erase(it_last_pos);
            }
            if (insert_success == 2) {
                //Undo erase
                cout << "test--------6:"<< insert_success <<":" << *it_last_pos<< endl;
                all_tmp_res->insert(it_last_pos, single_video);
            }
            return false;
        }
        return true;
    }
    
    
    int main() {
        
        vector<int> v;
        int a[11] = {0, 1, 9, 3, 4, 5, 6, 7, 8, 9, 10};
        v.insert(v.begin(), a, a+11);
        
        _insert_single_video(&v, 7, 9);
    
        for (auto it = v.begin(); it != v.end(); it++) {
            cout << *it <<endl;
        }
        return 0;
    }
  • 相关阅读:
    foj 2111 Problem 2111 Min Number
    hdoj 1175 连连看
    poj 2377 Bad Cowtractors
    poj 3666 Making the Grade
    2018华南理工大学程序设计竞赛 H-对称与反对称
    hdoj 4293 Groups
    FOJ Problem 2273 Triangles
    poj 3411 Paid Roads
    Codeforces 235A. LCM Challenge
    离散对数二连 poj 2417 Discrete Logging & HDU 2815 Mod Tree
  • 原文地址:https://www.cnblogs.com/TMatrix52/p/12506822.html
Copyright © 2011-2022 走看看