vector二维操作

        vector<vector<int> >vt;  //变量的定义
	vt.push_back(vector<int>(10,1)); //用1行10列的向量初始化,初始值为1
	vt.push_back(vector<int>(10,2));
	for(vector<vector<int> >::iterator it=vt.begin();it!=vt.end();it++)  //判断外层循环
	{
		for(vector<int >::iterator it1=it->begin();it1!=it->end();it1++)  //判断内层循环
			cout<<*it1<<" ";
	   cout<<endl;
	}



vector 例子:

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

class Point
{
	int obj_id; 
	string des;
	float x, y;
	static int count;
public:
	Point(){}
	Point(int a,float c, float d, string e) :obj_id(a), x(c), y(d),des(e){ count++; }
	static int GetCount(){ return count; }
	bool operator<(const Point &p){ return (x < p.x || (x == p.x && y < p.y)); }
	float Getx() { return x; }
	float Gety() { return y; }
	string Getdes() { return des;}
};
int Point::count=0;

bool lessmark( Point &p1,  Point &p2)
{
	return p1 < p2;
}

void main()
{
	fstream p_file("f:/point.txt", ios::in );
	if (p_file.fail())
	{
		cout << "p_file文件打开失败";
		exit(0);
	}
	vector<Point> point;
	int a;
	float x1, y1 ,x2;
	string des;
	while (!p_file.eof())
	{
		p_file >> a >> x2 >> x1 >> y1 >> des;
		cout<< a <<" "<<x2<<" "<<x1<<" "<<y1<<" "<<des<<endl;
		Point p(a, x1, y1, des);
		point.push_back(p);
	}
	p_file.close();
	vector<Point>::iterator itp = point.begin();
	ofstream p2_file("f:/point2.txt");
	sort(point.begin(), point.end(),lessmark );
	for (; itp != point.end(); itp++)
	{
		cout<<(*itp).Getx()<<"    "<<(*itp).Gety()<<" " << (*itp).Getdes()<<endl;
	}
	p2_file.close();
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值