C++批量数据处理

vector

vector可以保存给定类型的一连串的值,为了容纳新的值,vector可以根据需要来增加长度。

一个单独的vector包含的所有值都是同一类型的,但是不同的vector可以包含不同类型的对象

格式:

vector homework  //定义vector时要申明变量类型
homework.push_back(x) //添加数据

实际应用中的程序:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;


int main()
{
	cout << "please input your first name: ";
	string name;
	cin >> name;
	cout << "Hello, " << name << "!" << endl;
	cout << "Please enter your midterm and final exam grades: ";
	double midterm, final;
	cin >> midterm >> final;
	cout << "Enter your homw work grades,"
		"followed by end-of-file: ";
	vector homework;
	double x;
	while (cin >> x)
		homework.push_back(x);
	typedef vector::size_type vec_sz;
	vec_sz  size = homework.size();
	if (size == 0)
	{
		cout << endl << "You must enter your grades. "
			"please try again. " << endl;
		return 1;
	}

	sort(homework.begin(), homework.end());
	vec_sz mid = size / 2;
	double median;
	median = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) / 2 : homework[mid];

	streamsize prec = cout.precision();
	cout << "Your final grade is " << setprecision(3)
		<< 0.2*midterm + 0.4*final + 0.4*median
		<< setprecision(prec) << endl;
	system("pause");
	return 0;
}



你可能感兴趣的:(C++)