vector - CS215UKY

advertisement
Vectors
In C++, vectors are one type of dynamically-allocated container from the standard template library (STL).
They are capable of storing multiple elements of any defined datatype, and do so in a contiguous block of memory.
Defining A vector
vector <int> testVector;
vector <long> testVector(10);
vector <float> testVector(5,1.0);
 The first syntax declares an empty vector capable of storing the integer.
datatype.
 The second declares a vector with storage space for 10 long integers, each of which is intialized to the default value
for the type.
 The final line declares a vector with storage for 5 floats, and initializes each of their values to 1.0
Most Important method:
 at()
 back()
 begin()
 capacity()
 end()
 front()
 max_size()
 pop_back()
 push_back()
 assign()
 clear()






empty()
erase()
insert()
reserve()
resize()
size()
Accessing Elements of a Vector using at () method:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
/* Initialize vector of 10 copies of the integer 5 */
vector<int> vectorOne(10,5);
/* Display size of vector */
cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
/* run through the vector and display each element, using size() to determine index boundary */
for (long index=0; index < (long) vectorOne.size(); ++index) {
cout << "Element " << index << ": " << vectorOne.at(index) << endl;
}
}
Size, capacity and max_size methods
 size() returns the number of elements in the vector
There are two other member functions that are closely related to size(). So closely related, in fact, that they are frequently
confused with size() by novices and experts alike. These functions are capacity() and max_size().
 The method capacity() returns the number of elements that the vector can hold before more space is allocated
 The method max_size() returns the maximum number of elements that the container can hold.
o max_size() is determined by the system, the compiler, and the computer’s architecture, and refers to memory
address space limitations on a particular machine.
resize() and reserve() methods:
resize () changes the actual number of elements in a vector.
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
/* Initialize vector of 10 copies of the integer 5 */
vector<int> vectorOne(10,5);
/* Display size of vector */
cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
/* run through the vector and display each element, using size() to determine index boundary */
for (long index=0; index<(long)vectorOne.size(); ++index) {
cout << "Element " << index << ": " << vectorOne.at(index) << endl;
}
/* Change size of vector - element removal */
vectorOne.resize(7);
/* Display size of vector */
cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
/* run through the vector and display each element, using size() to determine index boundary */
for (long index=0; index<(long)vectorOne.size(); ++index) {
cout << "Element " << index << ": " << vectorOne.at(index) << endl;
}
}
Inserting and Deleting Elements
For adding a single element to the end of a vector, the method push_back() is used, it appends the add element to the end of the
vector
To remove a single element from the end of a vector, the function pop_back()
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
// initialize empty vector of integers
vector<int> vectorOne;
// use push_back() to add 10 random integers to the vector
for (long index=0; index<10; ++index)
vectorOne.push_back(rand());
// display contents of vector
cout << "vectorOne contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index)
cout << vectorOne.at(index) << " ";
cout << endl << endl;
// add two more elements to the vector using push_back()
for (long index=0; index<2; ++index)
vectorOne.push_back(rand());
// display contents of vector
cout << "vectorOne now contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index)
cout << vectorOne.at(index) << " ";
cout << endl << endl;
// remove the last 9 elements of the vector using pop_back()
for (long index=0; index<9; ++index)
vectorOne.pop_back();
// display contents of vector
cout << "vectorOne now contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index)
cout << vectorOne.at(index) << " ";
cout << endl << endl;
}
insert() and erase() methods
It's perfectly reasonable to assume that, at some point, someone is going to need to insert or remove an element at a specific
location other than the very end. The vector container provides methods for doing so: insert() and erase().
These are both very handy functions, but they require the use of iterators,
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
// initialize empty vector of integers
vector<int> vectorOne;
vector<int> vectorTwo;
// use push_back() to add 10 random integers from 0-4 to vectorOne
for (long index=0; index<10; ++index) vectorOne.push_back(rand()%5);
// use push_back() to add 10 random integers from 5-9 to vectorOne
for (long index=0; index<10; ++index) vectorTwo.push_back(5+rand()%5);
// display contents of vectorOne
cout << "vectorOne contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index) cout << vectorOne.at(index) << " ";
cout << endl;
// display contents of vectorTwo
cout << "vectorTwo contains the following elements:" << endl;
for (long index=0; index<(long)vectorTwo.size(); ++index) cout << vectorTwo.at(index) << " ";
cout << endl;
cout << endl << "inserting middle 6 elements from vectorOne into vectorTwo, then erasing them..." << endl;
// insert middle 6 elements from vectorOne into vectorTwo...
vectorTwo.insert(vectorTwo.begin()+5, vectorOne.begin()+2, vectorOne.end()-2);
// ...then erase those elements from vectorOne
vectorOne.erase(vectorOne.begin()+2, vectorOne.end()-2);
// display contents of vectorOne
cout << "vectorOne now contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index) cout << vectorOne.at(index) << " ";
cout << endl;
// display contents of vectorTwo
cout << "vectorTwo now contains the following elements:" << endl;
for (long index=0; index<(long)vectorTwo.size(); ++index) cout << vectorTwo.at(index) << " ";
cout << endl;
cout << endl << "erasing the third element from vectorTwo, and inserting a zero at the front." << endl ;
// erase third element (index=2) and insert a zero at start (index=0)
vectorTwo.erase(vectorTwo.begin()+2);
vectorTwo.insert(vectorTwo.begin(), 0);
// display contents of vectorTwo
cout << "vectorTwo now contains the following elements:" << endl;
for (long index=0; index<(long)vectorTwo.size(); ++index) cout << vectorTwo.at(index) << " ";
cout << endl;
}
assign ( )
Most of the techniques for assigning values to a vector dealt with to this point consider only assigning a value to a single
element, or assigning multiple elements the same value. There are a couple of functions that allow for the en masse assignment
of elements to a vector. The first is assign()
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
// initialize empty vector integers
vector<int> vectorOne;
// display contents of vectorOne
cout << "vectorOne contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index) cout << vectorOne.at(index) << " ";
cout << endl << endl;
// create a static array with 10 integer elements
int arrayOne[]={ 4, 2, 6, 3, 6, 3, 4, 6, 4, 3};
// use assign() to give vectorOne 5 copies of the integer 1
cout << "Using assign() to fill vector with 5 copies of integer 1." << endl << endl;
vectorOne.assign(5,1);
// display contents of vectorOne
cout << "vectorOne contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index) cout << vectorOne.at(index) << " ";
cout << endl << endl;
// display contents of arrayOne - note the dangerous, dangerous [] usage
cout << "arrayOne contains the following elements:" << endl;
for (long index=0; index<10; ++index) cout << arrayOne[index] << " ";
cout << endl << endl;
// assign contents of static array to the vector
cout << "Assigning contents of arrayOne to vectorOne." << endl << endl;
vectorOne.assign(arrayOne, arrayOne+10);
// display contents of vectorOne
cout << "vectorOne contains the following elements:" << endl;
for (long index=0; index<(long)vectorOne.size(); ++index) cout << vectorOne.at(index) << " ";
cout << endl;
}
font(), back(), begin(), end(), clear() and empty() methods
 The front() and back() methods are used to return references to the start and end of a vector, respectively.
 The back() method is especially convenient when you need to play around with an element after you’ve push_back()’ed it
into a vector
 begin() and end() return random access iterators to the first element, and the location just after the last element,
respectively
 clear() removes all of the elements from a vector.
o This means that size() will return zero after clear() is called. However, the capacity of the vector will remain the
same.
 empty() returns true for the vector v if v.size()==0, and false otherwise
Multidimensional Vectors
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(int* argc, char* argv) {
vector<vector<float> > mdVector;
float tempValue;
//
declares empty 2D-array constructed from STL vectors
// temporary value for storing input
// get a series of 10 floating point numbers
do {
//
get numbers from user
cout << "Enter a floating-point number (no characters!): ";
cin >> tempValue;
// input number & its sine value into array
mdVector.push_back( vector<float>(2,0.0) );
// add 2-element row to array
mdVector.back().at(0)=tempValue;
// store number
mdVector.back().at(1)=sin(tempValue);
// store sin(number)
} while( mdVector.size()<10 );
// display inputted numbers and their respective sine-values
cout << endl << "Unsorted numbers:" << endl << "#\tsin(#)" << endl;
for (long row=0; row<(long)mdVector.size(); ++row) {
cout << mdVector.at(row).at(0) << "\t" << mdVector.at(row).at(1) << endl;
}
// sort numbers by their sine values, using a quick and dirty
// direct sorting algorithm, and sqaw() to exchange rows
for (long row1=0; row1<(long)mdVector.size(); ++row1) {
for (long row2=row1; row2<(long)mdVector.size(); ++row2) {
if ( mdVector.at(row1).at(1) > mdVector.at(row2).at(1) ) {
mdVector.at(row1).swap(mdVector.at(row2));
}
}
}
// display numbers sorted by their respective sine-values
cout << endl << "Sorted numbers:" << endl << "#\tsin(#)" << endl;
for (long row=0; row<(long)mdVector.size(); ++row) {
cout << mdVector.at(row).at(0) << "\t" << mdVector.at(row).at(1) << endl;
}
// remove all rows except those with the two largest and two smallest sine values
mdVector.erase(mdVector.begin()+2, mdVector.end()-2);
// display the array with the numbers removed
cout << endl << "Numbers with all but 2 largest and 2 smallest sine values removed :" << endl << "#\tsin(#)" << endl;
for (long row=0; row<(long)mdVector.size(); ++row) {
cout << mdVector.at(row).at(0) << "\t" << mdVector.at(row).at(1) << endl;
}
cout << "Press any key to continue...";
cin.ignore();
cin.get();
}
Source:
http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/
http://www.dreamincode.net/forums/topic/34015-c-vector-tutorial-ii/
Download