1 Get container last element
------use back or rbegin to obtain
Back, Rbegin have constant and reference two forms of
std::vector<int> Myvector;
Myvector.back () =3;
Std::vector<int>::reverse_iterator Tailiter;
Tailiter=myvector.rbegin ();
*tailiter=3
2 Delete an element
you need to remove elements from a location, you should use the iterator traversal, and you should not use the at (i) way to traverse, because when you delete an element, it is deleted according to the iterator position.
When you delete an element, the return value is: The location of the first element after the deleted element
#include <vector>
#include <iostream>
int main ()
{
using namespace std;
Vector <int> v1;
Vector <int>::iterator Iter;
V1.push_back (ten);
V1.push_back (a);
V1.push_back (a);
V1.push_back ();
V1.push_back (m);
cout << "v1 =";
for (Iter = V1.begin (); Iter!= v1.end (); iter++)
cout << "" << *iter;
cout << Endl;
V1.erase (V1.begin ());
cout << "v1 =";
for (Iter = V1.begin (); Iter!= v1.end (); iter++)
cout << "" << *iter;
cout << Endl;
V1.erase (V1.begin () + 1, v1.begin () + 3);
cout << "v1 =";
for (Iter = V1.begin (); Iter!= v1.end (); iter++)
cout << "" << *iter;
cout << Endl;
}
Output:
V1 =
V1 =
V1 = 20 50 (PDF)
32 vector is easy to assign, you cannot assign values by =
instead, you should use traversal or assign functions to assign values
Delnode.vectornode is delpositionvector with the same type container
//Vector two container cannot be directly assigned value can be assigned by traversing each element, or you can use assign assignment
Vectornode Delnode;
Delnode.numberoffenkuai=nselect;
Node nodetemp;
Error Assignment Method:
This is the wrong way of assigning
delnode.vectornode=delpositionvector;
Correct assignment method one: Traverse
for (int i=0;i<delpositionvector.size (); i++)
{
nodetemp=delpositionvector.at (i);
DelNode.vectorNode.push_back (nodetemp);
Correct assignment Method Two: Assign function
DelNode.vectorNode.assign (Delpositionvector.begin (), Delpositionvector.end ());
4 inserting container elements at the specified iterator position
when an element is inserted, the return value is the location of the inserted element, and the element that was originally in this position is moved back in sequence
Iterator Insert (
iterator _where,
const type& _val
);
void Insert (
iterator _where,
size_type _count,
const type& _val
);
Template<class inputiterator>
void Insert (
iterator _where,
inputiterator _first,
Inputiterator _last
);
5 Updating an element in a container
one way to do this is to search for the element position, add the updated element in this location, and delete the original element
Or search for this element, delete this element, and add a new element to the element position
application Example:
DelNode.vectorNode.assign (Delpositionvector.begin (), Delpositionvector.end ());
BOOL Binsert=false;
Std::vector <vectornode>::iterator iter;
For (Iter=g_delvector.begin (); Iter!=g_delvector.end (); iter++)
{
if (*iter). Numberoffenkuai==nselect)
{
binsert=true;
G_delvector.erase (ITER);
G_delvector.insert (Iter,delnode);
Iter=g_delvector.insert (Iter,delnode);
Iter=g_delvector.erase (iter+1);
iter--;
break;
}
}
if (!binsert)
{
g_delvector.push_back (delnode);
}
6 push_back or pop an element, the iterator will fail to regain
Vectors in the STL are not associative containers, and when a new element is inserted, the original iterator is invalidated.
Std::vector<int> Vnum;
Vnum.push_back (1);
Vnum.push_back (3);
Vnum.push_back (5);
Std::vector<int>::iterator pIt = Vnum.begin ();
Std::cout << "Before insert a new number:" << *pit << Std::endl;
Vnum.push_back (7);
Std::cout << "After insert a new number:" << *pit << Std::endl; Oh! No!
Note that in the last sentence, a crash occurs when the last sentence is run, and an iterator accesses the error. After inserting the element, you want to regain the iterator.
For a relational container such as map, the original iterator still works when the new element is inserted.
Examples are as follows:
Std::map<int, int> mnum;
Mnum[0] = 0;
MNUM[1] = 1;
MNUM[2] = 2;
Std::map<int, Int>::iterator pIt = Mnum.begin ();
Std::cout << "Before insert a new number: (" << pit->first << "," << pit->second << " ) "<< Std::endl;
MNUM[3] = 3;
Std::cout << "After insert a new number: (" << pit->first << "," << pit->second << ") "<< Std::endl; Ok!
7 Merge two order containers
Std::vector<line>::iterator i1 = V1.begin (), i2 = V2.begin ();
while (I1!= v1.end () && i2!= v2.end ())
{
if (I1->index = = I2->index)
{line
t = {I1->ind Ex, I1->value1, i2->value2}
v3.push_back (t);
++I1;
++i2;
}
else if (I1->index > I2->index)
{
i2->value1 = 0;
V3.push_back (*I2);
++i2;
}
else
{
i1->value2 = 0;
V3.push_back (*I1);
++i1
}
}
while (I1!= v1.end ())
v3.push_back (* (i1++));
while (I2!= v2.end ())
v3.push_back (* (i2++));
9 sort
Alg_sort.cpp//compile with:/EHsc #include <vector> #include <algorithm> #include <functional>
For greater<int> () #include <iostream>//return whether the A/greater than the second
BOOL Udgreater (int elem1, int elem2) {return elem1 > elem2;
int main () {using namespace std;
Vector <int> v1;
Vector <int>::iterator Iter1;
int i;
for (i = 0; I <= 5; i++) {V1.push_back (2 * i);
} int II;
for (ii = 0; II <= 5; ii++) {V1.push_back (2 * II + 1);
} cout << "Original vector v1 = ("; for (Iter1 = V1.begin (); Iter1!= v1.end ();
iter1++) cout << *iter1 << "";
cout << ")" << Endl;
Sort (V1.begin (), V1.end ());
cout << "Sorted vector v1 = ("; for (Iter1 = V1.begin (); Iter1!= v1.end ();
iter1++) cout << *iter1 << ""; cout << ")" << endL To the sort in descending order.
Specify binary predicate sort (V1.begin (), V1.end (), greater<int> ());
cout << "resorted (greater) vector V1 = ("; for (Iter1 = V1.begin (); Iter1!= v1.end ();
iter1++) cout << *iter1 << "";
cout << ")" << Endl;
A user-defined (UD) binary predicate can also be used sort (v1.begin (), V1.end (), udgreater);
cout << "resorted (udgreater) Vector v1 = ("; for (Iter1 = V1.begin (); Iter1!= v1.end ();
iter1++) cout << *iter1 << "";
cout << ")" << Endl;
}
Original Vector v1 = (0 2 4 6 8 1 3 5 7 9)
Sorted Vector v1 = (0 1 2 3 4 5 6 7 8 9)
resorted (grea ter) Vector v1 = (9 8 7 6 5 4 3 2 1 0)
resorted (udgreater) Vector v1 = (11 10 9 8 7 6 5 4 3 2 1)
10 Clear all elements
11 Calendar Times
Vector<item_check>::iterator Iter=m_itemvector.begin ();
For (I=0;iter!=m_itemvector.end (); iter++,i++)
{
if (iter->flag==-1)
{break
;
}
iter->flag=1;
}
Vector<item_check>::iterator Iter=m_itemvector.begin ();
For (I=0;iter!=m_itemvector.end (); iter++,i++)//Cancel all
{
iter->flag=0;
} First
12 Delete Qualifying Items
int currentcount= (int) m_itemvector.size ();
for (int i=0;i<currentcount;i++)
{
if (m_itemvector.at (i). Flag==1)
{
m_itemvector.erase (m_ Itemvector.begin () +i);
DeleteItem (i);
This->invalidate ();
currentcount--;
i--; After removing position I, it is necessary to determine if position I is eligible, so I--。
}
}
13 positive sequence traversal and then reverse sequence traversal
Vector<item_check>::iterator Iter=m_itemvector.begin ();
For (I=0;iter!=m_itemvector.end (); iter++,i++)//Cancel all
{
iter->flag=0;
}
First for (;i>0;) //from the back to set ITER back to begin () and then the problem will occur
{
iter--;//end, just fall back to begin ()
i--;
iter->flag=1;
14 Finding in vectors
#include <vector>
#include <algorithm>//Find
using namespace std in vector;
Vector<int> L;
L.push_back (1);
L.push_back (2);
L.push_back (3);
L.push_back (4);
L.push_back (5);
Vector<int>::iterator result = Find (L.begin (), L.end (), 3); Find 3
if (result = = L.end ())//No
cout << "no" << Endl;
else//Find
cout << "Yes" << Endl;