欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【c++】vector容器&set容器的基本操作

程序员文章站 2022-03-22 22:16:40
...

vector顺序存储, 动态开辟空间,当需要实现拉链发的hash很方便而且也可以用作邻接表存图。
常用方法:push_back(), pop_back(), size(), clear(), insert(), erase();
其中:erase()可以删除单个元素,也可以删除一个区间的元素.erase(it)it为迭代器。
erase(first, last)即删除[first, last)内的元素。比如V.erase(V.begin()+1, V.end())

#include <iostream>
#include <vector>
#include <cstdio>
#include <vector>
using namespace std;
void travelMethodFirst(vector<int> myVector);
void travleMethodSecond(vector<int> myVector);
void deleteElementMethodFirst(vector<int>& myVector, int index);
void deleteElementMethodSecod(vector<int>& myVector, int from, int to);
void deleteLastElement(vector<int>& myVector);
void insertElement(vector<int> &myVector, int pos, int element);
int main()
{
    vector<int> V;
    //增加
    for(int i = 0;i < 10; i++) {
        V.push_back(i);
    }
    travleMethodSecond(V);
    //删除第三个元素
    vector<int>::iterator it = V.begin();
    deleteElementMethodFirst(V, 3);
    travleMethodSecond(V);
    deleteElementMethodSecod(V, 6, 9);
    travelMethodFirst(V);
    deleteLastElement(V);
    travleMethodSecond(V);
    V.clear();
    for(int i = 0;i < 10; i++) {
        V.push_back(i);
    }
    travelMethodFirst(V);
    insertElement(V, 2, 100);
    travelMethodFirst(V);
    return 0;
}
//在下标为pos的位置插入一个元素.
void insertElement(vector<int> &myVector, int pos, int element) {
    if(myVector.size() > (unsigned)pos) {
        myVector.insert(myVector.begin()+pos, element);
    }
}

//根据下标删除元素并且删除单个元素
void deleteElementMethodFirst(vector<int>& myVector, int index) {
    if(myVector.size() > (unsigned)index) {
        vector<int>::iterator it = myVector.begin();
        myVector.erase(it + index);
    }
}
//根据下标删除元素并且删除一个区间的元素[from, to)
void deleteElementMethodSecod(vector<int>& myVector, int from, int to) {
    if(from > to) {
        swap(from, to);
    }
    if((unsigned)to <= myVector.size()) {
        vector<int>::iterator it = myVector.begin();
        myVector.erase(it+from, it+to);
        //myVector.erase((myVector.begin()+from, myVector.begin()+to));
    }
}
//删除最后一个元素
void deleteLastElement(vector<int>& myVector) {
    if(!myVector.empty()) {
        myVector.pop_back();
    }
}

//下标方式访问
void travelMethodFirst(vector<int> myVector) {
    for(unsigned i = 0; i < myVector.size(); i++) {
        cout << myVector[i] << "  ";
    }
    cout << endl;
}
//指针方式访问
void travleMethodSecond(vector<int> myVector) {
    vector<int>::iterator it = myVector.begin();
    for(unsigned i = 0; i < myVector.size(); i++) {
        printf("%d  ",  *(it+i));
    }
    cout << endl;
}

set是一个内部自动有序且不含重复元素的容器。主要作用是自动去重并按升序排序。
如果自定义结构体,需要比较函数。
注意:set只能通过迭代器(iterator)访问, set::iterator it;
而且可以通过it访问set的元素,由于除vector和string之外的STL容器都不支持 (it+i)的访问方式。
常用方法:insert(), find(), erase(), size(), clear();
其中:erase()有两种:对于set st;可以用st.erase(value)或者st.erase(it)it为迭代器。

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

struct info {
    int value;
    string str;
    info(int val, string mystr) {
        value = val;
        str = mystr;
    }
    info() {}
    bool operator < (const info &a) const {
        if(a.value != value) {
            return a.value > value;
        } else {
            return a.str > str;
        }
    }
};

//输出
void output(set<info> mySet) {
    set<info>::iterator it = mySet.begin();
    for(; it != mySet.end(); it++) {
        cout << it->value << " " << it->str << endl;
    }
}
//查找
void testFindMethod(set<info>& myInfoSet, info tmp) {
    set<info>::iterator it = myInfoSet.find(tmp);
    if(it != myInfoSet.end()) {
        cout << "查找结果: " << it->value << " " << it->str << endl;
    }
}
//查找并删除
void testDeleteMethodOne(set<info>& myInfoSet, info tmp) {
     set<info>::iterator it = myInfoSet.find(tmp);
    if(it != myInfoSet.end()) {
        cout << "查找结果: " << it->value << " " << it->str << endl;
        myInfoSet.erase(it);
       // myInfoSet.erase(tmp);
        cout << "*****删除后的结果*****" << endl;
        output(myInfoSet);
    }
}
//删除一个区间内的元素
void testDeleteMethodTwo(set<info>& myInfoSet, info tmp) {
    set<info>::iterator it = myInfoSet.find(tmp);
    cout << "删除前:"  << endl;
    output(myInfoSet);
    if(it != myInfoSet.end()) {
        myInfoSet.erase(it, myInfoSet.end());
    }
    cout << "删除后:"  << endl;
    output(myInfoSet);

}

//测试
int main()
{
    set<info> myInfoSet;
    info read;
    read.value = 100;
    read.str = "alibaba";
    myInfoSet.insert(read);
    read.value = 10;
    read.str = "baidu";
    myInfoSet.insert(read);
    read.value = 10;
    read.str = "baidux";
    myInfoSet.insert(read);
    read.value = 20;
    read.str = "jd";
    myInfoSet.insert(read);
     read.value = 30;
    read.str = "huawei";
    myInfoSet.insert(read);
    cout << myInfoSet.size() << endl;
    output(myInfoSet);

    info tmp(10, "baidu");
    testFindMethod(myInfoSet, tmp);
    testDeleteMethodOne(myInfoSet, tmp);
    info temp(30, "huawei");
    testDeleteMethodTwo(myInfoSet, temp);
    return 0;
}

#include< bits/stdc++.h> 包含的头文件。
目前国内oj中,poj,hdu 不支持这个函数,这几个oj的编译器问题,其他国外的oj,还有*的oj都支持,CF,Topcoder也都支持。

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif