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
MSDN Explanation:
Removes an element or a range of elements in a vector from specified positions.
|
iterator Erase (
_where
);
Iterator Erase (
_first,
_last
); |
Position of the element to is removed from the vector. _first
Position of the The ' the ' the ' the ' removed from the vector. _last
Position just beyond the last element removed from the vector.
An iterator that designates the the "the" I remaining beyond any elements removed, or a pointer to the "end of the" vector If no such element exists.
MSDN Example:
#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 ());
MSDN Example:
#include <vector>
#include <iostream>
int main ()
{
using namespace std;
Vector<int> v1, v2, v3;
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;
V2.assign (V1.begin (), V1.end ());
cout << "v2 =";
for (iter = V2.begin (); Iter!= v2.end (); iter++)
cout << *iter << "";
cout << Endl;
V3.assign (7, 4);
cout << "v3 =";
for (iter = V3.begin (); Iter!= v3.end (); iter++)
cout << *iter << "";
cout << Endl;
}
Output
v1 =
V2 =
V3 = 4 4 4 4 4 4 4
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
MSDN: Explanations and examples
|
iterator Insert (
_where,
_val
);
void Inserts (
_where,
_count,
_val
);
Template<class inputiterator>
void Insert (
_where,
inputiterator _first ,
Inputiterator _last
); |
_where
The position in the vector where the this is inserted. _val
The value of the element being inserted into the vector. _count
The number of elements being inserted into the vector. _first
The position of the the ' the ' of the ' the ' is ' elements to be copied. _last
The position of the the ' the ' the ' the ' the ' beyond ' of the range of elements to be copied.
The I insert function returns an iterator this points to the position where the new element is inserted into The vector.
Any insertion operation can be expensive, and the vector Class for a discussion ofvector performance.
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
Example of CSDN Forum:
Vectors in the STL are not associative containers, and when a new element is inserted, the original iterator is invalidated.
Examples are as follows:
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++));
own Example:
Delete line std::vector<node> delposition; for (int i=0;i<g_delvector.size (); i++) {if (g_delvector.at (i). Numberoffenkuai==nselect) {delposition.assign (g
_delvector.at (i). Vectornode.begin (), g_delvector.at (i) vectornode.end ());
Break
}//Set underline underline std::vector<node> underlineposition; for (int i=0;i<g_underlinevector.size (); i++) {if (g_underlinevector.at (i). Numberoffenkuai==nselect) {UnderLin
Eposition.assign (g_underlinevector.at (i). Vectornode.begin (), g_underlinevector.at (i). Vectornode.end ());
Break
///merge strikethrough and underscore location node node;
Std::vector<node> eraseposition;
Std::vector<node>::iterator I1=delposition.begin (), I2=underlineposition.begin ();
while (I1!=delposition.end () &&i2!=underlineposition.end ()) {if (i1->nstart==i2->nend)//merge forward + + {
node.nstart=i2->nstart;
node.nend=i1->nend;
i1++;
i2++;
Eraseposition.push_back (node);
Continue } if (i1->nEnd==i2->nstart) {node.nstart=i1->nstart;
node.nend=i2->nend;
i1++;
i2++;
Eraseposition.push_back (node);
Continue
} if (I1->nend<i2->nstart) {node=*i1;
i1++;
Eraseposition.push_back (node);
Continue
} if (i1->nstart>i2->nend) {node=*i2;
i2++;
Eraseposition.push_back (node);
Continue
} while (I1!=delposition.end ()) eraseposition.push_back (* (i1++));
while (I2!=underlineposition.end ()) eraseposition.push_back (* (i2++));
Finishing the Erasepositon, making the place where the end and end are connected to merge Std::vector<node>::iterator Iter1,iter2;
For (Iter1=eraseposition.begin (); Iter1!=eraseposition.end ();)
{iter2=iter1+1;
if (Iter2!=eraseposition.end ()) {if (Iter1->nend==iter2->nstart) {iter1->nend=iter2->nend;
Iter1=eraseposition.erase (ITER2);
iter1--;
Continue
}} iter1++;
} truestring=_t ("");
CString mixstring; Getricheditctrl (). GetWindowText (Mixstring);
int nstart=0; For (Iter1=eraseposition.begin (); Iter1!=eraseposition.end (); iter1++) {Truestring+=mixstring.mid (nStart,iter1-
>nstart-nstart);
nstart=iter1->nend;
} truestring+=mixstring.mid (Nstart);
8 Create a global vector variable whose elements are still vector types----equivalent to two-D arrays
StdAfx.h
Defining data structures, and declaring variables
-------------struct defines a struct as not a variable, so it cannot be added to the extern
struct node{
int nstart
before it; int nend;
};
struct vectornode{
std::vector<node> vectornode;
int numberoffenkuai;//-block number
};
extern std::vector <VectorNode> g_delvector; the declaration is variable , you can add extern to represent the global variable
extern std::vector <VectorNode> g_addvector;
Stdafx.cpp defines the variables declared in. h
#include "stdafx.h"
std::vector <VectorNode> g_delvector;
Std::vector <VectorNode> G_addvector;
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 U
Dgreater (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)
Own example: CString variables stored in vector containers
Std::vector <CString> M_vectorimgname;
#include "algorithm"
BOOL stlsort (const CString &s1,const CString &s2)
{
int i,j;
CString name1=s1. Right (S1. GetLength ()-s1. Reversefind (L ' \ ")-1);
CString name2=s2. Right (S2. GetLength ()-s2. Reversefind (L ' \ \)-1);;
I=_ttoi (name1. Left (name1. Find (L '. '));
J=_ttoi (name2. Left (name2. Find (L '. '));
Return i<j;
}
if (M_vectorimgname.size () >0)
{
std::sort (M_vectorimgname.begin (), M_vectorimgname.end (), stlsort);