C++stl 向量,链表,栈,队列(vector, list, stack, queue)

时间:2021-09-28 17:42:55

随机存取的向量-vector

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

bool comp(const int &a, const int &b)
{
return a > b;
}

void VectorMain()
{
// constructors used in the same order as described above:
vector<int> first; // empty vector of ints
vector<int> second(4, 100); // four ints with value 100
vector<int> third(second.begin(), second.end()); // iterating through second
vector<int> fourth(third); // a copy of third

// the iterator constructor can also be used to construct from arrays:
int myints[] = { 16,2,77,29 };
vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));

cout << "The contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n';

// sort
sort(fifth.begin(), fifth.end());
cout << "The ascend contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n';

// descend
sort(fifth.begin(), fifth.end(), comp);
cout << "The descend contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n';

// push_back, pop_back, empty, clear
}

链表

#include <iostream>
#include <cmath>
#include <vector>
#include <list>
using namespace std;

// a binary predicate implemented as a function:
bool same_integral_part(double first, double second)
{
return (int(first) == int(second));
}

// a binary predicate implemented as a class:
struct is_near {
bool operator() (double first, double second)
{
return (fabs(first - second)<5.0);
}
};

void ListMain()
{
// constructors used in the same order as described above:
std::list<int> first; // empty list of ints
std::list<int> second(4, 100); // four ints with value 100
std::list<int> third(second.begin(), second.end()); // iterating through second
std::list<int> fourth(third); // a copy of third

double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
std::list<double> mylist(mydoubles, mydoubles + 10);

mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35

mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35

mylist.unique(same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0

mylist.unique(is_near()); // 2.72, 12.15, 72.25

std::cout << "mylist contains:";
for (std::list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

///////////////////////////insert/////////////////////////////
std::list<int> mylist1;
std::list<int>::iterator it;

// set some initial values:
for (int i = 1; i <= 5; ++i) mylist1.push_back(i); // 1 2 3 4 5

it = mylist1.begin();
++it; // it points now to number 2 ^

mylist1.insert(it, 10); // 1 10 2 3 4 5

// "it" still points to number 2 ^
mylist1.insert(it, 2, 20); // 1 10 20 20 2 3 4 5

--it; // it points now to the second 20 ^

std::vector<int> myvector(2, 30);
mylist1.insert(it, myvector.begin(), myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
std::cout << "mylist contains:";
for (it = mylist1.begin(); it != mylist1.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

// front(), back(), push_back(value), pop_back(), push_front(value), pop_front(), empty(), clear(), remove(value)
}

stack:

push(value) //向容器顶部插入元素
pop() //删除容器顶部的元素
top() //返回容器顶部的元素
size() //返回容器的元素个数
empty() //检查是否为空

queue:

back() //返回队列最后一个元素的引用
empty() //检查是否为空
front() //返回队列第一个元素的引用
push(value) //队列尾添加一个元素
pop() //删除队列第一个元素
size() //返回队列的元素个数