C++vector

vector C++数组

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


int main(){
	vector<int> v;
	v.push_back(100);
	v.push_back(200);
	cout << v.size() << endl;
	vector<int> v1(10);
	cout << v1.size() << endl;
	vector<int> v2(10,1024);
	for(int i=0;i<v2.size();i++){
		cout << v2[i] << " ";	
	}
	cout << endl;
	v2[0] = 9527;
	for(int i=0;i<v2.size();i++){
		cout << v2.at(i) << " ";	
	}
	cout << endl;

	cout << v2.size() << endl;
	int arr[5] = {1,2,3,4,5};
	vector<int> v3(arr,arr+5);
	cout << v3.size() << endl;	
	return 0;
}

v.push_back() 压入元素
两种表示元素的方式:v[i]、v.at(i)

int arr[5]={1,2,3,4,5};
vector < int> v(arr,arr+5); //vector使用已经创建的数组来创建时候,前面两个值都是地址

迭代器 vector < 类型 > ::iterator it

vector < int>::iterator it = v.begin();
v.end();
//begin() 返回的是第一个元素的指针
//end() 返回的是最后一个元素的下一个位置的指针
*it ;//地址取 * 即当前值
it = find(v.begin(),v.end(),5);//find [beg,end) 区间查找某个元素
//找到打印整个值,没找到的话 it指向v.end(),因为是最后一个位置的下一个位置,所以是随机值

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

int main(){
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	vector<int> v(arr,arr+10);
	vector<int>::iterator it = v.begin(); //指针 指向第一个元素
	//begin()  返回的是第一个元素的指针
	//end()    返回的是最后一个元素的下一个位置的指针
	for(;it != v.end(); it++){
		cout << *it << " ";
	}
	cout << endl;
	it = find(v.begin(),v.end(),5);//find [beg,end) 区间查找某个元素
	
	if(it != v.end()){
		cout << *it << endl;	
	}
	sort(v.begin(),v.end());

	return 0;
}

自定义写一个Emp调用迭代器和vector

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

class Emp{
private:
	int no;
	string name;
	float salary;
public:
	Emp(int no=0,string name="",float salary=0):no(no),name(name),salary(salary){
		
	}
	
	friend ostream& operator<<(ostream& os,const Emp& e){
		return os << e.no << " " << e.name << " " << e.salary;	
	}
	bool operator==(const Emp& e)const{
		return e.no == no;	
	}	
	bool operator<(const Emp& e)const{
		return salary < e.salary;	
	}
	void hello(){
		cout << "Hello" << endl;	
	}
	friend class Comp;
};

class Comp{
public:
	bool operator()(const Emp& e1,const Emp& e2){
		return e2.no < e1.no;	
	}
};

int main(){
	vector<Emp> v;
	Emp e = Emp(110);
	v.push_back(Emp(110,"张飞",100));
	v.push_back(Emp(120,"关羽",90));
	v.push_back(Emp(130,"赵云",98));
	v.push_back(Emp(140,"马超",89));
	v.push_back(Emp(150,"黄忠",97));
	vector<Emp>::iterator it;
	for(it = v.begin();it != v.end(); it++){
		cout << *it << endl;	
	}
	it = find(v.begin(),v.end(),e);
	cout << *it << endl;
	sort(v.begin(),v.end());
	cout << "-------------" << endl;
	for(it = v.begin();it != v.end(); it++){
		cout << *it << endl;	
	}
	sort(v.begin(),v.end(),Comp());
	cout << "-------------" << endl;
	for(it = v.begin();it != v.end(); it++){
		cout << *it << endl;	
	}
	it->hello();//指向v.end()也能使用类中的函数
	return 0;	
}

第一个sort部分因为在类Emp中重载了<的实现,用工资实现升序;
所以最会输出
140 马超 89
120 关羽 90
150 黄忠 97
130 赵云 98
110 张飞 100
第二个sort部分,定义了一个友元类的比较函数,按学号降序
150 黄忠 97
140 马超 89
130 赵云 98
120 关羽 90
110 张飞 100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值