STL List Set

1. List

begin() end() //Iterators:
front() back() //Element;
clear() 
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;

INSERT

INSERT
it = mylist.begin();// 1 2 3 4 5
++it;       // it points now to number 2
mylist.insert (it,10);// 1 10 2 3 4 5
// "it" still points to number 2 
mylist.insert (it,2,20);// 1 10 20 20 2 3 4 5

--it;// it points now to the second 20
std::vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());// 1 10 20 30 30 20 2 3 4 5

ERASE

ERASE
// 10 20 30 40 50 60 70 80 90
it1 = it2 = mylist.begin(); 
advance (it2,6);            
++it1;                      

it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90      
//An iterator pointing to the element that followed the last element erased by the function call.
//it1 points to number 30             
it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90 
//it2 points to number 80   

++it1;                     
--it2;   
                   
//[first,last)
mylist.erase (it1,it2);     // 10 30 60 80 90

SWAP

SWAP
std::list<int> first (3,100);   // three ints with a value of 100
std::list<int> second (5,200);  // five ints with a value of 200

first.swap(second);
//first contains: 200 200 200 200 200 
//second contains: 100 100 100

SORT

SORT
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three");

mylist.sort();// Three one two 

mylist.sort(cmp);

UNIQUE

UNIQUE (after sort)
mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
                            // 15.3,  72.25, 72.25, 73.0,  73.35

mylist.unique();           //  2.72,  3.14, 12.15, 12.77
                            // 15.3,  72.25, 73.0,  73.35

REMOVE

REMOVE
int myints[]= {17,89,7,14};
std::list<int> mylist (myints,myints+4);

mylist.remove(89);//17 7 14

 2.Set

begin() end()
empty() size()

INSERT

myset.insert(n);
myset.insert(it,n); 

int myints[]= {5,10,15};
myset.insert (myints,myints+3);

ERASE

myset.erase (it);
myset.erase (40);

it = myset.find (60);
myset.erase (it, myset.end());

SWAP

std::set<int> first (myints,myints+3);     // 10,12,75
std::set<int> second (myints+3,myints+6);  // 20,25,32

first.swap(second);

FIND

it=myset.find(20);
 // An iterator to the element, if val is found, or set::end otherwise.

COUNT

myset.count(i)
//1 if the container contains an element equivalent to val, or zero otherwise.

LOWER_BOUND UPPER_BOUND

itlow=myset.lower_bound (30);
//An iterator to the the first element in the container which is not considered to go before val, or set::end if all elements are considered to go before val.

itup=myset.upper_bound (60);
//An iterator to the the first element in the container which is considered to go after val, or set::end if no elements are considered to go after val.

// 10 20 30 40 50 60 70 80 90
//Notice that lower_bound(30) returns an iterator to 30, whereas upper_bound(60) returns an iterator to 70.

猜你喜欢

转载自www.cnblogs.com/Cindy-Chan/p/11182203.html