C++中的STL(Standard Template Library)用起来挺方便的,这里我们来做一下总结。
一、set
set是STL中一种标准关联容器 (vector,list,string,deque都是序列容器,而set,multiset,map,multimap是标准关联容器),它底层使用 平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高。
在set中元素都是唯一的,而且默认情况下会对元素 自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),对称差(set_symmetric_difference) 等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用multiset。
1)插入(insert)
set的元素添加时使用insert()方法,示例如下:
#include<bits/stdc++.h>
using namespace std; int main(){
set<int>s;
int n;
cout << "input: ";
for(int i = ; i < ; i++){
cin >> n;
s.insert(n);
}
cout << "output: ";
for(set<int>::iterator it = s.begin(); it != s.end(); ++it){
cout << *it << ' ';
}
}
运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQIAAAAjCAIAAADwqpMgAAADLUlEQVR4nO2aWW7DMAxEfcacWD+2T5Kcox8FClcSR0NqcdrM+zIUariJQLxs+77v+/58Ph+PxybEZ3Ich8ZAfDrHcRzHoTEQH43GQIj6GKSUUkr3BcXiDZK3Txd61hn9nlJjEaB83ZJqAHvgt6lghUTqz2L9GIxS9gbpPaP912Qk4Wpjv80zZ+0Fhxv7dWkG9Cey+BaZ701ThC9QzB4H7F3H+rFO46ODI+SPLLZn6oATjOkPJntgWq1g+k22vtHTnAqukVQXAd4CxQrKtNnrpaxbf6etemLXTamq/TVmpg6ML6/+YKrvDTLfICWyZJZyD16pgGtwkvh1IEt2OtUg47RWLKeWTbWPZB2syGP6U6j+KfKGm12T9p14pcbaW78yXrIed9aEjyTVAFss++yi6R1fx/QHw9wiW2lYP5H2Gd6cvQUaK860H++1SuSiWU9+I2/PBD/bfjB3jYHVBj5nb4F4e6YlPW0DNXSB/WLZHvsZ9emp5wCyMUgFOKzSOFsHW8IxV4NcY2+J8OJVF8wWPs5mviC1piOgEFj36s+i8y3yukCFmEfPe4PVIyvEJPShtRD60FoIjYEQmz60FmLTGAixfcyfouZza7yxFLHUssXAc3T88I3Ux+l4n++5LF35BoKZQv+TolEJzCvEtcpWJ8BGay9oKvbr0gzo4+2kL68ZiCdQh9W8yesz/ix2ipfXlnj5Kz5G2B57wTY9+tgX3oLrw+hjG2Z9EeBjih8bXHrQHrJtlk7265B8LX3LDLgGKZeRWzpWTZh4GH0gQlY1Vnwm334vw7iOgdUS0CpXdwM6LlINKyqwCOJs2lTzAn4tY6s4vD6IORXtYHbxkPni9aX8pzHAMKFeVzLAFss+u+ALVb2O6Vt5gYKACBmwPVOHG3j/MSB7jGnq8xt5e5DvMvtyr2sLb0YaM3ndwKgxsNYtG1JnGzEGuPdYvMd+1LVXn8yFrCpf/Bn5LuI8z/M8X68XuEXefp/FajUte2DM6AwhFeD16sZssekIKATWvfoWOF9s3GMfy3cd2RiM4v7EhOD5HoOxb5HfZcSFINE3RUJoDISYdm8gxF9CYyDEFwk3M2cWuEdJAAAAAElFTkSuQmCC" alt="" />
可见set已为我们剔除了相同的元素了,并且默认升序输出。假如我们想构造一个升序的set,可以这样声明:set<int, greater<int>> s ;
2)历遍
set的历遍一般是用迭代器iterator,这是对指针的封装,示例代码如上。
3)初始化
我们可以使用数组来进行初始化,或者用复制构造函数:
int main(){
int A[] = {, , };
set<int> setA(A, A + );
set<int> setB(setA);
}
4)并集、交集、差集
利用set的方法可以方便地求出并集、交集和差集:
#include<bits/stdc++.h>
using namespace std; int main(){
int A[] = {, , };
int B[] = {, , , -};
set<int> setA(A, A + );
set<int> setB(B, B + );
set<int> set1, set2, set3; set_union(setA.begin(), setA.end(), setB.begin(), setB.end(), insert_iterator<set<int>>(set1, set1.begin()));
set_intersection(setA.begin(), setA.end(), setB.begin(), setB.end(), insert_iterator<set<int>>(set2, set2.begin()));
set_difference(setA.begin(), setA.end(), setB.begin(), setB.end(), insert_iterator<set<int>>(set3, set3.begin())); cout << "union: ";
for(set<int>::iterator it = set1.begin(); it != set1.end(); ++it) cout << *it << ' '; cout << endl; cout << "intersection: ";
for(set<int>::iterator it = set2.begin(); it != set2.end(); ++it) cout << *it << ' '; cout << endl; cout << "difference: ";
for(set<int>::iterator it = set3.begin(); it != set3.end(); ++it) cout << *it << ' '; cout << endl;
}
三个函数的参数都类似,运行效果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAA8CAIAAABTvhANAAAF+UlEQVR4nO3d21rqRhyH4Vyi11YRsASrgrJTcYOKFoRFBPXI1pZVq9244UJ6MM08f2YyQyaEDJHfe8QKQxiy5iM8Aup4nud53i3hzUIHD4fD4XA4Go1Go9EdcT/t4eHh/v7+7u5u5Bv6bmcR7n0Qwjdfn+j5bnxdotPpdDqdn33X19fX19dXV1ftdrvdbl9cXJyfn7darVardXZ2dnp6enp6enJycnx83Gw2m83m0dHR0dHR4eHh4eHhga/RaNTr9Xq9XqvVarVatVqtVquVSqVSqez79qaVy+WSZNe349ue9pNvKwi/dnt7e2dnZ3d3t1Qq8XusVCrVarVWq9Xr9UajcbAcGo0GPXr0uNGjRI8JPSz8UBSLRdd1C4XC5uZmPp/P5XLZbHZjYyOTyWQymfX19R/MrUeVUXCEtGYWqEmRB8mzpLHx5ISWvmn1p/Wm3UjkroS6aGMsMxpbu92+vLy8uLhg1bHwWHXHx8csOSG2wMxoXeVymaalKUquqCgpKGxq/egrFAqu67quy+LkTZZKpXK5vLe3t7+/z5Z71Sr5qYoeRuEAys9K/GCyg6bqkOeRTIezI5yHcO4Szm+0OppWbxZNYEJj+swYmhnDS+OEEx0/17HwDvyTm/zcLCfHV4lwvqJpuT6aE6tFDimfz7M1JMj6NrT4mGw2m8vlWJmu6xaLxa2tLXqSZEGyJi2Sn7aE8ALb05wM2VGVT4aJRahKUYzw7e1tErfPz096WfYRwrvW2yz/TvsnyN++v6a9vr6+vr6+vLz8STw/Pz8/P/9BfCfG4/F4PP5d8luQp2m/zvKLucfHR2EL29XT0xObA5sem/b3ZTKej3Dk6RHmRyZ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qiZ0LbMwJYMewv9TL4NfgAFtBXVogQwAL2N3oRIYA1g8EAfy4bwKYBgQgBLGD5eZ43GAwmkwkiBEia53msQEQIYMft7a3nQ4QAFtwSiBAgef8BJAKyNqXTyt0AAAAASUVORK5CYII=" alt="" />
二、map
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能 力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严 格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。
1)声明
1 map<string, int> m1;
map<int, string> m2;
map<string, char> m3;
map< char, string> m4;
map<char, int> m5;
map<int, char> m6;
以上我们分别声明了6个不同的映射。
2)插入
#include<bits/stdc++.h>
using namespace std; int main(){
map<string, string> m; m.insert(make_pair("A", "apple"));
m.insert(pair<string, string>("B", "banana"));
m.insert(map<string, string>::value_type("C", "cherry"));
}
以上我们分别用了三种方式进行元素的插入,其效果都是一样的。当然还有另一种简单的方式如:m["A"] = "apple"。这种方式有所不同:一般的话,后面的方式可以修改前面的方式的值,反之不能。
#include<bits/stdc++.h>
using namespace std; int main(){
map<string, string> m;
//m["A"] = "foo";
m.insert(make_pair("A", "apple"));
m.insert(pair<string, string>("B", "banana"));
m.insert(map<string, string>::value_type("C", "cherry"));
m["A"] = "boo"; cout << m["A"] << endl;
cout << m["B"] << endl;
cout << m["C"] << endl;
}
上面我们用m["A"] = "boo" 来修改原来的映射,运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMUAAAA6CAIAAACPngprAAAKC0lEQVR4nO3d+1faWAIHcP6jGWc6ZxQBQSQCwf1btnt2p7uztTNT6wMQQR4JLxFERd4gSCABUTq2FQFBERB8lDh/yP5wIUWpVrF7HM7cz8npiek98Sb5nnvDzSWyCIIgCCIejycSiUQiQVFkkqIokowTsa3oZiQcDAd9oYA34HP7vS6fx+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1vRrWg0enF+wePxnrpeUF/6H6lQRvJeopxMAAAAAElFTkSuQmCC" alt="" />
可见修改成功。
#include<bits/stdc++.h>
using namespace std; int main(){
map<string, string> m;
m["A"] = "foo";
m.insert(make_pair("A", "apple"));
m.insert(pair<string, string>("B", "banana"));
m.insert(map<string, string>::value_type("C", "cherry"));
//m["A"] = "boo"; cout << m["A"] << endl;
cout << m["B"] << endl;
cout << m["C"] << endl;
}
现在我们用make_pair试图进行修改,结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALcAAAA3CAIAAADBvEwQAAAE2ElEQVR4nO3ca0/iWBgH8H6iMdlN1hldnRna0kJxZvGb7TovZhQFpYCr3EYubQ8QjTEmXrLOjAWhSC/sIH6MfVEK5dIWd9zQ1eeXxpTD6fEx50/bHEAMCRxCiOP5XKGQzeWS2c+x/Ux4N/mJ/fPDTuJDJP5HmP19c3ttc/tjZDu0E92ORROJWDK5n81mioU8z3GCwCMklMulcrlcGoMQQggJgsDzPDemaFIo5HW5XO7g4OCzIZvNZjKZTLq3pdPpdDqTSqX7UqlUKpVKJpPJ/f39vb29vb3d3d3dhLGZJBKJeDweM2NjbJSNDrDRaJRlBy0sy7IsG2VZljUOicdj8Xg8Hk8kEonEYGR9cF0sFuvvjNAH7O/0xh+zM8m2SSQSiUQi4XA4HA5vbW1tbm6GQqFQKLSxsbGxsbG+vr6+vv7J8PHHYIjnEBIkSWrJ8m2rJTVvb6RmtS59u2l8qdWvqvUr8eYvsXYlVr9Wq9e1WrVWq9dvJKlxe9uU5ZaiKIqiqKqiDjPaR1t08oiW7ravaSLpGlKj0WgMfkqSpO8b6vV6vX6jq9mpGkSxKo67FsVrURTF6wH9iaoo9g40RrqZyPaX9wqYWI+NCXX26zX5Zvhq+PIYMMRzzWZzbW2N9HpxgnyNE4tvPPNLb39afD33annu5dKLl4sv5hfm5hd+frXwy8Liwq+Ly8tLbz1vSJKgaYrx+xmGCQQCKyuBlWGBYYzBP+AzoWmaoiiK8upIkiRJYhxJEuQEBEEQBN7nwXGPB/d49EcEjk8YaTCOV9+8xs7Y6L2aKIqiaJqmfT6fz+/3639O4BnABJ5TVZWiaQwAK4IAKQFOBKEIKQEO4FwCnCGBU1WVNlJyZJhpUcBlzCnp5wOCAoYgxGuq6vMNpQSAISUkaJrm8/kw65QcmUzTDp6achlpmub3+46G9TuYH06zD56gSrncbrcZxo9ZTDakBGCVSqXdbgcYBoOUACu9lAQCGKQEWOmnxOZW1Oopm0PAk1KpVDRNYwKBWRcCXKxUKmuaxjDMrAsBLoYQUlXN7/fPuhDgYgghVVUhJcCOnhIfpATYgJQAZ3DFAc76KYGVD2DJfC6BlIDJSqWSqqr2nxwAz13v7tWUkvFLj2PjNCv3D+0PXETgeUVRaIrCpngDz6qD1QT/SH/gIjxXVBTF6/ViTpM68nK3mfVH6Q9cpFgsyrLsJUnMYiKnmen/Yh+4SLFQkGWZJAgMUgKs5PN5udUicHwkFkeTrgjYFOl5rP7ARfK5XKvVwnF81oUAF8v1UuKZdSHAxfKQEuAon8/DFQc4KBQKsiwTkBJgQ18vIQhi1oUAF+M4TlEUkiRnXQhwMUEQ+iv0E40vn4BnB6GS+b/cTAQpee70b23pnxywAil57g4PD9vtNmP6Nvl4JqyWzyf2t1put1mGt1m8B65wfHz89/fvKysrD32fxbE/NnZP49g+PhRwhZOTk06n8+7de5vpsZ/pkSnHHn46gZS43enpaafTef/+t3+XEsf+D22HlLjR2dnZ3d1dMBi0mSrHlNj0N3MMFkTEpc7OzrrdbjAYxKa4G8UsriAT+4/M9zQ5gJS41Pn5ebfbXV1dnXUhEBEXm3lK4FrzP3BxceGScwlwL0gJcHZ5eXl/fw8pAXYgJcAZpAQ4+getVlETpni4fwAAAABJRU5ErkJggg==" alt="" />
可见,修改失败。
3)历遍
#include<bits/stdc++.h>
using namespace std; int main(){
map<string, string> m; m.insert(make_pair("A", "apple"));
m.insert(pair<string, string>("B", "banana"));
m.insert(map<string, string>::value_type("C", "cherry")); for(map<string, string>::iterator it = m.begin(); it != m.end(); ++it){
cout << it->first << ' ' << it->second << endl;
} cout << m["A"] << endl;
cout << m["B"] << endl;
cout << m["C"] << endl; }
我们可以使用map的迭代器进行历遍,分别输出first和second,也可以像使用数组一样对map进行访问,运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPkAAABqCAIAAADx32AqAAAIuklEQVR4nO3d61Pa6ALH8fxF25lzZk5362l3V0Cubk/7f53T47a1Wy/VVoQeBbFVhAQYGad1xhteuAdIAoqIvd/enxcRjLkRNIiG32ecnTQ8PMaZL0+zJFIitPSaIkMURYXC4YVgcH5hwTf/ano2MPHS99T9v0cvvI8mPX9OuIfGnz8cf/7X5POxF1PPp6e83mmfb3Z+PrAUXAyHQiQZpigyGo1Eo9GIBEVRFEWRJBkOh0MSSwLB4CJvYWHh9evXrxrm5+cDgUBg7uRrbm5ubi7g9881+f1+v9/v8/l8s7OzMzMzMzMvX7586W18CXi9Xo/HMy3knnZPuadOuaemptzu0z1ut9vtdk+53W534ykez7TH4/F4vF6v13s6Mz85b3p6urkhwk/Y3DiZX+KFnOcCk5OTk5OTExMTExMTz549Gx8fHxsbGxsbGx0dHR0dHRkZGRkZedrwlx6eqhrRRviU5sxPnjwZHh4eHh5+/PjxI4mHcv4UGBoaGhoa+m/DgwcP/tPw7wYiFHxFhUMURdI0XWKYYqlEF4pZupDO0XvZ/E4mt53ObaeyiVRmO5XeTaeTmUw6k8nlsjSdLxYLDFNiWZZlWY5jubMa+8V7eIxIiVdsKgjQvDydz+fzp/+laZrfbsjlcrlclpdRk25IpdIpqWQqlUylUqnkKf6BdCp18sTGTFlZqt/85ABkj0eFzHE2j1dgr2G3YUdXux0gnH9bQULZlpxNiZPWC4XCw4cPLQMDJrPlV5O577f+m7d//1vfrzd+uXPj59s//dz3081bN27e+vsvt/5xq+/WP/vu3Ln9e/9vFovZZrM6HQ6n0+lyuQYHXYNnuc5yNjhO2QVsNpvVarVaB3gWi8ViMUtZLGaLDLPZbDabmv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7GDRHum20njRd9RyJGgdtFpfX19fX6/Vavfv3+/2sbQNoUMbrmPrOHuB89jY2NjY2LherQOcB1qHXrG5ubm5uXl0dITWweDQOvQKtA69Ymtra2trS/fW8VYJXDkdap2H1uEKSSQSaB16QiKRSCQSzdZVruETyhf/lU5XznfZH6Ajtre3t7e36/W6aF1Xyl3jdluTAFyGnZ2dnZ2dZuvxs5rDdGwdSzt0h7B1lXB1X9cBLhvf+vHx8SW3ju7hsu3u7u7u7vKtExp+F079tERpv+w8l/HjATTt7e0JW1eiVCeqhWsjmUzu7e2pt660GGORhuskmUwmk8l3797hfhgwOLQOvSKVSqVSKbQOxofWoVek0+l0Oo3WwfgymUwmk3n//n3zWhLeVAFjymazzdYJvF8OBpbNZrPZLFoH48vlcrlc7sOHD1ruc9R4g4Be4wH0lM/n8/m8qHX+IdnylAYoZXqR8QB6ommapumPHz+qty67JKu0q8t4AD0VCoVCoaDeupZeO7ENoKdisVgsFtE6GJ+wdVHccblzDELDa0Cv8QB6KpVKpVLp06dPuG4KBscwDMMwaB2Mj2VZlmU/f/6M1sHgOI5D69ATyuVyuVz+8uULWgeDQ+vQKyqVSqVSQetgfPv7+/v7+1+/ftXyOQKXeFwAejs4ODg4OFBvncD1HTCAarVarVa/ffuG1sHgDg8PDw8Pm63HBYTDlC7gy45XuuCvciOAyu0DAPoQtt7u/SotxxOSc/2W+6VTAeijVqvVajVp6yLqvYrCJdpf2tE6dBzf+vfv38/desvx7e5H69ARR0dHR0dH0tZl12nRtsbxQi1fHggdOqVerzdbJzT8vyahcE4iO15UrZaa0Tp0Sr1er9frP378uArXTRE6dNBVaB1nL3AZjo+Pj4+Pr8i6DtBBaB16BX+PQMv7YQCuvdXV1bdv31arVbQOBhcIBAKBQKFQQOtgcH6/3+/30zSN1sHg/H6/z+dD62B8s7OzaB16wf8BET8y0+68sK8AAAAASUVORK5CYII=" alt="" />
4)删除
在刚在的代码中,我们需要删除元素,可以这么做:
map<string, string>::iterator it;
it = m.find("A");
m.erase(it);
5)查找
假如我们想要知道一个映射是否存在,可以这么做:
map<int, char> m;
if(m.find() == m.end()) cout << "not found" << endl;
else cout << "found" << endl;
假若m[0]未被赋值,则m.find(0) == m.end()。
//End