【c++】vector容器&set容器的基本操作
来源:互联网 发布:黑镜第一季第二集 知乎 编辑:程序博客网 时间:2024/05/17 08:27
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
阅读全文
1 0
- 【c++】vector容器&set容器的基本操作
- C/C++ set容器的基本操作
- C/C++ set容器的基本操作
- vector向量容器的一些基本操作
- STL的vector容器基本操作
- c++ 容器vector的基本操作
- set容器与map容器基本操作
- Vector容器的基本用法
- C++STL容器的基本操作
- vector容器的基本操作(输入和输出)
- 对vector容器的操作
- 【转】vector向量容器 基本操作
- C++ vector容器基本操作小练
- C++STL中vector容器类的简单操作
- C++---set容器的用法
- C++容器:容器的基本操作
- vector容器基本用法
- STL基本容器: string、vector、list、deque、set、map
- ENG Note 2 : 语料
- 软件工程(C编码实践篇)--期末终结
- SE02 Unit06 TCP通信 、 UDP通信
- 用maven创建web项目时无法创建src/main/java等源目录
- 二叉树的前序,中序,后序遍历原则
- 【c++】vector容器&set容器的基本操作
- git---window系统中如何通过git命令符生成秘钥SSH Key,向GitHub添加密钥 ?
- SpringJdbc访问数据库
- hadoop新版的api接口实现启动运行hadoop代码
- 回顶部
- win10你没有权限在此位置中保存文件
- EditPlus技巧集锦
- Java+Selenium3方法篇14-元素操作之输入和点击
- 相会·《基层风云·1》·二