STL: list ,set ,pair.map的使用
/**
* K1:---------------------list的相關(guān)API------------------------------
*
* list<T> lstT;//list采用采用模板類實(shí)現(xiàn),對象的默認(rèn)構(gòu)造形式:
list(beg,end);//構(gòu)造函數(shù)將[beg, end)區(qū)間中的元素拷貝給本身。
list(n,elem);//構(gòu)造函數(shù)將n個elem拷貝給本身。
list(const list &lst);//拷貝構(gòu)造函數(shù)。
3.6.4.2 list數(shù)據(jù)元素插入和刪除操作
push_back(elem);//在容器尾部加入一個元素
pop_back();//刪除容器中最后一個元素
push_front(elem);//在容器開頭插入一個元素
pop_front();//從容器開頭移除第一個元素
insert(pos,elem);//在pos位置插elem元素的拷貝,返回新數(shù)據(jù)的位置。
insert(pos,n,elem);//在pos位置插入n個elem數(shù)據(jù),無返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)區(qū)間的數(shù)據(jù),無返回值。
clear();//移除容器的所有數(shù)據(jù)
erase(beg,end);//刪除[beg,end)區(qū)間的數(shù)據(jù),返回下一個數(shù)據(jù)的位置。
erase(pos);//刪除pos位置的數(shù)據(jù),返回下一個數(shù)據(jù)的位置。
remove(elem);//刪除容器中所有與elem值匹配的元素。
3.6.4.3 list大小操作
size();//返回容器中元素的個數(shù)
empty();//判斷容器是否為空
resize(num);//重新指定容器的長度為num,
若容器變長,則以默認(rèn)值填充新位置。
如果容器變短,則末尾超出容器長度的元素被刪除。
resize(num, elem);//重新指定容器的長度為num,
若容器變長,則以elem值填充新位置。
如果容器變短,則末尾超出容器長度的元素被刪除。
3.6.4.4 list賦值操作
assign(beg, end);//將[beg, end)區(qū)間中的數(shù)據(jù)拷貝賦值給本身。
assign(n, elem);//將n個elem拷貝賦值給本身。
list& operator=(const list &lst);//重載等號操作符
swap(lst);//將lst與本身的元素互換。
3.6.4.5 list數(shù)據(jù)的存取
front();//返回第一個元素。
back();//返回最后一個元素。
3.6.4.6 list反轉(zhuǎn)排序
reverse();//反轉(zhuǎn)鏈表,比如lst包含1,3,5元素,運(yùn)行此方法后,lst就包含5,3,1元素。
sort(); //list排序
*
*
*/
/**
* K2:----------------list的細(xì)節(jié)----------------------------
* 1.sort排序,需要傳入二元比較函數(shù).或者仿函數(shù).
* 注意點(diǎn):JAVA中的compare返回的是int,這里返回的是bool.
*
* 2.remove 自定義結(jié)構(gòu)體,需要重寫operator== 的二元函數(shù).
*
*/
/**
* K3:-------------------set的使用--------------------------
*
* 1.自定義類型的class,需要傳入仿函數(shù)的比較器.bool operator()(const T1&,consrT2&),public修飾.
* 或者重寫類內(nèi)部的< 重載符.
*
* 2.移除自定義的類,需要重寫operator==的二元函數(shù).
*
*
*
* set構(gòu)造函數(shù)
set<T> st;//set默認(rèn)構(gòu)造函數(shù):
mulitset<T> mst; //multiset默認(rèn)構(gòu)造函數(shù):
set(const set &st);//拷貝構(gòu)造函數(shù)
3.7.2.2 set賦值操作
set& operator=(const set &st);//重載等號操作符
swap(st);//交換兩個集合容器
3.7.2.3 set大小操作
size();//返回容器中元素的數(shù)目
empty();//判斷容器是否為空
3.7.2.4 set插入和刪除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//刪除pos迭代器所指的元素,返回下一個元素的迭代器。
erase(beg, end);//刪除區(qū)間[beg,end)的所有元素 ,返回下一個元素的迭代器。
erase(elem);//刪除容器中值為elem的元素。
*
*
*/
/**
*
* K4:----------------迭代器補(bǔ)充--------------------
* vector<int>::iterator pStart = v.begin(); //vector 容器提供了 begin()方法 返回指向第一個元素的迭代器
vector<int>::iterator pEnd = v.end(); //vector 容器提供了 end()方法 返回指向最后一個元素下一個位置的迭代器
* 并不是最后一個,而是最后一個的下一個.......
*
*
*/
/**
*
* K5:------------- == < 仿函數(shù),謂詞的總結(jié):------------
* **都要注意加const修飾**
* bool operator==(const T &t)const 是用于remove的時候調(diào)用.
* bool operator<(const T &t)const 是用于比較排序的時候調(diào)用.
* 仿函數(shù) 也是用于比較排序的時候調(diào)用. bool operator()(const T&t1,const T &t2)const ;
*
* 這個無法加const
* 或者寫一個全局函數(shù). bool compare(const T&t1,const T &t2)
*
* 1、函數(shù)對象通常不定義構(gòu)造函數(shù)和析構(gòu)函數(shù),所以在構(gòu)造和析構(gòu)時不會發(fā)生任何問題,避免了函數(shù)調(diào)用的運(yùn)行時問題。
* 2、函數(shù)對象超出普通函數(shù)的概念,函數(shù)對象可以有自己的狀態(tài)
* 3、函數(shù)對象可內(nèi)聯(lián)編譯,性能好。用函數(shù)指針幾乎不可能
* 4、模版函數(shù)對象使函數(shù)對象具有通用性,這也是它的優(yōu)勢之一
*
*
*/
//仿函數(shù)
class SetCompare {
public:
bool operator()(const SetPerson &p1, const SetPerson &p2) const {
return p1.age > p2.age;
}
};
//重載< 用于排序
class MapPerson {
public:
int age;
bool operator<(const MapPerson &p) const {
return this->age < p.age;
}
};
//重載== 用于比較刪除
class ThreePerson {
public:
int age;
ThreePerson() {
}
ThreePerson(int age) {
this->age = age;
}
bool operator==(const ThreePerson &person) const {
return person.age == this->age;
}
~ThreePerson() {
cout << "destroy------------" << endl;
}
};
//普通函數(shù)
bool comparePerson(const ThreePerson &p1, const ThreePerson &p2) {
return p2.age > p1.age;
}
//map的使用
void printMap(map <MapPerson, string> map1) {
for (auto it = map1.begin(); it != map1.end(); ++it) {
cout << "map's age: " << it->second << endl;
}
}
void useThreeState2() {
map <MapPerson, string> map1;
auto var1 = map1.insert(make_pair(MapPerson(), "July"));
auto var2 = map1.insert(make_pair(MapPerson(), "Lucy"));
map1.insert(make_pair(MapPerson(), "LiLei"));
printMap(map1);
cout<<var1.second<<endl;//插入成功. value=July res=1
cout<<var2.second<<endl;//插入失敗,value不變還是July res=0
}