STL容器
近容器
1、int arr[ ] 2、string 下面的程序只是对string这个容器内部一些函数的使用作以举例,全部运行并没有意义。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("abcdefg");
string s1(s);
string s2(s1.begin(), s1.end());
cout << s2 << endl;
for (int i = 0; i < 10; i++)
{
s.push_back('a' + i);
}
s.pop_back();
for (int i = 0; i < s.size(); i++)
{
if (s[i] == 'b')
{
s[i] = 'y';
}
cout << s[i] << " ";
}
cout << endl;
cout << typeid(s.c_str()).name() << endl;
string::iterator it = s.begin();
for (; it != s.end(); it++)
{
if (*it == 'a')
{
it = s.erase(it);
}
cout << *it << " ";
}
cout << endl;
s.begin();
s.front();
s.empty();
int pos = s.find("def");
cout << pos << endl;
char err[10] = { "abcdefg" };
char* p = err;
s.copy(p, 3);
cout << s << endl;
cout << err << endl;
s.reserve();
s.resize(20);
s.swap(s1);
顺序容器
1、vertor 一维数组 按照1.5倍扩容
#include <vector>;
template<typename CON>
void Show(CON& con)
{
typename CON::iterator it = con.begin();
for (; it != con.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> v;
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v.push_back(1 + i);
}
v.pop_back();
cout << v.back() << endl;
cout << v.front() << endl;
cout << v.size()<< endl;
cout << v.max_size() << endl;
v.swap(v1);
v.clear();
v.empty();
v.resize(10);
v.reserve(10);
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
vector<int>::iterator it = v1.begin();
for (; it != v1.end(); it++)
{
if (*it == 5)
{
}
cout << *it << " ";
}
cout << endl;
Show(v1);
string s1("abcdefg");
Show(s1);
copy(v1.begin(),v1.end(), ostream_iterator<int>(cout," "));
}
容器可以通过迭代器进行构造:
int main()
{
list<int>L;
for (int i = 0; i < 10; i++)
{
L.push_back(i+1);
}
vector<int>v1(L.begin(), L.end());
Show(v1);
return 0;
}
2、list 链表
#include <list>
int main()
{
list<int>L;
for (int i = 0; i < 10; i++)
{
L.push_back(i);
}
L.pop_back();
Show(L);
vector<int>v1;
vector<int>v1(L.begin(), L.end());
Show(v1);
L.push_front(3);
L.pop_front();
L.resize(20);
L.reverse();
}
3、deque 通过双端队列实现的二维数组
容器适配器
1、stack ——— deque
#include <stack>
int main()
{
stack<int> s;
for (int i = 0; i < 10; i++)
{
s.push(i);
}
s.pop();
s.top();
s.empty();
s.size();
return 0;
}
2、queue ——— deque
#include <queue>;
int main()
{
queue<int> q;
for (int i = 0; i < 10; i++)
{
q.push(i);
}
q.pop();
q.front();
q.size();
q.empty();
}
关联容器
1、set 是一个集合,集合内部的数据是有序排列,底层是用红黑树实现,set集合不允许数据重复
#include <set>;
int main()
{
set<int> s;
s.insert(56);
s.insert(12);
s.insert(39);
s.insert(6);
s.insert(999);
s.insert(25);
Show(s);
set<int>::iterator it = s.find(99);
if (it != s.end())
{
cout << *it << endl;
}
else
{
cout << "don`t find" << endl;
}
cout << s.count(78) << endl;
s.empty();
s.size();
}
例题: 假如下面不同的数字代表不同类型的糖果,小明有这样一盒糖,但是他一天只能吃这些糖果的一半,问:小明最多能吃到多少类型的糖果? 2 3 3 4 3 4 2 3 2 传入的参数是:vector v 只有两种比较的可能: ①如果总的长度为n,一半就是n/2 ②type(糖果总的类型) 比较type 和n/2 如果type大,能吃到最多的种类就是n/2 如果n/2大,能吃到最多的种类就是type 如何找type——将其放进set里面去 return min(set(v.begin(),v.end()).size(),v.size/2)
2、multiset 多重集合,与set的区别在于允许数据重复
#include <set>;
int main()
{
multiset<int> ms;
ms.insert(56);
ms.insert(98);
ms.insert(34);
ms.insert(21);
ms.insert(158);
ms.insert(999);
ms.insert(5);
ms.insert(84);
ms.insert(84);
ms.insert(84);
Show(ms);
multiset<int>::iterator it = ms.find(84);
if (it != ms.end())
{
for (int i = 0; i < ms.count(87); i++)
{
cout << *it << endl;
it++;
}
}
else
{
cout << "don`t find" << endl;
}
}
3、map 是一个映射表,数据按key有序,底层也是通过红黑树实现,不允许出现重复映射
#include <map>;
int main()
{
map<int,string>mm;
mm.insert(make_pair(1, "qqqq"));
mm.insert(make_pair(2, "wwww"));
mm.insert(make_pair(4, "aaaaa"));
mm.insert(make_pair(3, "abcde"));
mm.insert(pair<int,string>(5, "ddd"));
map<int, string>::iterator it = mm.begin();
for (; it != mm.end(); it++)
{
cout <<it-> first << "---->";
cout << it->second << endl;
}
map<int, string>::iterator it = mm.find(5);
if (it != mm.end())
{
cout << it->first << "---->";
cout << it->second << endl;
}
mm.empty();
mm.erase(5);
mm.count(5);
return 0;
}
4、multimap 多重映射表,与map的区别在于允许重复
#include <map>;
int main()
{
map<int,string>mm;
mm.insert(make_pair(1, "qqqq"));
mm.insert(make_pair(2, "wwww"));
mm.insert(make_pair(4, "aaaaa"));
mm.insert(make_pair(3, "abcde"));
mm.insert(pair<int,string>(5, "ddd"));
map<int, string>::iterator it = mm.begin();
for (; it != mm.end(); it++)
{
cout <<it-> first << "---->";
cout << it->second << endl;
}
map<int, string>::iterator it = mm.find(5);
if (it != mm.end())
{
cout << it->first << "---->";
cout << it->second << endl;
}
mm.empty();
mm.erase(5);
mm.count(5);
multimap<int, string>mmm(mm.begin(),mm.end());
mmm.insert(make_pair(1, "qqqq"));
mmm.insert(make_pair(2, "wwww"));
mmm.insert(make_pair(4, "aaaaa"));
mmm.insert(make_pair(3, "abcde"));
multimap<int, string>::iterator it2 = mmm.begin();
for (; it != mmm.end(); it++)
{
cout << it2->first << "--->";
cout << it2->second << endl;
}
return 0;
}
|