STL vector用法详解
标准模板库(STL)是程序员定义的数据类型和算法的集合,可供 C++ 程序使用。这些数据类型和算法不是 C++ 语言的一部分,但它们的创建是对内置数据类型的有益补充。如果打算继续在计算机科学领域学习,那么就应该熟悉 STL。本节介绍 STL 数据类型之一:矢量(vector)。 注意,要使用矢量,则程序的头文件必须指明使用 namespacestd,因为矢量包含在该命名空间中。许多较早的编译器不允许命名空间或不支持 STL。 在 STL 中定义的数据类型通常称为容器。之所以称为容器,是因为它们存储和组织数据。STL 中有两种类型的容器:顺序容器和关联容器:
vector 数据类型是一个序列容器,它很像一个一维数组,其表现和数组相似的地方如下:
但是,矢量相对于数组还有几个优点,比如说:
vector定义和初始化要在程序中使用矢量,则必须使用以下语句包含 vector 头文件:#include <vector> 创建矢量对象的语法与定义一个常规变量或数组所用的语法有所不同。以下是一个例子:vector<int> numbers; 这个语句将 numbers 定义为一个 int 的矢量。请注意,数据类型包含在尖括号内,紧跟在单词 vector 之后。由于矢量的大小随着添加值而扩大,因此不需要声明大小。不过,如果愿意的话,也可以声明一个开始大小。示例如下:vector<int> numbers(10); 这个语句将 numbers 定义为 10 个整数的矢量,但这只是一个起始大小。如果添加 10 个以上的值,它的大小将会扩大。注意,如果为矢量指定开始大小,则大小声明符将被括在圆括号中,而不是方括号。 为矢量指定开始大小时,还可以指定一个初始化值。初始化值被复制到每个元素。示例如下:vector<int> numbers (10,2); 在这个语句中,numbers 被定义为 10 个整数的矢量,并且 numbers 中的每个元素都被初始化为值 2。也可以用另一个矢量中的值初始化一个矢量。例如,如果 set1 是已经有值的 int 矢量,则以下语句将创建一个名为 set2 的新矢量,它是 set1 的精确副本: vector<int> set2(set1); 在执行这个语句之后,矢量 set2 将具有相同数量的元素,并保持与 set1 相同的一组值。如果使用的是 C++ 11 版本,则还可以使用一个值列表来初始化 vector,示例如下:vector<int> numbers { 10,20,30,40 }; 该语句定义了一个名为 numbers 的 int 矢量。该矢量有 4 个元素,其初始化值为 10,30 和 40。注意,初始化列表被括在一组大括号中,但是在它前面不使用赋值运算符(=)。表 1 总结了前面讨论的矢量定义过程。
存储和检索 vector 中的值要将值存储到已存在的矢量元素中,或访问矢量元素中存储的数据,可以使用数组下标运算符 []。下面的程序演示了该操作://This program stores employee hours worked // and hourly pay rates in two parallel vectors. #include <iostream> #include <iomanip> #include <vector> // Needed to use vectors using namespace std; int main() { const int NUM_EMPS = 5; // Number of employees vector <int> hours(NUM_EMPS); // Define a vector of integers vector <double> payRate(NUM_EMPS); // Define a vector of doubles double grossPay; // An employee's gross pay // Get employee work data cout << "Enter the hours worked and hourly pay rates of " << NUM_EMPS << " employees. n"; for (int index = 0; index < NUM_EMPS; index++) { cout <<"nHours worked by employee #" << (index + 1) << ": "; cin >> hours[index]; cout << "Hourly pay rate for employee #" << (index +1) << ": "; cin >> payRate[index]; } //Display each employeeA s gross pay cout << "nHere is the gross pay for each employee: n" ; cout << fixed << showpoint << setprecision (2); for (int index = 0; index < NUM_EMPS; index++) { grossPay = hours[index] * payRate[index]; cout << "Employee #" << (index + 1); cout << ": $" << setw(7) << grossPay << endl; } return 0; }程序输出结果为:
Enter the hours worked and hourly pay rates of 5 employees. #include <vector> 在第 11 行和第 12 行中的以下语句定义了两个矢量:
vector<int> hours (NUM_EMPS) ;??? // 定义整数矢量 由于在每个矢量名称后面跟着一个大小声明符,所以这两个矢量都将按指定的大小创建。在此示例中,命名常量 NUM_EMPS 等于 5,所以两个矢量一开始都有 5 个元素。因为它们已经有元素了,所以可以使用 [] 运算符从元素中读取值或给元素赋值,这与使用数组元素的方式完全一样。
cin >> hours[index]; grossPay = hours[index] * payRate[index]; 只要矢量已经定义并且拥有给定的起始元素,就可以像使用数组一样访问它们,存储和检索其数据。遍历vector在 C++11 版本中,程序员可以使用基于范围的 for 循环遍历矢量中的元素,下面的程序演示了其用法:// This program uses two range-based for loops with a vector. #include <iostream> #include <vector> using namespace std; int main() { // Define a vector with a starting size of 5 elements vector<int>numbers(5); // Get values for the vector elements // Make the range variable a reference variable so it can be // used to change the contents of the element it references. for (int &val : numbers) { cout << "Enter an integer value: "; cin >> val; } // Display the vector elements cout << "nHere are the values you entered: n"; for (int val : numbers) cout <<val <<" "; cout << endl; return 0; }程序输出结果:
Enter an integer value: 10 vector push_back 成员函数不能使用 [] 运算符来访问尚不存在的矢量元素。要将值存储在没有起始大小或已满的矢量中,应使用 push_back 成员函数。该函数接受一个值作为实参,并将其存储在位于矢量末尾的新元素中。以下是一个使用 push_back 函数,将一个元素添加到一名为 numbers 的 int 矢量的例子: numbers.push_back(25); 该语句可以创建一个新的元素,其存储的值为 25,该元素被放在 numbers 矢量的末尾。如果 numbers 以前没有元素,则新元素将成为其单一元素。下面的程序允许用户指定员工的数量。hours 和 payRate 这两个矢量并没有定义开始大小。由于这些矢量没有起始元素,因此需要使用 push_back 成员函数来存储值。 // This program stores employee hours worked and hourly pay rates // in two vectors. It demonstrates the use of the push_back member // function to add new elements to the vectors. #include <iostream> #include <iomanip> #include <vector> // Needed to use vectors using namespace std; int main() { vector<int> hours; // hours is an empty integer vector vector<double> payRate; // payRate is an empty double vector double grossPay; int numEmp1oyees; // Number of employees int index; // Loop counter // Get the number of employees cout << "How many employees do you have?"; cin >> numEmp1oyees; // Input the payroll data cout << "Enter the hours worked and hourly pay rates of the "<< numEmployees << " employees. n" ; for (index = 0; index < numEmployees; index++) { int tempHours; // Number of hours entered double tempRate; 丨丨 Pay rate entered cout << "Hours worked by employee #" << (index + 1) << ":"; cin >> tempHours; hours.push_back(tempHours); // Add an element to hours cout << "Hourly pay rate for employee #" << (index + 1) << ": "; cin >> tempRate; payRate.push_back(tempRate); // Add an element to payRate } //Display each employeeT s gross pay cout << "nHere is the gross pay for each employee: n"; cout << fixed << showpoint << setprecision (2); for (index = 0; index < numEmployees; index++) { grossPay = hours[index] * payRate[index]; cout << "Employee #" << (index + 1); cout << " $" << setw (7) << grossPay << endl; } return 0; }程序输出结果:
How many employees do you have?3 vector size成员函数与数组不同,矢量可以报告它们包含的元素的数量,这是通过 size 成员函数实现的。以下是一个使用 size 成员函数的示例:numValues = set.size(); 在该语句中,假定 numValues 是一个 int,set 是一个矢量。执行语句后,numValues 将包含 set 中的元素数量。在编写接收矢量作为实参的函数时,size 成员函数特别有用。例如,来看 showValues 函数的以下代码: void showValues(vector<int> vect) { for (int count = 0; count < vect.size(); count++) cout << vect[count] << endl; }因为矢量可以报告其大小,所以该函数不需要第二个参数来表示矢量中元素的个数。下面的程序演示了该函数的应用: //This program demonstrates the vector size member function. #include <iostream> #include <vector> using namespace std; // Function prototype void showValues(vector<int>); int main() { vector<int> values; // Store a series of numbers in the vector for (int count = 0; count < 7; count++) values.push_back(count * 2); // Display the numbers showValues(values); return 0; } void showValues(vector<int> vect) { for (int count = 0; count < vect.size(); count++) cout << vect[count] << " "; cout << endl; }程序输出结果: 0 2 4 6 8 10 12 程序中在定义矢量时,并未给定元素,所以在将值存储到矢量中时,使用了 push_back 函数。每次被调用时,push_back 函数都将创建一个新的矢量元素来存储值。vector pop_back成员函数要从矢量中删除最后一个元素,可以使用 pop_back 成员函数。以下语句从名为 collection 的矢量中移除最后一个元素:collection.pop_back(); 下面的程序演示了 pop_back 函数://This program demonstrates the vector size,//push_back,and pop_back member functions. #include <iostream> #include <vector> using namespace std; int main() { vector<int> values; // Store values in the vector values.push_back(1); values.push_back(2); values.push_back(3); cout << "The size of values is " << values.size () << endl; // Remove a value from the vector cout << "Popping a value from the vector...n"; values.pop_back(); cout << "The size of values is now " << values.size () << endl; // Now remove another value from the vector cout << "Popping a value from the vector...n"; values.pop_back(); cout << "The size of values is now " << values.size() << endl; // Remove the last value from the vector cout << "Popping a value from the vector...n"; values.pop_back(); cout << "The size of values is now " << values.size () << endl; return 0; }程序输出结果:
The size of values is 3 cout << "The value being removed from the vector is "<<values.pop_back() << endl; // Error! vector clear成员函数要完全清除矢量的内容,可以使用 clear 成员函数,如下例所示:numbers.clear(); 在执行该语句之后,numbers 的所有元素都将被清除。下面的程序演示了该函数的用法://This program demonstrates the vector clear member function. #include <iostream> #include <vector> using namespace std; int main() { vector<int> values(100); cout << "The values vector has " << values.size() << " elements.n"; cout << "will call the clear member function. . . n"; values.clear(); cout << "Now the values vector has "<< values.size() << " elements . n"; return 0; }程序输出结果:
The values vector has 100 elements. vector empty成员函数要确定一个矢量是否为空,可以使用 empty 成员函数。如果矢量为空,则该函数将返回 true;如果矢量中存储有元素,则返回 false。假设 numberVector 是一个矢量,以下是其应用示例:if (numberVector.empty()) cout << "No values in numberVector. n";下面的程序显示了如何传递一个矢量给函数。该函数名为 avgVector,演示了 empty 成员函数的用法。 //This program demonstrates the vector empty member function. #include <iostream> #include <vector> using namespace std; // Function prototype double avgVector(vector<int>); int main() { vector<int> values; // Define a vector to hold int values int numValues; // Number of values to be averaged double average; // Average of the stored values //Get the number of values to average cout << "How many values do you wish to average?"; cin >> numValues; // Get the values and store them in a vector for (int count = 0; count < numValues; count++) { int tempValue; cout << "Enter an integer value:"; cin >> tempValue; values.push_back(tempValue); } // Get the average of the values and display it average = avgVector(values); cout << "Average: " << average << endl; return 0; } double avgVector(vector<int> vect) { int total = 0; // Accumulator double avg = 0.0; if (vect.empty()) // Determine if the vector is empty cout << "No values to average. n"; else { for (int count = 0; count < vect.size(); count++) total += vect[count]; avg = static_cast<double>(total)/vect.size(); } return avg; }程序输出结果:
How many values do you wish to average?4 vector成员函数汇总表 2 提供了有关矢量成员函数的汇总信息,其中有一些在已经讨论过,另外还有一些是新添加的。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |