vector
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector> // vect:携带,or:表人,携带者在数学中有向量的意思
using namespace std;
void test01()
{
// 创建一个容器,名叫:vector
// < 放的是要操纵的数据类型哦~ >
// <> 后是给容器起的一个名字是
vector<int> v;
// 向容器中插入数据
v.push_back(10); // push:v.推,back:n.后面
v.push_back(20); // 作用:从后面插入一个数据
v.push_back(30);
v.push_back(40);
// 通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin(); // iter:重复,ator:做...工作的人或物
// vector<int>表示是在这个作用符下
// ::(作用符操作符)
// iterator(迭代器):这是一个数据类型,表示它是一个迭代器
// 后面写的是这个迭代器的名称
// v.begin() 表示它是一个起始迭代器,它指向容器中的第一个元素
// v.end() 表示它是一个结束迭代器,它指向容器中的最后一个元素的下一个位置
vector<int>::iterator itEnd = v.end();
// 遍历1
while (itBegin != itEnd)
{
printf("%d ", *itBegin);
++itBegin;
}
// 遍历2
for (vector<int>::iterator it = v.begin(); it < v.end(); ++it) {
printf("%d ", *it);
}
}
int main() {
test01();
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v({ 1, 2, 3 });
// 遍历3
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
cout << endl;
// 遍历4
for (int x : v) {
cout << x << ' ';
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// vector容器的数据存取
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; ++i)
{
v1.push_back(i);
}
// 利用[]的方式来访问vector中的元素
for (int i = 0; i < v1.size(); ++i)
{
cout << v1[i] << ' ';
}
cout << endl;
// 利用at()成员函数来访问vector中的元素
// 这个与[]的区别在于,它会判断是否越界
// 所以它 1.比[]慢一点 2.更安全
for (int i = 0; i < v1.size(); ++i)
{
cout << v1.at(i) << ' ';
}
cout << endl;
cout << "第一个元素为:" << v1.front() << endl; // front n.前面
cout << "最后一个元素为:" << v1.back() << endl; // back n.后面
}
int main()
{
test01();
return 0;
}
queue
#include <iostream>
#include <queue>
using namespace std;
class Person
{
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
};
int main()
{
// 创建队列容器
queue<Person> q;
Person p1("小白", 1);
Person p2("小黑", 2);
Person p3("小红", 3);
Person p4("小灰", 4);
q.push(p1); // 队列中只要写push
q.push(p2);
q.push(p3);
q.push(p4);
cout << "队列的大小为:" << q.size() << endl;
while (!q.empty()) // empty() e非,mp必须,ty表n. 不是必须的->n.空的东西
{
// 查看队头元素
cout << "队头元素——姓名" << q.front().name << " 年龄" << q.front().age << '\t';
// front n.前面
// 查看队尾元素
cout << "队尾元素——姓名" << q.back().name << " 年龄" << q.back().age << endl;
q.pop(); // 出队
}
cout << "队列的大小为:" << q.size() << endl;
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
// 创建stack容器
stack<int> s;
// 入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "size = " << s.size() << endl;
while (!s.empty()) {
printf("%d ", s.top());
s.pop();
}
cout << "\nsize = " << s.size() << endl;
return 0;
}
deque(double-ended queue),即“双端队列”
#include <iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>& d) // 在容器前加个const,使得容器只能读
{
for (deque<int>::const_iterator i = d.begin(); i < d.end(); ++i) {
printf("%d ", *i);
}
cout << '\n';
}
int main()
{
deque<int> d;
for (int i = 0; i < 10; ++i)
{
d.push_back(i);
}
printDeque(d);
// 新的容器(另一个容器的起始迭代器, 另一个容器的结束迭代器)
// 作用:将另一个容器当中的值 赋给 新的容器
deque<int> d2(d.begin(), d.end());
printDeque(d2);
deque<int> d3(10, 666); // (倍数, 数)
printDeque(d3);
deque<int> d4(d3);
printDeque(d4);
return 0;
}
#include <iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>& d) // 在容器前加个const,使得容器只能读
{
for (deque<int>::const_iterator i = d.begin(); i < d.end(); ++i) {
printf("%d ", *i);
}
cout << '\n';
}
int main()
{
deque<int> d1;
for (int i = 0; i < 10; ++i)
{
d1.push_back(i);
}
printDeque(d1);
if (!d1.empty()) {
printf("不为空,size = %d\n", d1.size());
}
else {
printf("为空");
}
// 重新指定大小
d1.resize(15, 36);
printDeque(d1);
return 0;
}
set
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>& s)
{
for (set<int>::iterator i = s.begin(); i != s.end(); ++i)
{
cout << *i << ' ';
}
cout << '\n';
}
int main()
{
set<int> s;
s.insert(90);
s.insert(80);
s.insert(40);
s.insert(50);
s.insert(70);
printSet(s);
// erase(e-出 + ras-擦 + e -> 擦出去)擦除
s.erase(s.begin()); // 删除迭代器指向的元素
printSet(s);
s.erase(90); // 删除指定的一个数字,如果没有也不会报错
printSet(s);
s.erase(s.begin(), s.end()); // 删除迭代器指向的范围中的元素
if (s.empty())
{
printf("空了\n");
}
s.insert(60);
printSet(s);
s.clear(); // clear a.清楚的,把一个容器变清楚就是将它清空
if (s.empty())
{
printf("空了\n");
}
return 0;
}
multiset
:multi(很多) + set(集合):多重集合,不同set
,它可以重复出现同一个元素。
#include <iostream>
#include <set> // set(集合)
using namespace std;
void printSet(const set<int>& s)
{
// set的迭代器为双向迭代器,不支持 < 这个比较操作符
for (set<int>::const_iterator i = s.begin(); i != s.end(); ++i) {
printf("%d ", *i);
}
}
void printMultiset(multiset<int>& ms) {
for (multiset<int>::iterator i = ms.begin(); i != ms.end(); ++i) {
printf("%d ", *i);
}
printf("\n");
}
int main()
{
set<int> s1;
pair<set<int>::iterator, bool> ret = s1.insert(10);
if (ret.second) {
printf("插入成功\n");
}
else {
printf("失败\n");
}
ret = s1.insert(10);
if (ret.second) {
printf("插入成功\n");
}
else {
printf("失败\n");
}
multiset<int> ms; // 创建一个多重集合容器
ms.insert(10);
ms.insert(10);
printMultiset(ms);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>& s)
{
for (set<int>::iterator i = s.begin(); i != s.end(); ++i)
{
cout << *i << ' ';
}
cout << '\n';
}
int main()
{
set<int> s;
s.insert(90);
s.insert(80);
s.insert(40);
s.insert(50);
s.insert(70);
printf("%d\n", s.count(550));
return 0;
}
lower_bound upper_bound
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
s.insert(30);
s.insert(80);
s.insert(40);
s.insert(50);
s.insert(70);
// lower(low a.低的 + er 表比较 下层的),bound(边界):下界
// lower_bound:找到 >=那个元素 的最小的元素的迭代器
set<int>::iterator ret1 = s.lower_bound(40);
// upper(up 向上 + er 表比较 上面的),bound(边界):上界
// upper_bound:找到 >那个元素 的最小元素的迭代器
set<int>::iterator ret2 = s.upper_bound(30);
printf("%d\n", *ret1);
printf("%d\n", *ret2);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <set>
using namespace std;
int main()
{
// pair(n.一对) 对组的创建
// 第一种方式
pair<string, int> p("Jack", 18);
// 这两个是属性,所以不用加小括号
cout << p.first << endl; // first a.第一的
cout << p.second << endl; // second a.第二的
// 第二种方式
pair<string, int> p2 = make_pair("Tom", 88);
cout << p2.first << endl;
printf("%d\n", p2.second);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <set>
#include <map>
using namespace std;
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator i = m.begin(); i != m.end(); ++i)
{
printf("key = %d\tvalue = %d\n", (*i).first, i->second);
}
}
int main()
{
// map(地图),<>里放的是模版参数
// 第一个参数为键值,第二个参数为实值
map<int, int> m;
m.insert(pair<int, int>(1, 10)); // 这是一个匿名的对组
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
printMap(m);
return 0;
}