内容简介:【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
(美)戈雷利克、(英)欧日沃尔德 / 东南大学出版社 / 2015-2
你的Python代码也许运行正确,但是你需要运行得更快速。通过探讨隐藏在设计备选方案中的基础理论,戈雷利克和欧日沃尔德编著的《高性能Python》将帮助你更深入地理解Python的实现。你将了解如何定位性能瓶颈,从而显著提升高数据流量程序中的代码执行效率。 你该如何利用多核架构和集群?或者你该如何搭建一个可以自由伸缩而不会影响可靠性的系统?有经验的Python程序员将会学习到这类问题的具体解......一起来看看 《高性能Python》 这本书的介绍吧!
html转js在线工具
html转js在线工具
RGB CMYK 转换工具
RGB CMYK 互转工具