set基本使用
增删查改
set<int> s;
s.insert(1);
s.insert(3);
s.insert(2);
s.insert(1);
s.insert(4);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
set<int> c = s;
auto pos = c.find(3);
c.erase(pos);
for (auto e : c)
{
cout << e << " ";
}
set会自动排序去重,因为底层是搜索树,中序遍历是有序的
map的基本使用
void test_map()
{
map<int, int> m;
m.insert(pair<int, int>(1, 1));
m.insert(pair<int, int>(1, 1));
m.insert(make_pair(2, 2));
m.insert(make_pair(3, 3));
map<int, int>::iterator it = m.begin();
while (it != m.end())
{
cout << it->first << " " << it->second;
cout << endl;
++it;
}
cout << endl;
for (auto e : m)
{
cout << e.first << " " << e.second;
cout << endl;
}
}
////计数
void test_map3()
{
map<string, int> m;
string str[] = { "西瓜", "西瓜", "西瓜", "香蕉", "草莓", "草莓", "西瓜", "西瓜", "西瓜", "西瓜", };
//map<string, int> m;
/*for (auto e : str)
{
//map<string, int> m;
map<string, int>::iterator it = m.find(e);
if (it != m.end())
{
it->second++;
}
else
{
m.insert(make_pair(e, 1));
}
}*/
for (auto e : str)
{
m[e]++;
}
for (auto e : m)
{
cout << e.first << " " << e.second << endl;
}
}
//