内容简介:【STL】set map的基本用法
set
public member function
set ::begin
iterator begin (); const_iterator begin () const;
Return iterator to beginning
Returns an iterator referring to the first element in the set container.
Internally, set containers keep their elements ordered from lower to higher, therefore begin returns the element with the lowest key value in the set .
Parameters
none
Return Value
An iterator to the first element in the container.
Both iterator and const_iterator are member types. In the set class template, these are bidirectional iterators.
Example
// set::begin/end
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[] = {75,23,65,42,13};
set<int> myset (myints,myints+5);
set<int>::iterator it;
cout << "myset contains:";
for ( it=myset.begin() ; it != myset.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
Output:
myset contains: 13 23 42 65 75
public member function
set ::end
iterator end (); const_iterator end () const;
Return iterator to end
Returns an iterator referring to the past-the-end element in the set container.
Parameters
none
Return Value
An iterator to the element past the end of the container.
Both iterator and const_iterator are member types. In the set class template, these are bidirectional iterators.
Example
// set::begin/end
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[] = {75,23,65,42,13};
set<int> myset (myints,myints+5);
set<int>::iterator it;
cout << "myset contains:";
for ( it=myset.begin() ; it != myset.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
Output:
myset contains: 13 23 42 65 75
代码实现
void testSet()
{
set<int> st;
st.insert(1);
st.insert(2);
st.insert(2);
st.insert(3);
st.insert(4);
st.insert(5);
set<int>::iterator it=st.begin();
while (it!=st.end())
{
cout<<*it<<endl;
it++;
}
cout<<endl;
//cout<<st.count(2)<<endl;
}
multiset
public member function
multiset ::begin
iterator begin (); const_iterator begin () const;
Return iterator to beginning
Returns an iterator referring to the first element in the multiset container.
Internally, multiset containers keep their elements ordered from lower to higher, therefore begin returns the element with the lowest key value in the multiset .
Parameters
none
Return Value
An iterator to the first element in the container.
Both iterator and const_iterator are member types. In the multiset class template, these are bidirectional iterators.
Example
// multiset::begin/end
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[] = {42,71,71,71,12};
multiset<int> mymultiset (myints,myints+5);
multiset<int>::iterator it;
cout << "mymultiset contains:";
for ( it=mymultiset.begin() ; it != mymultiset.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
Output:
mymultiset contains: 12 42 71 71 71
public member function
multiset ::end
iterator end (); const_iterator end () const;
Return iterator to end
Returns an iterator referring to the past-the-end element in the multiset container.
Parameters
none
Return Value
An iterator to the element past the end of the container.
Both iterator and const_iterator are member types. In the multiset class template, these are bidirectional iterators.
Example
// multiset::begin/end
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints[] = {15,98,77,77,39};
multiset<int> mymultiset (myints,myints+5);
multiset<int>::iterator it;
cout << "mymultiset contains:";
for ( it=mymultiset.begin() ; it != mymultiset.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
Output:
mymultiset contains: 15 39 77 77 98
代码实现
void testMULTISET()
{
multiset<int> mst;
mst.insert(12);
mst.insert(12);
mst.insert(13);
mst.insert(14);
mst.insert(16);
mst.insert(17);
multiset<int>::iterator it=mst.begin();
while (it!=mst.end())
{
cout<<*it<<endl;
it++;
}
cout<<endl;
//cout<<mst.count(12)<<endl;
}
map
public member function
map ::begin
iterator begin (); const_iterator begin () const;
Return iterator to beginning
Returns an iterator referring to the first element in the map container.
Internally, map containers keep their elements ordered by their keys from lower to higher , therefore begin returns the element with the lowest key value in the map .
Parameters
none
Return Value
An iterator to the first element in the container.
Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.
Dereferencing this iterator accesses the element's value, which is of type pair <const Key,T> .
Example
// map::begin/end
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
Output:
a => 200 b => 100 c => 300
public member function
map ::end
iterator end (); const_iterator end () const;
Return iterator to end
Returns an iterator referring to the past-the-end element in the map container.
Parameters
none
Return Value
An iterator to the element past the end of the container.
Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.
Dereferencing this iterator accesses the element's value, which is of type pair <const Key,T> .
Example
// map::begin/end
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
Output:
a => 200 b => 100 c => 300
代码实现
void testCOUNTmap()
{
map<string,int> CountMap;
CountMap.insert(pair<string,int>("小白",3));
CountMap.insert(pair<string,int>("小黑",2));
CountMap.insert(pair<string,int>("小黄",1));
CountMap.insert(pair<string,int>("小红",3));
map<string,int>::iterator it=CountMap.begin();
while (it!=CountMap.end())
{
cout<<" count:"<<CountMap.count((*it).first)<<" 名称:"<<(*it).first<<" 编号:" <<(*it).second<<endl;
it++;
}
}
求map中前K多的内容
struct CMPbyVALUE
{
bool operator()(const pair<string,int>&left,const pair<string,int>&right)
{
return left.second>right.second;
}
};
void GetTopKMap1(const int k)
{
vector<pair<string,int>> v;
map<string,int> mp;
map<string,int>::iterator it;
for (int i=0;i<10;i++)
{
mp["苹果"]++;
}
for (int i=0;i<9;i++)
{
mp["梨"]++;
}
for (int i=0;i<8;i++)
{
mp["桃子"]++;
}
for (int i=0;i<7;i++)
{
mp["哈密瓜"]++;
}
for(it=mp.begin();it!=mp.end();it++)
{
v.push_back(make_pair(it->first,it->second));
}
sort(v.begin(),v.end(),CMPbyVALUE());
for(int j=0;j<k;j++)
cout<<v[j].first<<" "<<v[j].second<<endl;
cout<<endl;
}
所有代码
#include <iostream>
#include<algorithm>
#include <string>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
using namespace std;
void testSet()
{
set<int> st;
st.insert(1);
st.insert(2);
st.insert(2);
st.insert(3);
st.insert(4);
st.insert(5);
set<int>::iterator it=st.begin();
while (it!=st.end())
{
cout<<*it<<endl;
it++;
}
cout<<endl;
//cout<<st.count(2)<<endl;
}
void testMULTISET()
{
multiset<int> mst;
mst.insert(12);
mst.insert(12);
mst.insert(13);
mst.insert(14);
mst.insert(16);
mst.insert(17);
multiset<int>::iterator it=mst.begin();
while (it!=mst.end())
{
cout<<*it<<endl;
it++;
}
cout<<endl;
//cout<<mst.count(12)<<endl;
}
void testMap()
{
map<string,int> numNameMap;
pair<map<string,int>::iterator,bool> ret;
numNameMap.insert(pair<string,int>("小白",3));
numNameMap.insert(pair<string,int>("小黑",2));
numNameMap.insert(pair<string,int>("小黄",1));
numNameMap.insert(pair<string,int>("小红",3));
map<string,int>::iterator it;
for(it=numNameMap.begin();it!=numNameMap.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
ret=numNameMap.insert(pair<string,int>("小红",3));
if (ret.second==false)
{
cout<<ret.first->first<<" "<<ret.first->second<<endl;
}
}
//void TopCountMap(vector<string>& v)
//{
// map<string,int> CountMap;
// CountMap.insert(pair<string,int>("苹果",1));
// CountMap.insert(pair<string,int>("苹果",1));
//
// CountMap.insert(pair<string,int>("苹果",1));
//
// CountMap.insert(pair<string,int>("苹果",1));
//
// CountMap.insert(pair<string,int>("苹果",1));
//
// CountMap.insert(pair<string,int>("苹果",1));
// map<string,int>::iterator i=CountMap.begin();
//
// for(;i!=CountMap.end();i++)
// {
// pair<map<string,int>::iterator,bool> ret
// =CountMap.insert(pair<string,int>(v[i],1));
// }
// for(int i=0;i!=CountMap.size();i++)
// {
// pair<map<string,int>::iterator,bool> ret=CountMap.insert(pair<string,int>(v[i],1));
// cout<ret.first<<" "<<ret.second <<endl;
// }
//
//
// }
void testCOUNTmap()
{
map<string,int> CountMap;
CountMap.insert(pair<string,int>("小白",3));
CountMap.insert(pair<string,int>("小黑",2));
CountMap.insert(pair<string,int>("小黄",1));
CountMap.insert(pair<string,int>("小红",3));
map<string,int>::iterator it=CountMap.begin();
while (it!=CountMap.end())
{
cout<<" count:"<<CountMap.count((*it).first)<<" 名称:"<<(*it).first<<" 编号:" <<(*it).second<<endl;
it++;
}
}
struct CMPbyVALUE
{
bool operator()(const pair<string,int>&left,const pair<string,int>&right)
{
return left.second>right.second;
}
};
void GetTopKMap1(const int k)
{
vector<pair<string,int>> v;
map<string,int> mp;
map<string,int>::iterator it;
for (int i=0;i<10;i++)
{
mp["苹果"]++;
}
for (int i=0;i<9;i++)
{
mp["梨"]++;
}
for (int i=0;i<8;i++)
{
mp["桃子"]++;
}
for (int i=0;i<7;i++)
{
mp["哈密瓜"]++;
}
for(it=mp.begin();it!=mp.end();it++)
{
v.push_back(make_pair(it->first,it->second));
}
sort(v.begin(),v.end(),CMPbyVALUE());
for(int j=0;j<k;j++)
cout<<v[j].first<<" "<<v[j].second<<endl;
cout<<endl;
}
//void GetTopKMap2(const int k)
//{
// vector<pair<string,int>> v;
// map<string,int> mp;
// map<string,int>::iterator it;
// for (int i=0;i<10;i++)
// {
// mp["苹果"]++;
// }
// for (int i=0;i<9;i++)
// {
// mp["梨"]++;
// }
// for (int i=0;i<8;i++)
// {
// mp["桃子"]++;
// }
// for (int i=0;i<7;i++)
// {
// mp["哈密瓜"]++;
// }
// for(it=mp.begin();it!=mp.end();it++)
// {
// v.push_back(make_pair(it->first,it->second));
// }
// v.reserve(v.size());
// sort(v.begin(),v.end(),CMPbyVALUE());
//
// for(int j=0;j<k;j++)
// cout<<v[j].first<<" "<<v[j].second<<endl;
// cout<<endl;
//}
int main()
{
vector<string> v;
testSet();
testMULTISET();
testMap();
testCOUNTmap();
GetTopKMap1(3);
GetTopKMap1(4);
system("pause");
return 0;
}
结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python深度学习
[美] 弗朗索瓦•肖莱 / 张亮 / 人民邮电出版社 / 2018-8 / 119.00元
本书由Keras之父、现任Google人工智能研究员的弗朗索瓦•肖莱(François Chollet)执笔,详尽介绍了用Python和Keras进行深度学习的探索实践,涉及计算机视觉、自然语言处理、生成式模型等应用。书中包含30多个代码示例,步骤讲解详细透彻。由于本书立足于人工智能的可达性和大众化,读者无须具备机器学习相关背景知识即可展开阅读。在学习完本书后,读者将具备搭建自己的深度学习环境、建......一起来看看 《Python深度学习》 这本书的介绍吧!