随機存取的向量-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(, ); // 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[] = { ,,, };
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)<);
}
};
void ListMain()
{
// constructors used in the same order as described above:
std::list<int> first; // empty list of ints
std::list<int> second(, ); // 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[] = { , , , , ,
, , , , };
std::list<double> mylist(mydoubles, mydoubles + );
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 = ; i <= ; ++i) mylist1.push_back(i); // 1 2 3 4 5
it = mylist1.begin();
++it; // it points now to number 2 ^
mylist1.insert(it, ); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist1.insert(it, , ); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
std::vector<int> myvector(, );
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() //傳回隊列的元素個數