C++强大的原因之一是得益于他强大的标准模板库
标准模板库 简称STL (Standard Template Library)
STL为程序员提供了强大的功能,例如数组操作:数组的计算,数组的大小,数组的排序,数组的插入等等。
vector
包含在头文件<vector>中
用<type_name>指出要实例化的类型,其中[]被重载,可像访问数组中的元素那样访问vector。他的构造函数可指出初始化长度。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const int n = 10;
//initialized length = 10
vector<int> vec (n);
for(int i=0;i<n;i++){
cin >> vec[i];
}
for(int i=0;i<n;i++){
cout << vec[i] << " ";
}
cout << "\nSize is " << vec.capacity();
return 0;
}
运行结果
可对vector执行的操作
cout << "\nSize is " << vec.size();
交换两个容器内的内容
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const int n = 3;
//initialized length = 10
vector<int> vec1 (n);
vector<int> vec2 (n);
for(int i=0;i<n;i++){
vec1[i] = i;
}
for(int i=0;i<n;i++){
vec2[i] = 2-i;
}
cout << "交换前\n";
for(int i=0;i<n;i++){
cout << vec1[i] << " ";
}
cout << endl;
for(int i=0;i<n;i++){
cout << vec2[i] << " ";
}
cout << "\n交换后\n";
swap(vec1,vec2);
for(int i=0;i<n;i++){
cout << vec1[i] << " ";
}
cout << endl;
for(int i=0;i<n;i++){
cout << vec2[i] << " ";
}
return 0;
}
运行结构
可以用迭代器代替下表历遍容器,代码如下
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
const int n = 10;
//initialized length = 10
vector<int> vec (n);
vector<int>::iterator pvec;
for(int i=0;i<n;i++){
vec[i] = rand() % 100;
}
for(pvec = vec.begin();pvec<vec.end();pvec++){
cout << *pvec << " ";
}
return 0;
}
运行结果
接着来说vector的类方法
push_back()——将元素添加到容器末尾,内存不够时自动开辟。
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
const int n = 10;
//initialized length = 10
vector<int> vec (n);
vector<int>::iterator pvec;
cout << "Before Size is " << vec.size() << endl;
for(int i=0;i<n;i++){
vec[i] = rand() % 100;
}
//参数为右值引用的重载版本
vec.push_back(155);
cout << "Use push_back after Size is " << vec.size() << endl;
for(pvec = vec.begin();pvec<vec.end();pvec++){
cout << *pvec << " ";
}
return 0;
}
运行结果
erase()——删除给定区间中的元素,它接受两个迭代器作为参数。起一个迭代器指向删除元素的启示位置,并且删除,第二个迭代器参数指向区间末尾,并且不删除。即删除[a, b)
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
const int n = 10;
vector<int> vec (n);
vector<int>::iterator pvec;
for(int i=0;i<n;i++){
vec[i] = i;
}
vec.erase(vec.begin()+1,vec.begin()+3); //delete 1,2 no 3
for(pvec = vec.begin();pvec<vec.end();pvec++){
cout << *pvec << " ";
}
return 0;
}
insetr()——将一个容器中的区间插入到另一个容器中的指定位置。他接受三个迭代器参数。
第一个迭代器指向被插入容器的位置。后两个迭代器表示源容器的插入区间 [a, b)
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
vector <int> old_vec (10,5);
vector <int> new_vec (10,6);
for(int i=0;i<10;i++){
old_vec[i] = i ;
}
new_vec.insert(new_vec.begin()+3,old_vec.begin()+4,old_vec.begin()+6);
auto pv = new_vec.begin(); //C++11
for(; pv < new_vec.end(); pv++){
cout << *pv << " ";
}
return 0;
}
运行结果
通用的STL函数
for_each()——可代替for循环的函数(<algorithm>)。接受三个参数,前两个迭代器指向容器中的元素,代表一个区间,最后一个参数是一个指向函数的指针。其作用是将区间中的元素执行函数指针指向函数的操作。并且不能修改容器中的值。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(int & a)
{
cout << "这个元素是" << a << endl;
}
int main()
{
const int n = 10;
void (*pfunc) (int&) = show;
vector <int> vec (n,6);
for(int i=0;i<n;i++)
{
vec[i] = i;
}
auto pvb = vec.begin(); //vector<int>::iterator pvb = vec.begin();
auto pve = vec.end();
for_each(pvb, pve, pfunc);
return 0;
}
运行结果
random_shuffle()接受两个迭代器指向容器的区间,并对其随机排列。
random_shuffle(pvb,pve);
运行结果
sotr()——该函数有两个重载版本,其中一个版本接受两个指向容器元素的迭代器,代表一个区间,对其进行升序排序(使用模板类自带的重载后的 < ),如果是用户自定义的 数据类型,则需要重载 < 。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(int & a)
{
cout << a << " ";
}
int main()
{
const int n = 10;
void (*pfunc) (int&) = show;
vector <int> vec (n,6);
for(int i=0;i<n;i++)
{
vec[i] = 10 - i;
}
auto pvb = vec.begin(); //vector<int>::iterator pvb = vec.begin();
auto pve = vec.end();
random_shuffle(pvb,pve);
for_each(pvb, pve, pfunc);
sort(pvb,pve);
cout << endl;
for_each(pvb, pve, pfunc);
return 0;
}
另一个重载版本则:前两个参数不变,后一个参数接受一个函数指针(返回值为bool类型),true和false表示调用函数的两个参数顺序是否正确。如果不正确则变为正确的顺序。这样做就可以变为升序排序。
bool compare(int &a, int &b)
{
return a > b;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(int & a)
{
cout << a << " ";
}
bool compare(int &a, int &b)
{
return a > b;
}
int main()
{
const int n = 10;
void (*pfunc) (int&) = show;
vector <int> vec (n,6);
for(int i=0;i<n;i++)
{
vec[i] = 10 - i;
}
auto pvb = vec.begin(); //vector<int>::iterator pvb = vec.begin();
auto pve = vec.end();
random_shuffle(pvb,pve);
for_each(pvb, pve, pfunc);
sort(pvb,pve, compare);
cout << endl;
for_each(pvb, pve, pfunc);
return 0;
}
类似的其他类型也可以使用以上函数,例如string,char,double。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(double & a)
{
cout << a << " ";
}
bool compare(double &a, double &b)
{
return a > b;
}
int main()
{
const int n = 10;
void (*pfunc) (double&) = show;
vector <double> vec (n,6);
for(int i=0;i<n;i++)
{
vec[i] = 10 - i + i * 3.14;
}
auto pvb = vec.begin(); //vector<int>::iterator pvb = vec.begin();
auto pve = vec.end();
random_shuffle(pvb,pve);
for_each(pvb, pve, pfunc);
sort(pvb,pve, compare);
cout << endl;
for_each(pvb, pve, pfunc);
return 0;
}
运行结果
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(string & a)
{
cout << a << endl;
}
int main()
{
const int n = 5;
void (*pfunc) (string&) = show;
vector <string> vec (n);
vec[0] = "begin";
vec[1] = "hello";
vec[2] = "world";
vec[3] = "C++";
vec[4] = "STL use";
auto pvb = vec.begin(); //vector<int>::iterator pvb = vec.begin();
auto pve = vec.end();
random_shuffle(pvb,pve);
for_each(pvb, pve, pfunc);
sort(pvb,pve);
cout << endl;
for_each(pvb, pve, pfunc);
return 0;
}
运行结果
也可以使用这样的for循环,就像使用int,double的那样。
#include <iostream>
using namespace std;
int main()
{
int num[10] = {1,2,3,5,4,6,9,8,7,0};
for(int a : num){
cout << a << " ";
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const int n = 5;
vector <string> vec (n);
vec[0] = "begin";
vec[1] = "hello";
vec[2] = "world";
vec[3] = "C++";
vec[4] = "STL use";
for(auto & x : vec){
cout << x << endl;
}
return 0;
}
运行结果