主要记录两点:
1、用vector定义可变长度数组
2、用vector按多字段值排序(数组中存储具有多个属性的类,按照类的某一属性进行排序)
3、从文件读入邻接矩阵,用vector存储矩阵进而求节点度的实例
1、用vector定义数组
在设计程序常会碰到这样的问题,需要根据输入数据来确定所用数组的大小,或者数组长度需要随程序运行动态变化。
这时有两种思路来设计可变长度的数组:
一种是用new 和 delete来动态分配和释放内存,从而达到动态数组的目的。
int len;
cin>>len;
int *p=new int[len];
......
delete[] p;
上面代码根据输入参数len来定义一个长度为len的数组,这里如果直接写
int len;
cin>>len;
int p[len];
是有问题的,因为这种形式的数组长度是在编译的时候确定的,故而后面的参数必须是一个常数。
还有要注意的是int *p=new int[len]开辟的内存必须用delete[] p来释放。
另一种是用C++标准模版库(STL)中的vector实现变长数组。
int len;
cin>>len;
vector<int> array(len);
后面要使用数组array的时候跟普通数组一样用array[i]就可以了。
这一部分主要参考C++动态数组
下面是用vector定义确定大小的二维数组
vector<vector<double>> A(5,vector<double>(5));//定义一个5*5的二维数组
下面是一个根据输入来确定数组大小的例子:
面试的时候在线编程要求第一行输入数据组数N,第二行输入行数R和列数C
接下来输入N组R*C的二维数组
/**********************************************************
* test.cpp
*
* 第一行输入数据组数N,第二行输入行数R和列数C,接下来输入N组R*C的二维数组
*
* 张京林, 2016-3
*********************************************************/
#include <iostream>
#include <vector>
using namespace std;
void main()
{
int N, R, C, num;
cin >> N;//N组测试数据
vector<vector<vector<int>>> testarray(N);
//vector<vector<vector<int>>> testarray;//配合push_back使用
cin >> R >> C;//每组数据都是R行C列的二维数组
vector<vector<int>> array(R,vector<int>(C));
for (int n = 0; n < N; n++)
{
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cin >> num;
array[i][j] = num;
}
}
//testarray.push_back(array);
testarray[n] = array;//可以直接赋值
}
getchar();//停留在结果界面
}
2、用vector按多字段值排序
有时候我们想把类对象存在数组里,后续操作希望能够根据对象不同的属性来对数组重新排序,这时候我们就需要用到sort函数了。
class Student
{
private:
int id; // 学号
float eyesight; // 视力
float height; // 身高
public:
Student(int id, float eyesight, float height)
{
this->id = id;
this->eyesight = eyesight;
this->height = height;
}
int get_id()
{
return id;
}
float get_eyesight()
{
return eyesight;
}
float get_height()
{
return height;
}
// 比较大小的函数(谓词)
bool comparer(Student& stu_a, Student& stu_b)
{
// 按eyesight升序 + height升序排列
if(stu_a.get_eyesight() != stu_b.get_eyesight())
return (stu_a.get_eyesight() < stu_b.get_eyesight());
else
return (stu_a.get_height() < stu_b.get_height());
}
int main(int argc, char** argv)
{
vector<Student> vec;
vec.push_back(Student(4, 1.1f, 170.2f));
vec.push_back(Student(3, 1.1f, 163.4f));
vec.push_back(Student(2, 1.5f, 166.6f));
vec.push_back(Student(1, 1.5f, 173.2f));
// 调用STL中的sort函数,其中的第三个参数就是我们前面定义的,比较两个Student对象大小的函数
sort(vec.begin(), vec.end(), comparer);
vector<Student>::iterator iter;
for(iter = vec.begin(); iter != vec.end(); ++iter)
{
cout << (*iter).get_eyesight() << "\t" << (*iter).get_height() << endl;
}
return 0;
}
这里主要参考STL vector按多字段值排序
3、用vector存储矩阵求节点度的实例
下面是我写的一个应用,通过文件读入一个邻接矩阵,然后根据邻接矩阵计算各节点的度,并按照节点的度排序,以研究度中心性( degree centrality d e g r e e c e n t r a l i t y )。我定义了一个node类,有两个属性:index和degree,我根据degree排序之后可以通过查询index知道该位置的是哪个节点。
/**********************************************************
* centrality.cpp
*
* 无向图的中心性
*
* 根据无向图节点的度中心性来寻找中心节点
*
* 张京林, 2015-4
*********************************************************/
#include "centrality.h"
#include "node.h"
using namespace std;
int main(void)
{
adj_Mat G = input();
int N = G.size();
vector<int> degree(N); //数组用于暂时存储计算得到的节点的度
vector<node> Nodes(N); //矩阵的节点
for (unsigned int i = 0; i < G.size(); i++)
{
degree[i] = 0;
}
//打印读入的邻接矩阵
/*for (unsigned int i = 0; i < G.size(); i++)
{
for (unsigned int j = 0; j < G[i].size(); j++)
{
cout << G[i][j] << "" << flush;
}
cout << endl;
}*/
//根据邻接矩阵计算各节点的度
for (unsigned int i = 0; i < G.size(); i++)
{
for (unsigned int j = 0; j < G[i].size(); j++)
{
if (G[i][j] == 1 && i!=j)
degree[i]++;
}
Nodes[i].set_index(i);
Nodes[i].set_degree(degree[i]);
}
//根据compare_degree规定的规则对数组内的节点排序
sort(Nodes.begin(), Nodes.end(), compare_degree);
return 0;
}
/*********************************************************
* node.h
*
* 定义了一个节点类
*
* 张京林, 2015-4
**********************************************************/
#pragma once
#include <vector>
class node
{
public:
node();
~node();
//设置与获取节点序号
int set_index(int i);
int get_index();
//设置与获取节点的度
int set_degree(int i);
int get_degree();
private:
int index;//节点序号
int degree;//节点的度
};
node::node()
{
index = 0;
degree = 0;
}
node::~node()
{
}
int node::set_index(int i)
{
index = i;
return 0;
}
int node::get_index()
{
return index;
}
int node::set_degree(int i)
{
degree = i;
return 0;
}
int node::get_degree()
{
return degree;
}
/*********************************************************
* centrality.h
*
* centrality.cpp的头文件
*
* 定义了文件读取函数adj_Mat input();和排序规则函数bool compare_degree(node& node_a, node& node_b);
*
* 张京林, 2015-4
**********************************************************/
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm> //sort必须包含这个头文件
#include <sstream> //istringstream 必须包含这个头文件
#include "node.h"
using namespace std;
bool compare_degree(node& node_a, node& node_b);//用于sort的第三个参数,设定排序规则
typedef vector<vector<int>> adj_Mat; //邻接矩阵
adj_Mat input();
//按照节点度降序排列
bool compare_degree(node& node_a, node& node_b)
{
return (node_a.get_degree() > node_b.get_degree());
}
adj_Mat input()
{
ifstream infile("graph1.txt");//从文件读入邻接矩阵
adj_Mat matrix;
istringstream istr;
string str;
vector<int> tmpvector;
while (getline(infile, str))
{
istr.str(str);
int tmp;
while (istr >> tmp)
{
tmpvector.push_back(tmp);//先读入一行数据,存在一个一个向量(数组)中
}
matrix.push_back(tmpvector);//再把每个行向量作为一个元素存到另一个向量(数组)中
tmpvector.clear();
istr.clear();
}
infile.close();
return matrix;
}