细说C++常用容器之vector

vector简介

	vector,又称为向量类型,是C++ STL的一个重要成员。它可以容纳许多类型的数据,因此称它为容器,可以看做是一个能够存储任意类型的动态数组。

vector常用操作

1.vector的初始化
  1. 使用push_back()插入数据
   vector<int> a; //声明向量a
   cout << "1.使用push_back()插入数据" << endl;
   for (int i = 0; i < 10; i++) {
   	a.push_back(i);
   }
   cout << "value of a:" << endl;
   printVector(a);
   cout << "size of a:" << a.size() << endl;
   cout << "=====================\n";
输出:
1.使用push_back()插入数据
value of a:
0 1 2 3 4 5 6 7 8 9 
size of a:10
  1. vector a1(10);//声明大小为10个整型元素的向量
	vector<int> a1(10); //声明大小为10的整型向量
	cout << "2.声明长度后使用push_back()插入数据" << endl;
	for (int i = 0; i < 10; i++) {
		a1.push_back(i);
	}
	cout << "value of a1:" << endl;
	printVector(a1);
	cout << "size of a1:" << a1.size() << endl;
	cout << "=====================\n";

输出:

2.声明长度后使用push_back()插入数据
value of a1:
0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 
size of a1:20
  1. 声明长度后使用下标插入数据
	vector<int> a2(10); //声明10个整型元素的向量
	cout << "3.声明长度后使用下标插入数据" << endl;
	for (int i = 0; i < 10; i++) {
		a2[i] = i;
	}
	cout << "value of a2:" << endl;
	printVector(a2);
	cout << "size of a2:" << a2.size() << endl;
	cout << "=====================\n";

输出:

3.声明长度后使用下标插入数据
value of a2:
0 1 2 3 4 5 6 7 8 9 
size of a2:10
  1. 定义向量的同时赋初值
	vector<int> a3(10, 1); //声明大小为10的整型向量,且初值为1
	cout << "4.定义向量的同时赋初值" << endl;
	cout << "value of a3:" << endl;
	printVector(a3);
	cout << "size of a3:" << a3.size() << endl;
	cout << "=====================\n";

输出:

4.定义向量的同时赋初值
value of a3:
1 1 1 1 1 1 1 1 1 1 
size of a3:10
  1. 使用向量来创建向量
	vector<int> b(10, 6); //声明大小为10的整型元素向量b,初值为6
	vector<int> a4(b); //用向量b创建向量a,整体复制
	cout << "5.使用向量来创建向量" << endl;
	cout << "value of a4:" << endl;
	printVector(a4);
	cout << "size of a4:" << a4.size() << endl;
	cout << "=====================\n";

输出:

5.使用向量来创建向量
value of a4:
6 6 6 6 6 6 6 6 6 6 
size of a4:10
  1. 使用一个向量中的值给另一个向量赋值
	vector<int> c;
	for (int i = 10; i < 15; i++) {
		c.push_back(i); //10,11,12,13,14
	}
	vector<int> a5(c.begin(), c.begin() + 3); //定义a5的值为c中的第0个到第2个,共三个
	cout << "6.使用一个向量中的值给另一个向量赋值" << endl;
	cout << "value of a5:" << endl;
	printVector(a5);
	cout << "size of a5:" << a5.size() << endl;
	cout << "=====================\n";

输出:

6.使用一个向量中的值给另一个向量赋值
value of a5:
10 11 12 
size of a5:3
  1. 从数组中获得初值
	int d[5] = { 1, 2, 3, 4, 5 };
	vector<int> a6(d, d + 5);
	cout << "7.从数组中获得初值" << endl;
	cout << "value of a6:" << endl;
	printVector(a6);
	cout << "size of a6:" << a6.size() << endl;
	cout << "=====================\n";

输出:

7.从数组中获得初值
value of a6:
1 2 3 4 5 
size of a6:5

注意: 下面的代码是正确的`

	vector<int> a(10);
	for(int i = 0; i< 10; i++){
		a1[i] = i;//正确
	}

但是,这段代码是错误的

	vector<int> a;
	for(int i = 0; i< 10; i++){
		a[i] = i;//错误,下标只能用于获取已存在的元素,而现在的a[i]还是空的对象
	}

如果你使用push_back()给vector赋值,但在此之前,已经使用过vector a(10)声明了一个vector,结果并不是你想要的那样,此时的vector长度是10+push_back进来的总长度,原因是push_back()是在a的最后一个元素后插入一个新元素。

2.遍历vector的方法

(1)使用下标

void printVector(vector<int> v) {
	if (!v.empty()) {
		for (int i = 0; i < v.size(); i++) {
			cout << v[i] << " ";
		}
	} else {
		cout << "vector is empty!" ;
	}
	cout << "\n";
}

(2)使用迭代器

void printVectorWithIterator(vector<int> v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << "\n";
}
3.vector的常用方法

(1)获取vector最后一个元素

	cout << "the last of a :" << a.back() << endl;

(2)获取vector第一个元素

	cout << "the first of a :" << a.front() << endl;

(3)返回a的第i个元素

for (int i = 0; i < a.size(); i++) {
		cout << a[i] << " "; //a[i],返回a的第i个元素,当且仅当a[i]存在时
	}

(4)清空vector

	a.clear(); //清空a中的元素
	cout << "after clear:";
	printVector(a);

(5)判断vector是否为空

	cout << "vector a is empty?" << boolalpha << a.empty() << endl;
	cout << "vector a1 is empty?" << boolalpha << a1.empty() << endl;

(6)删除vector的最后一个元素

	cout << "before pop_back() a2:";
	printVector(a2);
	a2.pop_back(); //删除最后一个元素
	cout << "after pop_back() a2:";
	printVector(a2);

(7)erase()

	a6.erase(a6.begin(), a6.begin() + 2); //删除a6中的第0个(从第0个算起)至第1个元素,[0,2)
	cout << "after erase:" << endl;
	printVector(a6); //3,4,5

(8)push_back()

	cout << "before push_back():";
	printVector(a6);
	a6.push_back(6); //在最后一个向量后插入元素
	cout << "after push_back():";
	printVector(a6);

(9)insert(position,value)

	cout << "before insert:";
	printVector(a6);
	a6.insert(a6.begin() + 1, 6); //在a6的第一个元素位置处插入6。如3,4,5,6,则插入后为3,6,4,5,6
	cout << "after insert:";
	printVector(a6);

(10)insert(position,count,value)

	cout << "before insert:";
	a6.erase(a6.begin() + 1, a6.begin() + 2);
	printVector(a6); //3,4,5,6
	a6.insert(a6.begin() + 1, 3, 7); //在a6的第1个位置插入3个7
	cout << "after insert:";
	printVector(a6); //3,7,7,7,4,5,6

(11)insert(a6.begin(), e + 2, e + 5);//e为数组,在a6的第0个元素的位置插入e的第2个元素到第4个元素,不包括e+5。

	cout << "before insert:";
	a6.erase(a6.begin() + 1, a6.begin() + 4);
	printVector(a6); //3,4,5,6
	//数组
	int e[6] = { 10, 11, 12, 13, 14, 15 };
	a6.insert(a6.begin(), e + 2, e + 5);	//[2,5),插入12,13,14
	cout << "after insert:";
	printVector(a6);	//12,13,14,3,4,5,6

(12)size();//返回vector中元素的个数;

	cout << "size of a6:" << a6.size() << endl;	//vector中元素的个数

(13)capacity();//返回vector在内存中总共可以容纳的元素个数

	cout << "capacity of a6 :" << a6.capacity() << endl;
	cout << "capacity of a5 :" << a5.capacity() << endl;
	cout << "capacity of a4 :" << a4.capacity() << endl;
	cout << "capacity of a3 :" << a3.capacity() << endl;
	cout << "capacity of a2 :" << a2.capacity() << endl;
	cout << "capacity of a1 :" << a1.capacity() << endl;
	cout << "capacity of a :" << a.capacity() << endl;

(14)resize();//将vector的现有元素个数调至新的大小,多则删,少则补

	cout << "before resize a1:" << endl;
	printVector(a1);
//	a1.resize(6, 2);	//将a的现有元素个数调至6个,多则删
//	cout << "多则删:" << endl;
	a1.resize(25, 6);	//少则补,补5个6
	cout << "少则补:" << endl;
	printVector(a1);

注意,resize(10)与resize(10,2)不同的地方在于,前者调整大小之后的值是随机的,而后者的值是固定的2(也就是说补的值均为2)。
(15)reserve();//容量扩充

	vector<int> a7(10);
	cout << "before reserve:" << a7.capacity() << endl;
	a7.reserve(100);
	cout << "after reserve:" << a7.capacity() << endl;

注意,vector中这两个属性capacity和size很容易弄混淆。size是当前vector容器真实占用的大小,也就是容器当前拥有多少个容器;capacity是指在发生realloc前能允许的最大元素数,即预分配的内存空间。针对capacity这个属性,STL中的其他容器,如list map set deque,由于这些容器的内存是散列分布的,因此不会发生类似realloc()的调用情况,我们可以认为capacity属性针对这些容器是没有意义的,所以在设计时这些容器没有这两个属性。在STL中,拥有capacity属性的容器只有vector和string。当然,这两个属性分别对应两个方法:resize()和reserve()。
使用resize(),容器内的对象内存空间是真正存在的。
使用reserve()仅仅只是修改了capacity的值,容器内的对象并没有真实的内存空间(空间是"野"的)。此时切记使用[]操作符访问容器内的对象,很可能出现数组越界的问题。
(16)swap();//交换两个vector

	cout << "before swap:" << endl;
	cout << "value of a5:";
	printVector(a5);
	cout << "value of a6:";
	printVector(a6);
	a5.swap(a6);	//交换a6,a5的值
	cout << "after swap:" << endl;
	cout << "value of a5:";
	printVector(a5);
	cout << "value of a6:";
	printVector(a6);

(17)向量的比较

	cout << "a5==a6?"<< (a5 == a6) << endl;//a5==a6?false
	int f[7] = {12,13,14,3,4,5,6};
	vector<int> a8(f,f+7);
	cout << "a5==a8?" << (a5 == a8) << endl;//a5==a8?true
	cout << "capacity of a5 :" << a5.capacity() << endl;//capacity of a5 :8
	cout << "capacity of a8 :" << a8.capacity() << endl;//capacity of a8 :7
	cout << "size of a5 :" << a5.size() << endl;//size of a5 :7
	cout << "size of a8 :" << a8.size() << endl;//size of a8 :7

向量的比较操作还有!=,>=,<=,>,<。从上面的结果可以看出,两个vector相等,它们的元素相同,capacity不一定相同,size一定相同。

4.vector其他用法

(1)从向量中选择元素添加

	int n[6] = {1,2,3,4,5,6};
	vector<int> aa;
	vector<int> cc(n,n+4);//1,2,3,4
	cout << "value of cc:";
	printVector(cc);
	for(vector<int>::iterator it = cc.begin(); it < cc.end(); it++){
		aa.push_back(*it);
	}
	cout << "value of aa:";
	printVector(aa);

(2)从文件中读取元素添加到向量
ps:代码中的文件路径使用相对路径,因此文件位置一定要放对。如不知道该放在哪里,可以使用getcwd.cpp中的方法查看当前工作目录。当然,你也可以使用绝对路径。

	ifstream in("data.txt");
	vector<int> bb;
	for(int i;in>>i;){
		bb.push_back(i);
	}
	cout << "read from file:";
	printVector(bb);
5.vector相关的几个重要算法

头文件:#include<algorithm>
(1)sort(v.begin(),v.end());//升序排列

	void sortVector(vector<int> v){
	cout << "before sort:";
	printVector(v);

	//从小到大排序
	sort(v.begin(),v.end());//对[begin,end)进行从小到大排序

	cout << "after sort:" ;
	printVector(v);
}

(2)reverse(v.begin(),v.end());//对[begin,end)的元素倒置,但不排序(如1,3,2,4变为4,2,3,1)

	void reverseVector(vector<int> v){
	cout << "before reverse:";
	printVector(v);

	//reverse倒置vector
	reverse(v.begin(),v.end());//对[begin,end)的元素倒置,但不排序(如1,3,2,4变为4,2,3,1)

	cout << "after reverse:";
	printVector(v);

}

(3)降序排列,值得注意的是并没有提供一个直接的方法来使vector降序,这里结合sort()和reverse()使用来完成降序排列。

	void descVector(vector<int> v){
	cout << "before desc:";
	printVector(v);

	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());

	cout << "after desc:";
	printVector(v);
}

(4)copy(v1.begin(),v1.end(),position);//把v1从begin到end(不包括)的元素复制到v2,从v2的position位置开始,覆盖掉原有元素

	void showCopyVector(vector<int> v1,vector<int> v2){
	cout << "before copy,vector v2:";
	printVector(v2);
	cout << "copy vector v1:";
	printVector(v1);

	//copy(begin,end,position)
	copy(v1.begin(),v1.end(),v2.begin()+1);//把v1从begin到end(不包括)的元素复制到v2,从position开始,覆盖掉原有元素

	cout << "after copy,vector v2:";
	printVector(v2);
}

(5)find(v.begin(),v.end(),number);//在v中从v.begin()开始(包括它)到v.end()(不包括它)的元素中查找number,若存在返回指向number在当前向量中位置的迭代器。

	void findInVector(vector<int> v,int number){
	printVector(v);
	vector<int>::iterator it = find(v.begin(),v.end(),number);//返回指向number位置的迭代器
	int index = it - v.begin();
	cout << "find " << number << " in vector,index is:" << index << endl;
}

注意,访问vector中的数据,有下面两种方法:
(1) vector::at()
(2) vector::operator[]
operator[]主要是为了与C语言进行兼容,使它可以像C语言数组一样操作。
但at()是我们的首选,因为at()进行了边界检查,如果访问超过了vector的范围,将抛出一个异常。

6.附完整源代码
vector.cpp—主程序
/*
 * vector.cpp
 *
 *  Created on: 2019年11月11日
 *      Author: xb
 */
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>

using namespace std;

void showcwd();//获取当前工作目录
void printVector(vector<int> v);//打印vector
void sortVector(vector<int> v);//sort排序(从小到大)
void reverseVector(vector<int> v);//reverse倒置vector
void descVector(vector<int> v);//vector排序(从大到小)
void showCopyVector(vector<int> v1,vector<int> v2);//copy v1 to v2
void findInVector(vector<int> v,int number);//查找元素位置

int main() {

	cout << "当前工作目录:" ;
	showcwd();

	cout << "*******************一、vector初始化*******************************"
			<< endl;

	vector<int> a; //声明向量a
	cout << "1.使用push_back()插入数据" << endl;
	for (int i = 0; i < 10; i++) {
		a.push_back(i);
	}
	cout << "value of a:" << endl;
	printVector(a);
	cout << "size of a:" << a.size() << endl;
	cout << "=====================\n";

	vector<int> a1(10); //声明10个整型元素的向量
	cout << "2.声明长度后使用push_back()插入数据" << endl;
	for (int i = 0; i < 10; i++) {
		a1.push_back(i);
	}
	cout << "value of a1:" << endl;
	printVector(a1);
	cout << "size of a1:" << a1.size() << endl;
	cout << "=====================\n";

	vector<int> a2(10); //声明10个整型元素的向量
	cout << "3.声明长度后使用下标插入数据" << endl;
	for (int i = 0; i < 10; i++) {
		a2[i] = i;
	}
	cout << "value of a2:" << endl;
	printVector(a2);
	cout << "size of a2:" << a2.size() << endl;
	cout << "=====================\n";

	vector<int> a3(10, 1); //声明10个整型元素的向量,且初值为1
	cout << "4.定义向量的同时赋初值" << endl;
	cout << "value of a3:" << endl;
	printVector(a3);
	cout << "size of a3:" << a3.size() << endl;
	cout << "=====================\n";

	vector<int> b(10, 6); //声明10个整型元素向量b,初值为6
	vector<int> a4(b); //用向量b创建向量a,整体复制
	cout << "5.使用向量来创建向量" << endl;
	cout << "value of a4:" << endl;
	printVector(a4);
	cout << "size of a4:" << a4.size() << endl;
	cout << "=====================\n";

	vector<int> c;
	for (int i = 10; i < 15; i++) {
		c.push_back(i); //10,11,12,13,14
	}
	vector<int> a5(c.begin(), c.begin() + 3); //定义a5的值为c中的第0个到第2个,共三个
	cout << "6.使用一个向量中的值给另一个向量赋值" << endl;
	cout << "value of a5:" << endl;
	printVector(a5);
	cout << "size of a5:" << a5.size() << endl;
	cout << "=====================\n";

	//从数组中获得初值
	int d[5] = { 1, 2, 3, 4, 5 };
	vector<int> a6(d, d + 5);
	cout << "7.从数组中获得初值" << endl;
	cout << "value of a6:" << endl;
	printVector(a6);
	cout << "size of a6:" << a6.size() << endl;
	cout << "=====================\n";

	cout << "*******************二、vector常用方法*******************************"
			<< endl;
	cout << "1.back()" << endl;
	cout << "the last of a :" << a.back() << endl;
	cout << "=====================\n";

	cout << "2.front()" << endl;
	cout << "the first of a :" << a.front() << endl;
	cout << "=====================\n";

	cout << "3.print a[i]:" << endl;
	for (int i = 0; i < a.size(); i++) {
		cout << a[i] << " "; //a[i],返回a的第i个元素,当且仅当a[i]存在时
	}
//	cout << "\n a[13] = " << a[13];//错误值
	cout << "\n=====================\n";

	cout << "4.clear()" << endl;
	a.clear(); //清空a中的元素
	cout << "after clear:";
	printVector(a);
	cout << "=====================\n";

	cout << "5.empty()" << endl;
	cout << "vector a is empty?" << boolalpha << a.empty() << endl;
	cout << "vector a1 is empty?" << boolalpha << a1.empty() << endl;
	cout << "=====================\n";

	cout << "6.pop_back()" << endl;
	cout << "before pop_back() a2:";
	printVector(a2);
	a2.pop_back(); //删除最后一个元素
	cout << "after pop_back() a2:";
	printVector(a2);
	cout << "=====================\n";

	cout << "7.erase()" << endl;
	a6.erase(a6.begin(), a6.begin() + 2); //删除a6中的第0个(从第0个算起)至第1个元素,[0,2)
	cout << "after erase:" << endl;
	printVector(a6); //3,4,5
	cout << "=====================\n";

	cout << "8.push_back()" << endl;
	cout << "before push_back():";
	printVector(a6);
	a6.push_back(6); //在最后一个向量后插入元素
	cout << "after push_back():";
	printVector(a6);
	cout << "=====================\n";

	cout << "9.insert(position,value)" << endl;
	cout << "before insert:";
	printVector(a6);
	a6.insert(a6.begin() + 1, 6); //在a6的第一个元素位置处插入6。如3,4,5,6,则插入后为3,6,4,5,6
	cout << "after insert:";
	printVector(a6);
	cout << "=====================\n";

	cout << "10.insert(position,count,value)" << endl;
	cout << "before insert:";
	a6.erase(a6.begin() + 1, a6.begin() + 2);
	printVector(a6); //3,4,5,6
	a6.insert(a6.begin() + 1, 3, 7); //在a6的第1个位置插入3个7
	cout << "after insert:";
	printVector(a6); //3,7,7,7,4,5,6
	cout << "=====================\n";

	cout << "11.插入数组中的元素" << endl;
	cout << "before insert:";
	a6.erase(a6.begin() + 1, a6.begin() + 4);
	printVector(a6); //3,4,5,6
	//数组
	int e[6] = { 10, 11, 12, 13, 14, 15 };
	a6.insert(a6.begin(), e + 2, e + 5);	//[2,5),插入12,13,14
	cout << "after insert:";
	printVector(a6);	//12,13,14,3,4,5,6
	cout << "=====================\n";

	cout << "12.size()" << endl;
	cout << "size of a6:" << a6.size() << endl;	//vector中元素的个数
	cout << "=====================\n";

	cout << "13.在内存中可以容纳的元素的个数" << endl;
	cout << "capacity of a6 :" << a6.capacity() << endl;
	cout << "capacity of a5 :" << a5.capacity() << endl;
	cout << "capacity of a4 :" << a4.capacity() << endl;
	cout << "capacity of a3 :" << a3.capacity() << endl;
	cout << "capacity of a2 :" << a2.capacity() << endl;
	cout << "capacity of a1 :" << a1.capacity() << endl;
	cout << "capacity of a :" << a.capacity() << endl;
	cout << "=====================\n";

	cout << "14.resize()" << endl;
	cout << "before resize a5,size of a5:" << a5.size() << endl;
	a5.resize(10);
	cout << "after resize a5,size of a5:" << a5.size() << endl;
	cout << "=====================\n";

	cout << "15.将a1的现有元素个数调至新的size,多则删,少则补" << endl;
	cout << "before resize a1:" << endl;
	printVector(a1);
//	a1.resize(6, 2);	//将a的现有元素个数调至6个,多则删
//	cout << "多则删:" << endl;
	a1.resize(25, 6);	//少则补,补5个6
	cout << "少则补:" << endl;
	printVector(a1);
	cout << "=====================\n";

	//容量扩充
	cout << "16.容量扩充" << endl;
	vector<int> a7(10);
	cout << "before reserve:" << a7.capacity() << endl;
	a7.reserve(100);
	cout << "after reserve:" << a7.capacity() << endl;
	cout << "=====================\n";

	//两个向量整体交换
	cout << "17.两向量交换" << endl;
	cout << "before swap:" << endl;
	cout << "value of a5:";
	printVector(a5);
	cout << "value of a6:";
	printVector(a6);
	a5.swap(a6);	//交换a6,a5的值
	cout << "after swap:" << endl;
	cout << "value of a5:";
	printVector(a5);
	cout << "value of a6:";
	printVector(a6);
	cout << "=====================\n";

	cout << "a5==a6?"<< (a5 == a6) << endl;
	int f[7] = {12,13,14,3,4,5,6};
	vector<int> a8(f,f+7);
	cout << "a5==a8?" << (a5 == a8) << endl;
	cout << "capacity of a5 :" << a5.capacity() << endl;
	cout << "capacity of a8 :" << a8.capacity() << endl;
	cout << "size of a5 :" << a5.size() << endl;
	cout << "size of a8 :" << a8.size() << endl;
	cout << "=====================\n";

	//从向量中选择元素添加
	int n[6] = {1,2,3,4,5,6};
	vector<int> aa;
	vector<int> cc(n,n+4);//1,2,3,4
	cout << "value of cc:";
	printVector(cc);
	for(vector<int>::iterator it = cc.begin(); it < cc.end(); it++){
		aa.push_back(*it);
	}
	cout << "value of aa:";
	printVector(aa);
	cout << "=====================\n";

	//从文件中读取元素添加到向量
	ifstream in("data.txt");
	vector<int> bb;
	for(int i;in>>i;){
		bb.push_back(i);
	}
	cout << "read from file:";
	printVector(bb);
	cout << "=====================\n";

	cout << "*******************三、vector常用算法*******************************" << endl;
	ifstream fin("data1.txt");
	vector<int> v;
	for(int i;fin>>i;){
		v.push_back(i);
	}
//	cout << "read from data1.txt:";
//	printVector(v);
	cout << "vector升序排列:" << endl;
	sortVector(v);
	cout << "=====================\n";

	cout << "倒置vector" << endl;
	reverseVector(v);
	cout << "=====================\n";

	cout << "vector降序排列:" << endl;
	descVector(v);
	cout << "=====================\n";

	cout << "copy vector:" << endl;
	showCopyVector(aa,v);
	cout << "=====================\n";

	cout << "find in vector:" ;
	findInVector(v,6);
	cout << "=====================\n";

	//访问vector数据首选at()
	cout << "in vector:";
	printVector(v);
	cout << "index[2] is " << v.at(2) << endl;
	cout << "=====================\n";

	//assign()
	cout << "before assign:";
	printVector(v);
	vector<int> tmp = {1,3,5,7,9};
	cout << "with vector:";
	printVector(tmp);
	v.assign(tmp.begin(),tmp.begin()+3);
	cout << "after assign:";
	printVector(v);
	cout << "=====================\n";

	v.assign(4,2);//向量只包含4个元素,且值都为2
	printVector(v);

	//system("pause");

	return 0;
}
printVector.cpp—遍历vector
/*
 * printVector.cpp
 * 遍历vector
 *
 *  Created on: 2019年11月11日
 *      Author: xb
 */
#include <vector>
#include <iostream>

using namespace std;

/*
 * 使用下标从向量中获取元素
 */
void printVector(vector<int> v) {
	if (!v.empty()) {
		for (int i = 0; i < v.size(); i++) {
			cout << v[i] << " ";
		}
	} else {
		cout << "vector is empty!";
	}
	cout << "\n";
}

/*
 * 使用迭代器遍历
 */
void printVectorWithIterator(vector<int> v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << "\n";
}
vector_algorithm.cpp—vector相关算法
/*
 * vector_algorithm.cpp
 * 
 * sort()
 * reverse()
 * copy()
 * find()
 *
 *  Created on: 2019年11月12日
 *      Author: xb
 */
#include <algorithm> //和vector相关的几个重要算法
#include <vector>
#include <iostream>

using namespace std;

void printVector(vector<int> v);
void printVectorWithIterator(vector<int> v);

/*
 * sort排序-升序排列
 */
void sortVector(vector<int> v){
	cout << "before sort:";
	printVector(v);

	//从小到大排序
	sort(v.begin(),v.end());//对[begin,end)进行从小到大排序

	cout << "after sort:" ;
	printVector(v);
}

/*
 * vector倒置
 */
void reverseVector(vector<int> v){
	cout << "before reverse:";
	printVector(v);

	//reverse倒置vector
	reverse(v.begin(),v.end());//对[begin,end)的元素倒置,但不排序(如1,3,2,4变为4,2,3,1)

	cout << "after reverse:";
	printVector(v);

}

/*
 * vector降序排列
 *
 */
void descVector(vector<int> v){
	cout << "before desc:";
	printVector(v);

	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());

	cout << "after desc:";
	printVector(v);
}

/*
 * 复制一个vector的值到另一个vector
 *
 */
void showCopyVector(vector<int> v1,vector<int> v2){
	cout << "before copy,vector v2:";
	printVector(v2);
	cout << "copy vector v1:";
	printVector(v1);

	//copy(begin,end,position)
	copy(v1.begin(),v1.end(),v2.begin()+1);//把v1从begin到end(不包括)的元素复制到v2,从position开始,覆盖掉原有元素

	cout << "after copy,vector v2:";
	printVector(v2);
}

/*
   * 查找元素位置
 *
 */
void findInVector(vector<int> v,int number){
	printVectorWithIterator(v);
	vector<int>::iterator it = find(v.begin(),v.end(),number);//返回指向number的迭代器
	int index = it - v.begin();
	cout << "find " << number << " in vector,index is:" << index << endl;
}
getcwd.cpp—获取当前工作目录
/*
 * getcwd.cpp
 * 获取当前工作目录
 * 
 *  Created on: 2019年11月12日
 *      Author: xb
 */
#include <direct.h> //windows下,包含头文件<direct.h>或<unistd.h>均可
//#include <unistd.h> //linux下,只能使用<unistd.h>
#include <stdio.h>
#include <stdlib.h>

void showcwd() {
	char *buffer;
	if ((buffer = getcwd(NULL, 0)) == NULL) {
		perror("getcwd error");
	} else {
		printf("%s\n", buffer);
		free(buffer);
	}
}

以上代码在Eclipse IDE for C/C++ Developers(Version: 2018-09 (4.9.0))中运行正确,linux下结果一致,如有不一致的地方,欢迎批评指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
05-31
在Oracle中,PL/SQL是一种编程语言,允许开发人员编写存储过程、函数和触发器等程序对象。这些程序对象可以使用变量存储数据,其中包括全局变量和局部变量。 全局变量是定义在包中的变量,可以在包中的任何过程或函数中使用。在包中定义变量的语法如下: ``` CREATE OR REPLACE PACKAGE package_name AS variable_name datatype; ... END package_name; ``` 在这个包中,变量名为`variable_name`,数据类型为`datatype`。这个变量可以在这个包中的任何过程或函数中使用。 局部变量是在过程或函数中定义的变量,它们的作用域只在这个过程或函数中。在过程或函数中定义变量的语法如下: ``` CREATE OR REPLACE PROCEDURE procedure_name AS variable_name datatype; ... BEGIN ... END procedure_name; ``` 在这个过程或函数中,变量名为`variable_name`,数据类型为`datatype`。这个变量只能在这个过程或函数中使用,不能在其他过程或函数中使用。 全局变量和局部变量都可以存储不同类型的数据,例如数字、字符、日期等。在使用变量之前,需要先声明变量的类型和名称。在程序执行过程中,可以对变量进行赋值、修改等操作。 需要注意的是,在使用全局变量时,需要注意多个程序对象可能会同时访问同一个全局变量,因此需要考虑并发访问的问题。在使用局部变量时,需要注意变量的作用域和生命周期,确保变量只在需要的时候才会分配内存空间,并在不需要时及时释放内存空间,以避免资源的浪费。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值