各种STL的基本用法
STL及一些常用函数的基本用法
1.vector(向量)的基本用法
vector是C++的STL中一个常见的容器,使用时需要加上#include\的头文件,其可以动态储存数据,既可以很方便的实现插入,删除等基本操作,其基本语法如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a;
for(int i=0;i<10;i++)
a.push_back(i); //push_back can add data in the end of vector
int len=a.size(); //a.size() can return the length of vector,a.begin() return the first data's address,and a.end() is return last.
cout<<len<<endl;
a.insert(a.begin()+len,10); //insert a data in the len-th
a.erase(a.end()); //delete a end data of vector
a.pop_back(); //pop the last data
if(a.empty()) //judge whether this vector is empty
cout<<"The vector is empty!"<<endl;
//a.reverse(a.begin(),a.end()); //reverse all the data.
sort(a.begin(),a.end()); //sort the data from begin to the end of vector,can also add cmp function
vector<int>::iterator it;
for(it=a.begin();it<=a.end();it++) cout<<*it<<" ";
cout<<endl; //ergodic all the datas of vector
/*you can also use this operation to ergodic the data of vector
for(vector<int>::size_type i=0;i<=a.size();i++) cout<<a[i]<<" ";
cout<<endl;
*/
a.clear(); //clear all the datas of the vector
return 0;
}
2.queue(队列)的基本用法
队列是C++STL中一个常见的容器,其具有先进先出的特点,在使用时要加#include\的头文件,其基本语法如下:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> myqueue; //定义队列
for(int i=1;i<=5;i++)
myqueue.push(i); //将i压入队列的底部(末端)
cout<<myqueue.size()<<endl; //返回queue的长度
cout<<myqueue.front()<<endl; //返回队列的第一个元素(也就是最先进入队列的元素,不会删除这个元素)
if(myqueue.empty()) //判断这个queue中是否有元素(队列是否为空)
cout<<"The queue is empty!"<<endl;
myqueue.pop(); //删除第一个元素
cout<<myqueue.front()<<endl;
cout<<myqueue.back()<<endl; //返回queue的最后一个元素并将它输出出来(不删除)
return 0;
}
3.stack(栈)的基本操作
栈也是C++的STL中一种常见的容器,其使用时要加头文件#include\,是一种先进后出或者说后进先出的容器,其基本语法如下:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> mystack;
if(mystack.empty()) //判断这个stack是否为空,如果为空,则返回1
cout<<"The stack is empty!"<<endl;
for(int i=1;i<=5;i++)
mystack.push(i); //将元素push入stack中
cout<<mystack.size()<<endl; //返回这个栈的length
cout<<mystack.top()<<endl; //输出栈顶元素(最后加入的元素,不删除元素)
mystack.pop(); //弹出栈顶的元素(删除栈顶元素)
while(!mystack.empty()) //逆序遍历栈,但是栈的元素全部被删除
{
cout<<mystack.top()<<" ";
mystack.pop();
}
return 0;
}
4.set(集合)的基本用法
set是C++STL标准模板库的一个常用模板,其可以对里面的元素自动按升序排序,并去掉重复元素。其内部原理是红黑树,其基本语法如下:
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> myset; //初始化定义一个set
myset.insert(3); //向set中插入元素,注意set中没有Push这一成员函数,只能insert
myset.insert(6);
myset.insert(1);
myset.insert(10);
myset.insert(8);
myset.insert(3);
cout<<myset.size()<<endl; //返回set的大小
for(set<int>::iterator it=myset.begin();it!=myset.end();it++)
cout<<*it<<" ";
cout<<endl;
set<int>::iterator it=myset.find(3); //查找元素,返回元素所在的迭代器
cout<<*it<<endl; //要注意,虽然it是set的迭代器,但是并不支持it<myset.end()的写法
myset.erase(myset.find(1)); //删除元素,注意里面的参数是迭代器
myset.erase(3); //也可以把要删除的值直接做参数传入set的erase中
myset.erase(it,myset.end()); //区间删除,从it迭代器到末尾的元素全部删除
myset.clear(); //清空集合
return 0;
}
5.map(映射)的基本用法
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,使用时需包含头文件#include<map>下面是map的一些基本操作:
#include<iostream>
#include<utility>
#include<map>
#include<string>
using namespace std;
int main()
{
map<string,int> mymap,map1,map2; //定义map变量
map<string,int>::iterator it; //map的迭代器
//向map变量中添加元素
mymap.insert(pair<string,int>("a",1));
mymap.insert(pair<string,int>("c",3));
mymap.insert(pair<string,int>("b",2));
mymap.insert(pair<string,int>("d",4));
mymap.insert(pair<string,int>("e",5));
mymap.insert(pair<string,int>("f",6));
mymap["g"]=7;
//map的查找,注意,我们通过map的键查找
it=mymap.find("g");
if(it==mymap.end()) cout<<"We haven't find the key!"<<endl;
else
cout<<it->first<<" "<<it->second<<endl; //键对应first,值对应second
//map元素的删除
it=mymap.find("d");
if(it==mymap.end())
cout<<"We haven't find the key!"<<endl;
else
mymap.erase(it); //删除迭代器,即删除这个元素
//map的遍历
map<string,int>::iterator it1;
for(it1=mymap.begin();it1!=mymap.end();it1++)
cout<<it1->first<<" "<<it1->second<<endl;
//map的swap操作
//注意,map中的swap不是一个元素之间的交换,而是map两个容器之间的交换
map1.insert(pair<string,int>("be",1));
map2.insert(pair<string,int>("af",2));
map1.swap(map2);
map<string,int>::iterator it2;
for(it=map1.begin();it!=map1.end();it++)
cout<<it->first<<" "<<it->second<<endl;
//map的sort问题,map不能使用sort,因为map默认将插入的元素按照键的从小到大顺序排列
cout<<mymap.size()<<endl; //返回map的迭代器数目
/*
以下为map的一些常见子函数
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数*/
return 0;
}
6.unorded_map的用法
unordered_map也是stl的容器之一,不过它与map的不同之处在于:unordered_map使用哈希的方式储存,所以其查找的效率能达到\(O(1)\)的效果,下面是其及基本用法:
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
unordered_map<string,double> m;
int main() {
m.insert(make_pair("PI", 3.14));
m["root2"] = 1.414;
m["root3"] = 1.732;
m["log10"] = 2.302;
m["loge"] = 1.0; //插入值
m.insert(make_pair("e", 2.718));
string key = "PI";
if (m.find(key) == m.end()) //没找到
cout << key << " not found\n\n";
else cout << "Found " << key << "\n\n";
//cout << m["Pi"] << endl; 是可行的
unordered_map<string, double>::iterator it;
for (it = m.begin(); it != m.end(); it++) //遍历元素
cout << it->first << " " << it->second << endl;
system("pause");
return 0;
}
7.list(列表)的基本用法
list是一种序列式容器。list容器完成的功能实际上和数据结构中的双向链表是极其相似的,list中的数据元素是通过链表指针串连成逻辑意义上的线性表,也就是list也具有链表的主要优点,即:在链表的任一位置进行元素的插入、删除操作都是快速的。在使用list时需加头文件#include,其基本语法如下:
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> mylist,mylist2;
for(int i=1; i<=5; i++)
mylist.push_back(i); //尾插法
mylist.push_front(0); //头插法
if(mylist.empty())
cout<<"The list is empty!"<<endl;
cout<<mylist.size()<<endl;
mylist.insert(mylist.end(),6); //插入元素
mylist2.insert(mylist2.begin(),10);
mylist.erase(mylist.begin()); //删除某一个元素
//mylist.erase(mylist.begin(),mylist.end()); 删除某一个区间的元素
list<int>::iterator it; //list的迭代器
cout<<"Before swap:"<<endl;
for(it=mylist.begin(); it!=mylist.end(); it++)
cout<<*it<<" "; //list的遍历
cout<<endl;
mylist.pop_front(); //删除第一个list元素,pop_back(),删除最后一个元素
mylist.resize(7); //重新设置list的最大长度,超出范围的将会被删除
mylist.swap(mylist2);
cout<<"After swap:"<<endl;
for(it=mylist.begin();it!=mylist.end();it++)
cout<<*it<<" ";
cout<<endl;
mylist.merge(mylist2); //将mylist2归并到mylist中去
cout<<"After merge:"<<endl;
for(it=mylist.begin();it!=mylist.end();it++)
cout<<*it<<" ";
cout<<endl;
return 0;
}
7.next_premutation(全排列)的用法
next_permutation可以方便快捷的实现全排列
arr[5]={0,1,2,3,4};
sort(arr,arr+5);
do{
for(int i=0;i<5;++i)
cout<<arr[i]<<' ';
cout<<endl;
}while(next_permutation(arr,arr+5));
//注意,该库函数是按照字典序的顺序完成全排列的
8.stringstream的用法
stringstream可以对getline很好的进行分离:
string s,word;
getline(cin,s);
stringstream str(s);
while(str>>word)
cout<<word<<endl;
9.unique的用法
unique可以用来去重,该函数返回容器中不同元素的个数
//以下标为1开始的数组为例
int tot=unique(arr+1,arr+n+1)-(arr+1); //n为总元素个数
int tot=unique(vec.begin(),vec.end())-vec.begin(); //计算vector不同元素数量
10.lower_bound的用法
该函数可以计算指定位置第一个大于等于传入参数的元素的下标,前提是数组单调(如果还要求不重复的话可先用unique去个重,再sort一下就好了)
int pos=lower_bound(a+1,a+n+1,x)-a; //数组下标从1开始
//在vector中也是类似
int val=*--upper_bound(vec.begin(),vec.end(),x); //返回第一个大于等于x的数值(假设一定存在)
11.string中的substr的用法
截取一个子字符串
#include <bits/stdc++.h>
using namespace std;
int main()
{
string x="Hello_World";
cout<<x.substr()<<endl; //cout<<"Hello_World"<<endl;
cout<<x.substr(5)<<endl; //截取x[5]到结尾,cout<<"_world"<<endl;
cout<<x.substr(0,5)<<endl; //以x[0]为始,向后截取5位(包含x[0])cout<<"Hello"<<endl;
/*指定的截取长度加起始位置即_off+_count>源字符串的长度,则子字符串将延续到源字符串的结尾*/
system("pause");
}
各种STL的基本用法的更多相关文章
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- STL的其他用法(adjacent_find, find_first_of, sort_heap, merge, binary_search)总结
2017-08-20 17:26:07 writer:pprp 1.adjacent_find() 下面是源码实现: template <class ForwardIterator> Fo ...
- C++-STL:vector用法总结
目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...
- STL中map用法
Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于 这个特性,它完成有可能在我们处理一对一数据的 ...
- [STL] SET实用用法
背景 今天考试深受平衡树之害,可以参见上一篇博客,想到了set却苦于实用的不熟练.同时QTY询问set的具体用法,所以写这篇博客,同时留作自用. 分类 参看了一下网上其他set博客,上来都是长篇大论概 ...
- C++中的STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- C++ STL sort()函数用法
C++STL提供的在里的排序函数,有以下两种形式 此外还提供有稳定排序版本stable_sort(),用法类似. 第一种形式: template <class RandomAccessItera ...
- (转载) STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- STL的常用用法、函数汇总(不定时更新)
隶书文字为原创. 1.vector 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vec ...
- STL set 详细用法
一个集合(set)是一个容器,它其中所包含的元素的值是唯一的. 用到的库 #include <set> 定义 最简单: set<int> a; set和其他的stl一样,都支持 ...
随机推荐
- 深入delphi编程理解之消息(五)重写(override)dispatch、wndproc方法和Application.OnMessage事件
dispatch.wndproc是VCL framework在TWinCtronl定义的虚拟方法,下面程序通过重写(override)这两函数拦截WM_LBUTTONDOWN消息,来与Applicat ...
- MyBatis-Spring整合之方式4
直接删除Mybatis的配置文件,修改Beans.xml文件的sqlSessionFactory的参数内容,如下: <!--配置sqlSessionFactory--> <bean ...
- 利用单臂路由实现VLAN间的路由
实验4:利用单臂路由实现VLAN间的路由. 实验原理: 实验内容: 本实验模拟公司网络场景,路由器R1是公司的出口网关,员工PC通过接入层交换机(如S2和S3)接入公司网络,接入层交换机又通过汇聚交 ...
- Java web 会话技术 cookie与session
一.会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行会话的过程 ...
- 4 中文乱码 selenium的使用
# 中文乱码 #处理中文乱码 import requests from lxml import etree from urllib import request url = 'http://pic.n ...
- HTML-移动端-rem px vw vh 的转换
vw/vh rem px 三者的转换(快速入门移动端页面编写) 1:三种单位的转换 2:如何适配移动端的不同设备 前提知识: 手机端的长宽是实际设计过程中的两倍 比如手机端是 750 * 1200 那 ...
- python ui自动化之元素定位和常用操作
做ui自动化的最基础的就是页面元素定位了,如果连页面元素都定位不到,自动化从何谈起呢?接下来我们就看看页面元素定位的方法吧!(这里就用百度页面来进行演示) 一.最通用的几种定位方式: 1.通过id定位 ...
- SSIS 从oracle 转数据回MS SQL
SSIS 从oracle 转数据回MS SQL,转每月的销售记录,大概15000以内,在开发机器上没问题,部署到生产环境出现各种状况“ SSIS 无法从连接伺服器的OLE DB提供者BULK 提取资料 ...
- JavaScirpt - 模块的写法
传送门 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 1. 原始写法 function f1() { // do sth. ...
- 域名和URL各部分组成
什么是域名? 域名,是由一串用点分隔的名字组成的Internet上某一台计算机或计算机组的名称,用于在数据传 输时标识计算机的电子方位. IP地址是Internet主机的作为路由寻址用的数字型标识,人 ...