C++/Data Structure/Vector — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 14:21, 25 мая 2010
Содержание
- 1 Access a vector using an iterator.
- 2 Accessing a Vector Through an Iterator
- 3 Access the elements of a vector through an iterator.
- 4 Create permutations based on vector
- 5 Creating and Resizing Vectors
- 6 Demonstrate accumulate() in vector
- 7 Demonstrate adjacent_difference() in vector
- 8 Demonstrate allocator"s max_size() fucntion in vector
- 9 Demonstrate count and count_if.
- 10 Demonstrate count_if().
- 11 Demonstrate count() in vector
- 12 Demonstrate inner_product() in vector
- 13 Demonstrate insert and erase.
- 14 Demonstrate insert_iterator in vector
- 15 Demonstrate partial_sum() in Vector
- 16 Demonstrate remove_copy and replace_copy.
- 17 Demonstrate remove_copy in Vector
- 18 Demonstrate reverse in vector
- 19 Demonstrating the four ways that vectors can be created.
- 20 end() in vector
- 21 Matching Elements Using the equals and mismatch Operations
- 22 Perform an in-place merge for two vectors
- 23 Store a class object in a vector.
- 24 Storing Class Objects with overloaded operators in a Vector
- 25 The basic operation of a vector: size, push_back,
- 26 Use istream_iterator to read various data types
- 27 Use istream_iterator with the copy algorithm
- 28 Use pop_back() and empty().
- 29 Using clear()
- 30 Using Other Search functions
- 31 Vector Capacity vs size
- 32 Vector Init Array
- 33 Vector: Insert Erase Sort
- 34 Work with heaps: make_heap from vector
Access a vector using an iterator.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
int i;
for(i = 0; i <10; i++)
v.push_back(i);
for(i = 0; i <10; i++)
cout << v[ i ] << " ";
cout << endl;
vector<int>::iterator p = v.begin();
while(p != v.end()) {
cout << *p << " ";
p++;
}
return 0;
}
Accessing a Vector Through an Iterator
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<char> vectorObject(10);
vector<char>::iterator p; // create an iterator
int i;
p = vectorObject.begin();
i = 0;
while(p != vectorObject.end()) {
*p = i + "a";
p++;
i++;
}
cout << "Original contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << "\n\n";
// change contents of vector
p = vectorObject.begin();
while(p != vectorObject.end()) {
*p = toupper(*p);
p++;
}
// display contents of vector
cout << "Modified Contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << endl;
return 0;
}
Access the elements of a vector through an iterator.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject(10);
vector<int>::iterator p;
int i;
p = vectorObject.begin();
i = 0;
while(p != vectorObject.end()) {
*p = i;
p++;
i++;
}
cout << "Original contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << "\n\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
*p = *p * 2; // change contents of vector
p++;
}
cout << "Modified Contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " "; // display contents of vector
p++;
}
cout << endl;
return 0;
}
Create permutations based on vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<char> vectorObject;
int i;
for(i = 0; i < 3; i++)
vectorObject.push_back("A"+i);
cout << "All permutations of 3 characters:\n";
do {
for(i = 0; i < vectorObject.size(); i++)
cout << vectorObject[ i ];
cout << endl;
} while(next_permutation(vectorObject.begin(), vectorObject.end()));
return 0;
}
Creating and Resizing Vectors
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> intVector;
template<class T, class A>
void ShowVector(const vector<T, A>& v);
int main()
{
intVector intValueVector;
cout << "intValueVector" << "\n";
ShowVector(intValueVector);
intVector intValueVector2(3);
cout << "intValueVector2(3)" << "\n";
ShowVector(intValueVector2);
intValueVector2.resize(5, 100);
cout << "intValueVector2 after resize(5, 100)\n";
ShowVector(intValueVector2);
intValueVector2.reserve(10);
cout << "intValueVector2 after reserve(10)\n";
ShowVector(intValueVector2);
return 0;
}
template<class T, class A>
void ShowVector(const vector<T, A>& v)
{
cout << "max_size() = " << v.max_size();
cout << "\tsize() = " << v.size();
cout << "\t" << (v.empty()? "empty": "not empty");
cout << "\tcapacity() = " << v.capacity();
cout << "\n\n";
}
Demonstrate accumulate() in vector
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> vectorObject(5);
int i, total;
for(i = 0; i < 5; i++)
vectorObject[ i ] = i;
total = accumulate(vectorObject.begin(), vectorObject.end(), 0);
cout << "Summation of vectorObject is: " << total;
return 0;
}
Demonstrate adjacent_difference() in vector
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> vectorObject(10), vectorObject2(10);
int i;
for(i = 0; i <10; i++)
vectorObject[i] = i*2;
cout << "Original sequence: ";
for(i = 0; i <10; i++)
cout << vectorObject[ i ] << " ";
cout << endl;
adjacent_difference(vectorObject.begin(), vectorObject.end(), vectorObject2.begin());
cout << "Resulting sequence: ";
for(i = 0; i < 10; i++)
cout << vectorObject2[ i ] << " ";
return 0;
}
Demonstrate allocator"s max_size() fucntion in vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<short int>::allocator_type vectorShortIntAllocator;
vector<int>::allocator_type vectorIntAllocator;
vector<long int>::allocator_type vectorLongAllocator;
vector<float>::allocator_type vectorFloatAllocator;
vector<double>::allocator_type vectorDoubleAllocator;
cout << "Here are the number of object that can be allocated.\n";
cout << "short integers: ";
cout << vectorShortIntAllocator.max_size() << endl;
cout << "integers: ";
cout << vectorIntAllocator.max_size() << endl;
cout << "long integers: ";
cout << vectorLongAllocator.max_size() << endl;
cout << "floats: ";
cout << vectorFloatAllocator.max_size() << endl;
cout << "doubles: ";
cout << vectorDoubleAllocator.max_size() << endl;
return 0;
}
Demonstrate count and count_if.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool even(int x)
{
return !(x%2);
}
int main()
{
vector<int> vectorObject;
int i;
for(i = 0; i <20; i++) {
if(i%2) vectorObject.push_back(1);
else vectorObject.push_back(2);
}
cout << "Sequence: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ] << " ";
cout << endl;
int n;
n = count(vectorObject.begin(), vectorObject.end(), 1);
cout << n << " elements are 1\n";
n = count_if(vectorObject.begin(), vectorObject.end(), even);
cout << n << " elements are even.\n";
return 0;
}
Demonstrate count_if().
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isEven(int i)
{
return !(i%2);
}
int main()
{
vector<int> vectorObject;
int i;
for(i =1; i < 20; i++)
vectorObject.push_back(i);
cout << "Sequence:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
i = count_if(vectorObject.begin(), vectorObject.end(), isEven);
cout << i << " numbers are evenly divisible by 2.\n";
return 0;
}
Demonstrate count() in vector
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> vectorObject;
int i;
for(i = 0; i < 10; i++) {
if(rand() % 2)
vectorObject.push_back(true);
else
vectorObject.push_back(false);
}
cout << "Sequence:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << boolalpha << vectorObject[i] << " ";
cout << endl;
i = count(vectorObject.begin(), vectorObject.end(), true);
cout << i << " elements are true.\n";
return 0;
}
Demonstrate inner_product() in vector
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> vectorObject1(5), vectorObject2(5);
int i, total;
for(i = 0; i < 5; i++)
vectorObject1[ i ] = i;
for(i = 0; i < 5; i++)
vectorObject2[ i ] = i+2;
total = inner_product(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), 0);
cout << "Inner product is: " << total;
return 0;
}
Demonstrate insert and erase.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v(5, 1);
int i;
cout << "Size = " << v.size() << endl;
cout << "Original contents:\n";
for(i = 0; i <v.size(); i++)
cout << v[ i ] << " ";
cout << endl << endl;
vector<int>::iterator p = v.begin();
p += 2;
v.insert(p, 10, 9);
cout << "Size after insert = " << v.size() << endl;
cout << "Contents after insert:\n";
for(i = 0; i <v.size(); i++)
cout << v[ i ] << " ";
cout << endl << endl;
p = v.begin();
p += 2;
v.erase(p, p+10);
cout << "Size after erase = " << v.size() << endl;
cout << "Contents after erase:\n";
for(i = 0; i <v.size(); i++)
cout << v[ i ] << " ";
cout << endl;
return 0;
}
Demonstrate insert_iterator in vector
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject;
vector<int>::iterator itr;
int i;
for(i = 0; i <5; i++)
vectorObject.push_back(i);
cout << "Original array: ";
itr = vectorObject.begin();
while(itr != vectorObject.end())
cout << *itr++ << " ";
cout << endl;
itr = vectorObject.begin();
itr += 2; // point to element 2
// create insert_iterator to element 2
insert_iterator<vector<int> > i_itr(vectorObject, itr);
*i_itr++ = 100; // insert rather than overwrite
*i_itr++ = 200;
cout << "Array after insertion: ";
itr = vectorObject.begin();
while(itr != vectorObject.end())
cout << *itr++ << " ";
return 0;
}
Demonstrate partial_sum() in Vector
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector<int> vectorObject(5), vectorObject2(5);
int i;
for(i = 0; i < 5; i++)
vectorObject[i] = i;
cout << "Original sequence: ";
for(i = 0; i <5; i++)
cout << vectorObject[i] << " ";
cout << endl;
partial_sum(vectorObject.begin(), vectorObject.end(), vectorObject2.begin());
cout << "Resulting sequence: ";
for(i = 0; i < 5; i++)
cout << vectorObject2[ i ] << " ";
return 0;
}
Demonstrate remove_copy and replace_copy.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
char str[] = "www.java2s.com";
vector<char> vectorObject, vectorObject2(30);
unsigned int i;
for(i = 0; str[i]; i++)
vectorObject.push_back(str[i]);
cout << "Input sequence:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i];
cout << endl;
remove_copy(vectorObject.begin(), vectorObject.end(), vectorObject2.begin(), " ");
cout << "Result after removing spaces:\n";
for(i = 0; i <vectorObject2.size(); i++)
cout << vectorObject2[i];
cout << endl << endl;
cout << "Input sequence:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i];
cout << endl;
// replace spaces with colons
replace_copy(vectorObject.begin(), vectorObject.end(), vectorObject2.begin(), " ", ":");
cout << "Result after repacing spaces with colons:\n";
for(i = 0; i <vectorObject2.size(); i++)
cout << vectorObject2[i];
cout << endl;
return 0;
}
Demonstrate remove_copy in Vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v, vectorObject2(20);
int i;
for(i = 0; i <20; i++) {
if(i%2) v.push_back(1);
else v.push_back(2);
}
cout << "Sequence: ";
for(i = 0; i <v.size(); i++)
cout << v[ i ] << " ";
cout << endl;
remove_copy(v.begin(), v.end(), vectorObject2.begin(), 1);
cout << "Result: ";
for(i = 0; i <vectorObject2.size(); i++)
cout << vectorObject2[ i ] << " ";
cout << endl << endl;
return 0;
}
Demonstrate reverse in vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vectorObject;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back(i);
cout << "Initial: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
reverse(vectorObject.begin(), vectorObject.end());
cout << "Reversed: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
return 0;
}
Demonstrating the four ways that vectors can be created.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> a; // empty
vector<char> b(5, "X"); // size and initialize
vector<char> c(b); // initialize with another vector
int i;
for(i = 0; i <5; i++)
a.push_back("A"+i);
// create a vector from a range
vector<char> d(a.begin()+1, a.end()-1);
for(i = 0; i <a.size(); i++)
cout << "a[" << i << "]: " << a[i] << " ";
cout << "\n\n";
for(i = 0; i <b.size(); i++)
cout << "b[" << i << "]: " << b[i] << " ";
cout << "\n\n";
for(i = 0; i <c.size(); i++)
cout << "c[" << i << "]: " << c[i] << " ";
cout << "\n\n";
for(i = 0; i <d.size(); i++)
cout << "d[" << i << "]: " << d[i] << " ";
return 0;
}
end() in vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back(i);
cout << "Vector printed forwards:\n";
vector<int>::iterator p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << "\n";
cout << "Vector printed backwards:\n";
p = vectorObject.end();
while(p != vectorObject.begin()) {
p--; // decrement iterator before using
cout << *p << " ";
}
return 0;
}
Matching Elements Using the equals and mismatch Operations
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor);
int main()
{
typedef vector<int>::iterator VectorInterator;
vector<int> intValueVector(4);
intValueVector[0] = 1;
intValueVector[1] = 2;
intValueVector[2] = 3;
intValueVector[3] = 4;
vector<int> intValueVector2(3);
intValueVector2[0] = 1;
intValueVector2[1] = 2;
intValueVector2[2] = 3;
VectorInterator first1 = intValueVector.begin();
VectorInterator last1 = intValueVector.end();
VectorInterator first2 = intValueVector2.begin();
if (equal(first1, last1, first2))
cout << "intValueVector == intValueVector2\n";
else
cout << "intValueVector != intValueVector2\n";
pair<VectorInterator, VectorInterator> pi = mismatch(first1, last1, first2);
cout << "First mismatch element in intValueVector = ";
ShowElement(intValueVector, pi.first);
cout << "\n";
cout << "First mismatch element in intValueVector2 = ";
ShowElement(intValueVector2, pi.second);
cout << "\n";
return 0;
}
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor)
{
if (itor != c.end())
{
if (itor != c.begin())
cout << *itor << "\tthe previous element is " << *(itor - 1);
else
cout << "first";
}
else
cout << "last";
}
Perform an in-place merge for two vectors
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<char> vectorObject1;
int i;
for(i = 0; i <26; i+=2)
vectorObject1.push_back("A"+i);
for(i = 0; i <26; i+=2)
vectorObject1.push_back("B"+i);
cout << "Original contents of vectorObject1:\n";
for(i = 0; i <vectorObject1.size(); i++)
cout << vectorObject1[ i ];
cout << endl;
inplace_merge(vectorObject1.begin(), vectorObject1.begin()+13, vectorObject1.end());
cout << "Result of merge:\n";
for(i = 0; i <vectorObject1.size(); i++)
cout << vectorObject1[ i ];
return 0;
}
Store a class object in a vector.
#include <iostream>
#include <vector>
using namespace std;
class Demo {
double d;
public:
Demo() {
d = 0.0;
}
Demo(double x) {
d = x;
}
Demo &operator=(double x) {
d = x; return *this;
}
double getd() {
return d;
}
};
bool operator<(Demo a, Demo b)
{
return a.getd() < b.getd();
}
bool operator==(Demo a, Demo b)
{
return a.getd() == b.getd();
}
int main()
{
vector<Demo> v;
int i;
for(i = 0; i <10; i++)
v.push_back(Demo(i/3.0));
for(i = 0; i <v.size(); i++)
cout << v[ i ].getd() << " ";
cout << endl;
for(i = 0; i <v.size(); i++)
v[ i ] = v[ i ].getd() * 2.1;
for(i = 0; i <v.size(); i++)
cout << v[ i ].getd() << " ";
return 0;
}
Storing Class Objects with overloaded operators in a Vector
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class IntValueClass {
int temp;
public:
IntValueClass() {
temp = 0;
}
IntValueClass(int x) {
temp = x;
}
IntValueClass &operator=(int x) {
temp = x; return *this;
}
double get_temp() {
return temp;
}
};
bool operator<(IntValueClass a, IntValueClass b)
{
return a.get_temp() < b.get_temp();
}
bool operator==(IntValueClass a, IntValueClass b)
{
return a.get_temp() == b.get_temp();
}
int main()
{
vector<IntValueClass> v;
unsigned int i;
for(i=0; i<7; i++)
v.push_back(IntValueClass(60 + rand()%30));
cout << "Fahrenheit temperatures:\n";
for(i=0; i<v.size(); i++)
cout << v[i].get_temp() << " ";
cout << endl;
for(i=0; i<v.size(); i++)
v[i] = (int)(v[i].get_temp()-32) * 5/9 ;
cout << "Centigrade temperatures:\n";
for(i=0; i<v.size(); i++)
cout << v[i].get_temp() << " ";
return 0;
}
The basic operation of a vector: size, push_back,
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<char> vectorObject(10); // create a vector of length 10
unsigned int i;
cout << "Size = " << vectorObject.size() << endl;
for(i=0; i<10; i++)
vectorObject[i] = i + "a";
cout << "Current Contents:\n";
for(i=0; i<vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << "\n\n";
cout << "Expanding vector\n";
for(i=0; i<10; i++)
vectorObject.push_back(i + 10 + "a");
cout << "Size now = " << vectorObject.size() << endl;
cout << "Current contents:\n";
for(i=0; i<vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << "\n\n";
for(i=0; i<vectorObject.size(); i++)
vectorObject[i] = toupper(vectorObject[i]);
cout << "Modified Contents:\n";
for(i=0; i<vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
return 0;
}
Use istream_iterator to read various data types
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main()
{
int i;
double d;
string str;
vector<int> vi;
vector<double> vd;
vector<string> vs;
cout << "Enter some integers, enter 0 to stop.";
istream_iterator<int> int_itr(cin);
do {
i = *int_itr;
if(i != 0) {
vi.push_back(i);
++int_itr;
}
} while (i != 0);
cout << "Enter some doubles, enter 0 to stop.";
istream_iterator<double> double_itr(cin);
do {
d = *double_itr;
if(d != 0.0) {
vd.push_back(d);
++double_itr;
}
} while (d != 0.0);
cout << "Enter some strings, enter "quit" to stop.";
istream_iterator<string> string_itr(cin);
do {
str = *string_itr;
if(str != "quit") {
vs.push_back(str);
++string_itr;
}
} while (str != "quit");
cout << "Here is what you entered:\n";
for(i = 0; i <vi.size(); i++)
cout << vi[ i ] << " ";
cout << endl;
for(i = 0; i <vd.size(); i++)
cout << vd[ i ] << " ";
cout << endl;
for(i = 0; i <vs.size(); i++)
cout << vs[ i ] << " ";
return 0;
}
Use istream_iterator with the copy algorithm
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int i;
vector<int> v(5);
cout << "Enter 5 integers: \n";
istream_iterator<int> int_itr(cin);
copy(int_itr, istream_iterator<int>(), v.begin());
cout << "Here are the values you entered: ";
for(i = 0; i <v.size(); i++) cout << v[ i ] << " ";
return 0;
}
Use pop_back() and empty().
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> vectorObject;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back(i + "A");
cout << "Vector"s original contents:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << "\n\n";
do {
vectorObject.pop_back(); // remove an element from the end
cout << "Vector now contains:\n";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
} while(!vectorObject.empty());
return 0;
}
Using clear()
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back(i);
cout << "Initial contents of vectorObject: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
cout << "Initial size of vectorObject: ";
cout << vectorObject.size() << endl;
cout << "Initial capacity of vectorObject: ";
cout << vectorObject.capacity() << "\n\n";
vectorObject.clear();
cout << "Size of vectorObject after clear(): ";
cout << vectorObject.size() << endl;
cout << "Capacity of vectorObject after clear(): ";
cout << vectorObject.capacity() << "\n\n";
// put new values in vectorObject
for(i = 0; i <10; i++)
vectorObject.push_back(i*10);
cout << "New contents of vectorObject: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;
cout << "Size of vectorObject after adding new elements: ";
cout << vectorObject.size() << endl;
cout << "Capacity of vectorObject after adding new elements: ";
cout << vectorObject.capacity() << "\n\n";
return 0;
}
Using Other Search functions
#include <iostream>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
using namespace std;
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor);
int main()
{
typedef vector<int>::iterator VectorInterator;
vector<int> intValueVector(10);
intValueVector[0] = 0;
intValueVector[1] = 1;
intValueVector[2] = 1;
intValueVector[3] = 2;
intValueVector[4] = 3;
intValueVector[5] = 4;
intValueVector[6] = 1;
intValueVector[7] = 2;
intValueVector[8] = 3;
intValueVector[9] = 5;
vector<int> intValueVector2(3);
intValueVector2[0] = 1;
intValueVector2[1] = 2;
intValueVector2[2] = 3;
VectorInterator first1 = intValueVector.begin();
VectorInterator last1 = intValueVector.end();
VectorInterator first2 = intValueVector2.begin();
VectorInterator last2 = intValueVector2.end();
VectorInterator retItor = find_first_of(first1, last1, first2, last2);
cout << "find_first_of(first1, last1, first2, last2) = ";
ShowElement(intValueVector, retItor);
cout << "\n";
retItor = search(first1, last1, first2, last2);
cout << "search(first1, last1, first2, last2) = ";
ShowElement(intValueVector, retItor);
cout << "\n";
retItor = find_end(first1, last1, first2, last2);
cout << "find_end(first1, last1, first2, last2) = ";
ShowElement(intValueVector, retItor);
cout << "\n";
return 0;
}
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor)
{
if (itor != c.end())
{
if (itor != c.begin())
cout << *itor << "\tthe previous element is " << *(itor - 1);
else
cout << "first";
}
else
cout << "last";
}
Vector Capacity vs size
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> vectorObject(10);
cout << "Initial size: " << vectorObject.size() << endl;
cout << "Initial capacity: " << vectorObject.capacity();
cout << "\n\n";
vectorObject.reserve(20); // reserve more space
cout << "Size after reserve: " << vectorObject.size() << endl;
cout << "Capacity after reserve: " << vectorObject.capacity();
cout << "\n\n";
return 0;
}
Vector Init Array
#include <iostream>
#include <vector>
using namespace std;
void print( vector<int> );
int main()
{
int data[] = {111, 112, 123, 134};
int size = sizeof( data ) / sizeof( data[0] );
vector<int> vec( data, &data[ size ] );
print( vec );
return 0;
}
void print( vector<int> vec ) {
vector<int>::iterator p = vec.begin();
while ( p < vec.end() )
cout << *p++ << " ";
cout << endl;
}
Vector: Insert Erase Sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print( vector<int> );
int main()
{
vector<int> vectorObject(5);
print( vectorObject );
vector<int>::iterator p = vectorObject.begin();
cout << *p << endl;
vectorObject.insert( vectorObject.begin(), 1 );
print( vectorObject );
cout << *p << endl;
p = vectorObject.begin();
cout << *p << endl;
vectorObject.erase( vectorObject.begin() );
print( vectorObject );
vectorObject.insert( vectorObject.begin() + 1, 1 );
print( vectorObject );
vectorObject.erase( vectorObject.begin() + 2 );
print( vectorObject );
vectorObject.insert( vectorObject.end(), 3 );
print( vectorObject );
vectorObject.erase( vectorObject.end() - 1 );
print( vectorObject );
vectorObject.insert( vectorObject.begin() + 3, 1 );
print( vectorObject );
vectorObject.erase( find( vectorObject.begin(), vectorObject.end(), 6 ) );
print( vectorObject );
vectorObject.insert( vectorObject.begin() + 1, 3 );
vectorObject.insert( vectorObject.begin() + 5, 3 );
print( vectorObject );
vectorObject.erase( find( vectorObject.begin(), vectorObject.end(), 3 ) );
vectorObject.erase( find( vectorObject.begin(), vectorObject.end(), 3 ) );
print( vectorObject );
vectorObject[0] = 231;
vectorObject[1] = 21;
vectorObject[2] = 116;
vectorObject[3] = 45;
vectorObject[4] = 116;
print( vectorObject );
sort( vectorObject.begin(), vectorObject.end() );
print( vectorObject );
return 0;
}
void print( vector<int> v ) {
vector<int>::iterator p = v.begin();
while ( p != v.end() )
cout << *p++ << " ";
cout << endl;
}
Work with heaps: make_heap from vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<char> vectorObject;
int i;
for(i = 0; i <20; i+=2)
vectorObject.push_back("A"+i);
cout << "Sequence before building heap:";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ] << " ";
cout << endl;
make_heap(vectorObject.begin(), vectorObject.end());
cout << "Sequence after building heap:";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ] << " ";
cout << endl;
vectorObject.push_back("H");
push_heap(vectorObject.begin(), vectorObject.end());
cout << "Sequence after pushing onto heap:";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ] << " ";
cout << endl;
pop_heap(vectorObject.begin(), vectorObject.end());
cout << "Sequence after poping from heap:";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[ i ] << " ";
cout << endl;
return 0;
}