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/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ijZcvR5uSkbj2A+A4/nqiZ0LbMwJYMewv9TL4NfgAFtBXVogQwAL2N3oRIYA1g8EAfy4bwKYBgQgBLGD5eZ43GAwmkwkiBEia53msQEQIYMft7a3nQ4QAFtwSiBAgef8BJAKyNqXTyt0AAAAASUVORK5CYII=" 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/yP5wIUWpVrF7HM7cz8npiek98Sb5nnvDzSWyCIIgCCIejycSiUQiQVFkkqI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lJZ/Pf+p6Qf2JiBFELBbbim1Fo1vRrWg0enF+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+lyuQYHXYNnuc5yNjh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e0WZDKoNbtq7xddXy5YHQQSu+9VqtxrdOtNmQ7GDRHum20njRd9RyJGgdtFpfX19fX6/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

C++ STL模板的更多相关文章

  1. 泛型编程、STL的概念、STL模板思想及其六大组件的关系,以及泛型编程(GP)、STL、面向对象编程(OOP)、C++之间的关系

    2013-08-11 10:46:39 介绍STL模板的书,有两本比较经典: 一本是<Generic Programming and the STL>,中文翻译为<泛型编程与STL& ...

  2. DLL中导出STL模板类的问题

    接上一篇. 上一篇的dll在编译过程中一直有一个警告warning C4251: ‘CLASS_TEST::m_structs’ : class ‘std::vector<_Ty>’ ne ...

  3. STL模板_容器概念

    一.STL(Standard Template Library,标准模板库)概述1.容器:基于泛型的数据结构.2.算法:基于泛型的常用算法.3.迭代器:以泛型的方式访问容器中的元素,是泛型的算法可以应 ...

  4. STL模板_概念

    模板和STL一.模板的背景知识1.针对不同的类型定义不同函数版本.2.借助参数宏摆脱类型的限制,同时也因为失去的类型检查而引 入风险.3.借助于编译预处理器根据函数宏框架,扩展为针对不同类型的 具体函 ...

  5. C++STL模板库序列容器之vector

    目录 STL之Vecter 一丶STL简介 二丶Vector用法 1.vector容器的使用 2.vector迭代器. 3.vector中的方法. 三丶常用算法 1.常见算法中的算法方法. 2.sor ...

  6. c++ STL模板(一)

    一.sort函数 1.头文件:#include < algorithm>: 2.它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n): 3.Sort函数有三个参数:(第三个参 ...

  7. STL模板整理 Binary search(二分查找)

    前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...

  8. STL模板整理 vector

    一.什么是标准模板库(STL)? 1.C++标准模板库与C++标准库的关系 C++标准模板库其实属于C++标准库的一部分,C++标准模板库主要是定义了标准模板的定义与声明,而这些模板主要都是 类模板, ...

  9. C++面试笔记--STL模板与容器

    1.C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vec ...

  10. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...

随机推荐

  1. HTML 学习笔记 JavaScript (prototype)

    原博地址:http://www.cnblogs.com/dolphinX/p/3286177.html 原博客的作者是一个非常牛逼的前端大神,我作为一个初学者,在此借助大神的博客进行自己的学习.在这里 ...

  2. 非对称加密算法RSA

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年首次公布,当时他们 ...

  3. 【Python】[面向对象编程] 访问限制,继承和多态

    1.在Python中两个下划线__ 就是带便私有属性 private   注意已两个下划线开头并且结尾的 如 __init__ 是特殊变量,不是私有变量 2.多态,Python的“file-like ...

  4. 《Java EE 开发技术与案例教程》 这是一本好书啊:简洁精辟(相见恨晚)

    第一章:Java EE 概述 1.get:JPA:Java Persistence API, 数据持久化API: JPA是一种ORM规范,它的实现实例:Hibernate.mybatis 2.Web ...

  5. Tp缓存

    系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache) 对于File方式缓存下的缓存目录下 ...

  6. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  7. java高新技术-基本数据类型拆装箱及享元设计模式

    享元设计模式 public static void main(String[] args) { Integer iObj = 3; //自动装箱 System.out.println(iObj + 1 ...

  8. Alpha事后诸葛亮

    Aruba小组Cento项目Postmortem 队员: 408 409 410 428 429 431   设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰 ...

  9. Typescript Mixins(混合)

    除了惯例的面对对象的思想,另一种较流行的通过可复用组件创建类的方法是将简单的类混合到一起.你可能对这种混合的方式比较熟悉或对Scala语言的特性有理解,这种模式在JavaScript社区也有一定的人气 ...

  10. Structure Of Management Information - SNMP Tutorial

    30.6 The Structure Of Management Information In addition to the standards that specify MIB variables ...