In C ++, the string length can be obtained through string. Length (), which is not so easy for an array.
For example, an array of int type:
Int A [] = {1, 2, 3, 5, 6, 9}; how to obtain it?
By using the special usage of sizeof, we all know that sizeof () is the size of the space occupied, so we can: int length = sizeof (a)/sizeof (INT ); to obtain the number of elements in array.
1. Storage and output of Vector Data:
# Include <stdio. h>
# Include <vector>
# Include <iostream>
Using namespace STD;
Void main ()
{
Int I = 0;
Vector <int> V;
For (I = 0; I <10; I ++)
{
V. push_back (I); // store the elements one by one into the vector.
}
/* V. Clear () */clears the stored data
For (I = 0; I <v. Size (); I ++) // v. Size () indicates the number of elements stored in a vector.
{
Cout <V [I] <"; // display each element
}
Cont <Endl;
}
Note: You can also use v. Begin () and V. End () to obtain the pointer positions of the starting and ending element addresses of a vector. You can also do this:
Vector <int>: iterator ITER;/* iterator abstracts most of the basic features of a pointer */
For (iter = V. Begin (); iter! = V. End (); ITER ++)
{
Cout <* ITER <Endl;
}
2. Two-dimensional vector definition.
1) define a 10 vector element with a value of 1-10 for each vector.
# Include <stdio. h>
# Include <vector>
# Include <iostream>
Using namespace STD;
Void main ()
{
Int I = 0, j = 0;
// Define a two-dimensional dynamic array with 10 rows. Each row uses a vector to store the data of this row.
Therefore, the length of each row can be changed. Vector <int> (0) is used to initialize the vector. Otherwise, elements cannot be stored in the vector.
Vector <vector <int> array (10, vector <int> (0 ));
For (j = 0; j <10; j ++)
{
For (I = 0; I <9; I ++)
{
Array [J]. push_back (I );
}
}
For (j = 0; j <10; j ++)
{
For (I = 0; I <array [J]. Size (); I ++)
{
Cout <array [J] [I] <"";
}
Cout <Endl;
}
}
2) define a row and column as an array of changes.
# Include <stdio. h>
# Include <vector>
# Include <iostream>
Using namespace STD;
Void main ()
{
Int I = 0, j = 0;
Vector <vector <int> array;
Vector <int> line;
For (j = 0; j <10; j ++)
{
Array. push_back (line); // You Need to initialize each vector. Otherwise, elements cannot be saved.
For (I = 0; I <9; I ++)
{
Array [J]. push_back (I );
}
}
For (j = 0; j <10; j ++)
{
For (I = 0; I <array [J]. Size (); I ++)
{
Cout <array [J] [I] <"";
}
Cout <Endl;
}
}
Specify an element using vettor erase
# Include "iostream"
# Include "vector"
Using namespace STD;
Int main ()
{
Vector <int> arr;
Arr. push_back (6 );
Arr. push_back (8 );
Arr. push_back (3 );
Arr. push_back (8 );
For (vector <int >:: iterator it = arr. Begin (); it! = Arr. End ();)
{
If (* It = 8)
{
It = arr. Erase (it );
}
Else
{
++ It;
}
}
Cout <"After remove 8:/N ";
For (vector <int >:: iterator it = arr. Begin (); It <arr. End (); ++ it)
{
Cout <* It <"";
}
Cout <Endl;
}
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <vector>
Using namespace STD;
Void main ()
{
Int iarray [] = {, 6, 8 };
Vector <int> ivector (iarray, iarray + sizeof (iarray)/sizeof (INT ));
Int iarray1 [] = {6, 6 };
Vector <int> ivector1 (iarray1, iarray1 + sizeof (iarray1)/sizeof (INT ));
Int iarray2 [] = {5, 6 };
Vector <int> ivector2 (iarray2, iarray2 + sizeof (iarray2)/sizeof (INT ));
Int iarray3 [] = {, 7 };
Vector <int> ivector3 (iarray3, iarray3 + sizeof (iarray3)/sizeof (INT ));
// Find the first element with the same adjacent element values in the ivector.
Cout <* adjacent_find (ivector. Begin (), ivector. End () <Endl;
// Find the number of elements in the ivector whose element value is 6
Cout <count (ivector. Begin (), ivector. End (), 6) <Endl;
// Find the number of elements smaller than 7 in the ivector
Cout <count_if (ivector. Begin (), ivector. End (), bind2nd (less <int> (), 7) <Endl;
// Locate the element where the first element with the element value of 4 in the ivector is located
Cout <* Find (ivector. Begin (), ivector. End (), 4) <Endl;
// Locate the element where the first element greater than 2 in the ivector is located
Cout <* find_if (ivector. Begin (), ivector. End (), bind2nd (greater <int> (), 2 ))
<Endl;
// Find the last position of ivector1 In the subsequence of ivector, and then the elements in the next three positions.
Cout <* (find_end (ivector. Begin (), ivector. End (), ivector1.begin (),
Ivector1.end () + 3) <Endl;
// Find the first position in the subsequence ivector1 of the ivector, and then the elements in the next three positions.
Cout <* (find_first_of (ivector. Begin (), ivector. End (), ivector1.begin (),
Ivector1.end () + 3) <Endl;
// The starting position element of the subsequence ivector2 in the ivector
Cout <* search (ivector. Begin (), ivector. End (), ivector2.begin (), ivector2.end ())
<Endl;
// Find the starting position element with three six consecutive times
Cout <* search_n (ivector. Begin (), ivector. End (), 3, 6, direction _to <int> () <Endl;
// Determine whether the ivector and ivector3 are equal to each other (0 is false, 1 is true)
Cout <equal (ivector. Begin (), ivector. End (), ivector3.begin () <Endl;
// Find the location where ivector3 does not match the vertex in the ivector.
Pair <int *, int *> result = mismatch (ivector. Begin (), ivector. End (), ivector3.begin ());
Cout <result. First-ivector. Begin () <Endl;
}
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <vector>
Using namespace STD;
Class even_by_two {// function object in the class definition form
Public:
Int operator () const
{Return _ x + = 2 ;}
PRIVATE:
Static int _ x;
};
Int even_by_two: _ x = 0; // static data member Initialization
Void main ()
{
Int iarray [] = {, 6, 8 };
Int iarray1 [] = {0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8 };
Vector <int> ivector (iarray, iarray + sizeof (iarray)/sizeof (INT ));
Vector <int> ivector1 (iarray + 6, iarray + 8 );
Vector <int> ivector2 (iarray1, iarray1 + sizeof (iarray1)/sizeof (INT ));
Ostream_iterator <int> output (cout, ""); // defines the stream iterator used to output data
// Iteratively traverse the ivector1 range and perform the even_by_two operation on each element
Generate (ivector1.begin (), ivector1.end (), even_by_two ());
Copy (ivector1.begin (), ivector1.end (), output );
Cout <Endl;
// Iteratively traverse the specified range (start point and length) of the ivector and perform even_by_two operations on each element
Generate_n (ivector. Begin (), 3, even_by_two ());
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Delete element 6
Remove (ivector. Begin (), ivector. End (), 6 );
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Delete element 6 (not actually deleted from the original sequence) and place the result in another Interval
Vector <int> ivector3 (12 );
Remove_copy (ivector. Begin (), ivector. End (), ivector3.begin (), 6 );
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
// Delete (not actually deleted from the original sequence) elements smaller than 6
Remove_if (ivector. Begin (), ivector. End (), bind2nd (less <int> (), 6 ));
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Delete the element smaller than 7 (not actually deleted from the original sequence) and place the result in another interval,
Remove_copy_if (ivector. Begin (), ivector. End (), ivector3.begin (),
Bind2nd (less <int> (), 7 ));
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
// Change all element values 6 to element value 3
Replace (ivector. Begin (), ivector. End (), 6, 3 );
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Change all element values 3 to element value 5. The result is placed in another interval.
Replace_copy (ivector. Begin (), ivector. End (), ivector3.begin (), 3, 5 );
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
// Change all element values smaller than 5 to element value 2
Replace_if (ivector. Begin (), ivector. End (), bind2nd (less <int> (), 5), 2 );
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Change all element values 8 to element value 9. The result is placed in another interval.
Replace_copy_if (ivector. Begin (), ivector. End (), ivector3.begin (),
Bind2nd (defaults _to <int> (), 8), 9 );
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
// Rearranging each element in reverse order
Reverse (ivector. Begin (), ivector. End ());
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Rearranges each element in reverse order, and the result is placed in another interval.
Reverse_copy (ivector. Begin (), ivector. End (), ivector3.begin ());
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
// Rotate (SWAP element) [first, middle), and [middle, end)
Rotate (ivector. Begin (), ivector. Begin () + 4, ivector. End ());
Copy (ivector. Begin (), ivector. End (), output );
Cout <Endl;
// Rotate (SWAP element) [first, middle], and [middle, end]. The result is placed in another interval,
Rotate_copy (ivector. Begin (), ivector. Begin () + 5, ivector. End (),
Ivector3.begin ());
Copy (ivector3.begin (), ivector3.end (), output );
Cout <Endl;
}
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <vector>
Using namespace STD;
Void main ()
{
Int iarray [] = };
Vector <int> ivector (iarray, iarray + sizeof (iarray)/sizeof (INT ));
// Find and output the maximum and minimum elements
Cout <* max_element (ivector. Begin (), ivector. End () <Endl;
Cout <* min_element (ivector. Begin (), ivector. End () <Endl;
// Sorts the ivector. Begin () + 4-ivector.begin () elements,
// Set it to the [ivector. Begin (), ivector. Begin () + 4] range. The remaining elements do not guarantee the original relative order.
Partial_sort (ivector. Begin (), ivector. Begin () + 3, ivector. End ());
Copy (ivector. Begin (), ivector. End (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Partial sorting and copying to another location
Vector <int> ivector1 (5 );
Partial_sort_copy (ivector. Begin (), ivector. End (), ivector1.begin (),
Ivector1.end ());
Copy (ivector1.begin (), ivector1.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Sort. The default value is incremental.
Sort (ivector. Begin (), ivector. End ());
Copy (ivector. Begin (), ivector. End (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Inserting the specified element into the interval does not affect the lowest and highest positions in the original sorting of the interval.
Cout <* lower_bound (ivector. Begin (), ivector. End (), 24) <Endl;
Cout <* upper_bound (ivector. Begin (), ivector. End (), 24) <Endl;
// For an ordered interval, you can use the binary search method to find an element.
Cout <binary_search (ivector. Begin (), ivector. End (), 33) <Endl;
Cout <binary_search (ivector. Begin (), ivector. End (), 34) <Endl;
// The next permutation and combination
Next_permutation (ivector. Begin (), ivector. End ());
Copy (ivector. Begin (), ivector. End (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// The Last permutation and combination
Prev_permutation (ivector. Begin (), ivector. End ());
Copy (ivector. Begin (), ivector. End (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Merge the two sequences ivector and ivector1 and put the result in ivector2.
Vector <int> ivector2 (13 );
Merge (ivector. Begin (), ivector. End (), ivector1.begin (), ivector1.end (),
Ivector2.begin ());
Copy (ivector2.begin (), ivector2.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Place an element smaller than * (ivector. Begin () + 5) to the left of the element
// Place the rest to the right of the element. It is not guaranteed to maintain the original relative position
Nth_element (ivector2.begin (), ivector2.begin () + 5, ivector2.end ());
Copy (ivector2.begin (), ivector2.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Sort and keep the original relative position
Stable_sort (ivector2.begin (), ivector2.end ());
Copy (ivector2.begin (), ivector2.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Find a subinterval for an ordered interval. Each element has the same value as a specific element.
Pair <vector <int >:: iterator, vector <int >:: iterator> pairite;
Pairite = pai_range (ivector2.begin (), ivector2.end (), 22 );
Cout <* (pairite. First) <Endl;
Cout <* (pairite. Second) <Endl;
// Merge two ordered sequences and then replace them locally
Int iarray3 [] = {1, 3, 5, 7, 2, 4, 6, 8 };
Vector <int> ivector3 (iarray3, iarray3 + sizeof (iarray3)/sizeof (INT ));
Inplace_merge (ivector3.begin (), ivector3.begin () + 4, ivector3.end ());
Copy (ivector3.begin (), ivector3.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Compare sequences ivector3 and ivector4 in alphabetical order
Int iarray4 [] = {1, 3, 5, 7, 1, 5, 9, 3 };
Vector <int> ivector4 (iarray4, iarray4 + sizeof (iarray4)/sizeof (INT ));
Cout <lexicographical_compare (ivector3.begin (), ivector3.end (),
Ivector4.begin (), ivector4.end () <Endl
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wanglibaocsde/archive/2011/01/12/6133020.aspx