目录
关联式容器
键值对
//键值对
template <class T1, class T2>
struct pair
{
T1 first;//key
T2 second;//value
//键值对构造函数
pair()
:first(T1())
, second(T2())
{}
//键值对拷贝构造函数
pair(const T1& a, const T2& b)
:first(a)
, second(b)
{}
};
set简介
set官方文档:set - C++ Reference
set的常用接口
构造函数
//无参构造: set<Type> s1
set();
//迭代器区间构造[begin,end): set<Type> s2(s1.begin,s1.end)
template <class InputIterator>
set(InputIterator first, InputIterator last);
//拷贝构造: set<Type> s3(s1)
set(const set& x);
set的迭代器
set的容量
修改相关接口
find()函数
insert()函数
pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
int main()
{
set<int> s1;
s1.insert(5);
s1.insert(4);
s1.insert(3);
s1.insert(6);
s1.insert(7);
s1.insert(1);
s1.insert(5);
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
set<int>::iterator pos = s1.find(7);
s1.insert(pos, 8);// set容器中的pos位置插入元素8
set<int>::iterator it1 = s1.begin();
while (it1 != s1.end())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
return 0;
}
运行结果:
erase()函数
void erase (iterator position); //配合find()函数使用
//删除set容器中pos位置的元素
size_t erase (const value_type& val);
//删除set容器中值为val的元素,返回被删除元素的个数
//(由于set容器中的value唯一,若该元素存在,则返回1,否则返回0)
int main()
{
set<int> s1;
s1.insert(5);
s1.insert(4);
s1.insert(3);
s1.insert(6);
s1.insert(7);
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//erase()版本1
set<int>::iterator pos = s1.find(3);
if (pos != s1.end())
{
cout << "查找到元素val,删除val:" << endl;
s1.erase(pos);
}
for (auto& e : s1)
{
cout << e <<" ";
}
cout << endl;
//erase版本2
//删除的元素存在,返回1
size_t ret1 = s1.erase(5);
cout << "ret1=" << ret1 << endl;
//删除的元素不存在不做任何处理,返回0
size_t ret2 = s1.erase(30);
cout << "ret2=" << ret2 << endl;
return 0;
}
运行结果:
clear()函数
int main()
{
set<int> s1;
s1.insert(5);
s1.insert(4);
s1.insert(3);
s1.insert(6);
s1.insert(7);
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
cout << "size=" << s1.size() << endl;
cout << s1.empty() << endl;
s1.clear();
cout << "size=" << s1.size() << endl;
cout << s1.empty() << endl;
}
运行结果:
count函数
int main()
{
set<int> s1;
s1.insert(5);
s1.insert(4);
s1.insert(3);
s1.insert(6);
s1.insert(7);
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//使用count查找set容器中查找值为val的元素,返回值为val的元素个数
//(val在set容器中,返回1; val不在set容器中,返回0;)
if (s1.count(5))
{
cout << "在" << endl;
}
else
{
cout << "不在" << endl;
}
return 0;
}
运行结果:
lower_bound()函数
upper_bound()函数
int main()
{
set<int> s1;
s1.insert(7);
s1.insert(3);
s1.insert(9);
s1.insert(1);
s1.insert(4);
s1.insert(6);
s1.insert(10);
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// >= val
set<int>::iterator itlow = s1.lower_bound(2);
cout << *itlow << endl;
// > val
set<int>::iterator itup = s1.upper_bound(7);
cout << *itup << endl;
//遍历[itlow,itup)区间范围内的元素
while (itlow != itup)
{
cout << *itlow << " ";
++itlow;
}
cout << endl;
return 0;
}
运行结果:
multiset简介
int main()
{
// 排序
multiset<int> s;
s.insert(5);
s.insert(1);
s.insert(6);
s.insert(3);
s.insert(4);
s.insert(1);
s.insert(1);
s.insert(5);
s.insert(2);
s.insert(10);
multiset<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//multiset容器count函数返回值不再非1即0
cout << "cout1=" << s.count(1) << endl;
cout << "cout2=" << s.count(5) << endl;
//find()函数返回中序遍历multiset容器中第一个值为val元素的迭代器
it = s.find(5);
while (it != s.end() && *it == 5)
{
cout << *it << " ";
++it;
}
cout << endl;
return 0;
}
运行结果:
multiset官方文档:multiset - C++ Reference
map简介
typedef pair<const key, T> value_type;
map官方文档:map - C++ Reference
map的常用接口
构造函数
//无参构造: map<Type1,Type2> m1
map ();
//迭代器区间构造: map<Type1,Type2> m2(m1.begin(),m1.end())
template <class InputIterator>
map (InputIterator first, InputIterator last);
//拷贝构造函数: map<Type1,Type2> m3(m1);
map (const map& x);
map的迭代器
map的容量
修改相关接口
insert()函数
pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
1. 有名对象构造pair
int main()
{
map<string, string> dict;
pair<string, string> kv("courage", "勇气");
dict.insert(kv);
return 0;
}
2. 匿名对象构造pair
int main()
{
map<string, string> dict;
dict.insert(pair<string, string>("insistence", "坚持"));
return 0;
}
3. make_pair()函数构造pair
//pair<key,value> pair中的第一个元素key被设置为x,pair中的第二个元素value被设置为y
template <class T1,class T2>
pair<T1,T2> make_pair (T1 x, T2 y)
{
return ( pair<T1,T2>(x,y) );
}
int main()
{
map<string, string> dict;
dict.insert(make_pair("endeavor", "努力"));
return 0;
}
4.{ }构造
int main()
{
map<string, string> dict;
//原因:c++11支持多参数隐式类型转换(构造函数)
dict.insert({ "integrity", "正直" });
return 0;
}
示例:
int main()
{
map<string, string> dict;
dict.insert(make_pair("learn", "学习"));
dict.insert(make_pair("courage", "勇气"));
dict.insert(make_pair("insistence", "坚持"));
dict.insert(make_pair("integrity", "正直"));
dict.insert(make_pair("endeavor", "努力"));
dict.insert(make_pair("learn", "推断"));
// 迭代器遍历
map<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
//cout << *it << endl;(×)(it为结构体指针,*it为pair)
// 访问方式一(√)
//cout << (*it).first << ":" << (*it).second << endl;
//访问方式二(√)
cout << it->first << ":" << it->second << endl;
++it;
}
return 0;
}
运行结果:
元素访问operator[ ]
//operator[]等价形式如下:
(*((this->insert(make_pair(k,mapped_type()))).first)).second
//模拟重载如下:
value& operator[](const key& k)
{
pair<iterator, bool> ret = insert(make_pair(k, value()));
return *(ret.first).second
}
示例
int main()
{
map<string, string> dict;
dict.insert(make_pair("learn", "学习"));
dict.insert(make_pair("courage", "勇气"));
dict.insert(make_pair("insistence", "坚持"));
dict.insert(make_pair("integrity", "正直"));
dict.insert(make_pair("endeavor", "努力"));
dict.insert(make_pair("learn", "推断"));
map<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
cout << (*it).first << ":" << (*it).second << endl;
++it;
}
cout << endl;
//[]功能一: 插入
dict["sort"];
//[]功能二: 查找
cout << dict["insistence"] << endl;
//[]功能三: 修改
dict["learn"] = "推断";
//[]功能四: 插入+修改
dict["charming"] = "迷人的";
for (auto& kv : dict)//范围for遍历时加上引用,减少拷贝,提升效率
{
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
return 0;
}
运行结果:
multimap简介
int main()
{
multimap<string, string> dict;
dict.insert(make_pair("learn", "学习"));
dict.insert(make_pair("courage", "勇气"));
dict.insert(make_pair("insistence", "坚持"));
dict.insert(make_pair("integrity", "正直"));
dict.insert(make_pair("endeavor", "努力"));
dict.insert(make_pair("learn", "推断"));
// 迭代器遍历
multimap<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
cout << it->first << ":" << it->second << endl;
++it;
}
return 0;
}
运行结果: