1 /*- ==========================================================
2 * 文件名 :STL_con_ite_7.cpp
3 * 开发人员:袁培荣
4 * 当前版本:1.0.0.2595
5 * 创建时间:2012-05-26
6 * 修改时间:2012-05-26
7 * 功能说明:STL 容器和迭代器连载7_容器大小的操作
8 * 版权说明:版权所有 袁培荣 YuanPeirong
9 * 编译环境:Windows 7(x64) SP1 简体中文专业版
10 * 编译器: Visual Studio 2010 SP1(中文旗舰版)
11 MinGW 20120426 GNU GCC 4.6.2
12 Visual C++ 6.0 SP6(中文企业版)
13 - ==========================================================*/
14
15 #include <iostream>
16 #include <vector>
17 #include <list>
18 #include <deque>
19 #include <string>
20
21 using std::cout;
22 using std::endl;
23 using std::vector;
24 using std::list;
25 using std::deque;
26 using std::string;
27
28 int main(int argc, char* argv[])
29 {
30 //首先,请你看本文的标题,是容器大小的操作,
31 //而非顺序容器大小的操作,也就是说:
32 //本文的内容适用所有容器,而不仅仅是顺序容器。
33
34 //操作1:
35 //size()成员函数返回当前容器中元素的个数
36 //返回类型为 容器名<T>::size_type
37 cout<<"操作1:"<<endl;
38 vector<int> v1;
39 vector<string> v2(10);
40 vector<int>::size_type sv1=v1.size();
41 vector<string>::size_type sv2=v2.size();
42 cout<<"v1.size()="<<sv1<<endl;
43 cout<<"v2.size()="<<sv2<<endl;
44 //注意:不能用 vector<int>::size_type类型来接收 v2.size()
45 //因为一个是vector<int>类,另一个是vector<string>类,没有任何关系。
46 //如果容器元素有增减,我们就要重新获取其size。
47 v1.push_back(10); //在最尾加入一个10
48 v2.pop_back(); //把最尾元素删除
49 cout<<"增减容器元素后:"<<endl;
50 sv1=v1.size(); //重新获取容器的大小
51 sv2=v2.size(); //重新获取容器的大小
52 cout<<"v1.size()="<<sv1<<endl;
53 cout<<"v2.size()="<<sv2<<endl;
54
55 //操作2:
56 //max_size() 成员函数返回当前容器所能容纳元素数量的最大值
57 //(注:包括可重新分配内存) 返回类型为 容器名<T>::size_type
58 //这个值可能与编译器,编译选项,操作系统,类型T等的不同而不同
59 cout<<"操作2:"<<endl;
60 vector<int>::size_type mv1=v1.max_size();
61 vector<string>::size_type mv2=v2.max_size();
62 cout<<"v1.max_size()="<<mv1<<endl; //你的输出值不一定和我的一样
63 cout<<"v2.max_size()="<<mv2<<endl; //mv2和mv1也不一定相同
64 //max_size()函数的返回值和当前容器元素的个数没有关系
65 //改变当前容器元素的个数和结果不变
66 v1.push_back(15); //在最尾加入一个10
67 v2.pop_back(); //把最尾元素删除
68 cout<<"增减容器元素后:"<<endl;
69 mv1=v1.max_size();
70 mv2=v2.max_size();
71 cout<<"v1.max_size()="<<mv1<<endl;
72 cout<<"v2.max_size()="<<mv2<<endl;
73 //注意:对同一个容器对象,size函数和max_size函数的返回类型是一样的
74 //其实,我们不用定义 mv1和mv2,可以分别重用sv1和sv2
75 cout<<"mv1用sv1替换,mv2用sv2替换后:"<<endl;
76 sv1=v1.max_size();
77 sv2=v2.max_size();
78 cout<<"v1.max_size()="<<sv1<<endl;
79 cout<<"v2.max_size()="<<sv2<<endl;
80
81 //操作3:
82 //empty()成员函数,如果当前容器没有容纳任何元素,
83 //则返回true,否则返回false.
84 cout<<"操作3:"<<endl;
85 if(v1.empty())
86 cout<<"v1是空的"<<endl;
87 else
88 cout<<"v1不是空的"<<endl;
89 v1.clear(); //清空v1
90 cout<<"清空v1后:"<<endl;
91 if(v1.empty())
92 cout<<"v1是空的"<<endl;
93 else
94 cout<<"v1不是空的"<<endl;
95
96 //操作4:
97 //resize(n)成员函数改变当前容器的大小为n
98 //如果n小于原来的大小,则删除多出来的元素
99 //如果n大于原来的大小,则添加的元素采用值初始化
100 //如果n等于原来的大小,则不操作
101 cout<<"操作4:"<<endl;
102 v1.push_back(10); //操作3中已经将v1清空,现在为其添加两个元素
103 v1.push_back(20);
104 cout<<"v1.size()="<<v1.size()<<endl;
105 int i;
106 for(i=0; i!=v1.size(); i++)
107 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
108 v1.resize(1);
109 cout<<"调整v1的大小为1后:"<<endl;
110 cout<<"v1.size()="<<v1.size()<<endl;
111 for(i=0; i!=v1.size(); i++)
112 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
113 v1.resize(5);
114 cout<<"再调整v1的大小为5后:"<<endl;
115 cout<<"v1.size()="<<v1.size()<<endl;
116 for(i=0; i!=v1.size(); i++)
117 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
118
119 //操作5:
120 //resize(n,t)成员函数改变当前容器的大小为n
121 //如果n小于原来的大小,则删除多出来的元素
122 //如果n大于原来的大小,则添加的元素都为t
123 //如果n等于原来的大小,则不操作
124 cout<<"操作5:"<<endl;
125 v1.clear(); ////清空v1
126 v1.push_back(10); //为其添加两个元素
127 v1.push_back(20);
128 cout<<"v1.size()="<<v1.size()<<endl;
129 for(i=0; i!=v1.size(); i++)
130 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
131 v1.resize(1, 100);
132 cout<<"v1.resize(1, 100)后:"<<endl;
133 cout<<"v1.size()="<<v1.size()<<endl;
134 for(i=0; i!=v1.size(); i++)
135 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
136 v1.resize(5, 100);
137 cout<<"v1.resize(5, 100)后:"<<endl;
138 cout<<"v1.size()="<<v1.size()<<endl;
139 for(i=0; i!=v1.size(); i++)
140 cout<<"v1["<<i<<"]="<<v1[i]<<endl;
141
142 return 0;
143 }
144
145
146 //============================
147 //运行结果:
148 //============================
149 // 操作1:
150 // v1.size()=0
151 // v2.size()=10
152 // 增减容器元素后:
153 // v1.size()=1
154 // v2.size()=9
155 // 操作2:
156 // v1.max_size()=1073741823
157 // v2.max_size()=1073741823
158 // 增减容器元素后:
159 // v1.max_size()=1073741823
160 // v2.max_size()=1073741823
161 // mv1用sv1替换,mv2用sv2替换后:
162 // v1.max_size()=1073741823
163 // v2.max_size()=1073741823
164 // 操作3:
165 // v1不是空的
166 // 清空v1后:
167 // v1是空的
168 // 操作4:
169 // v1.size()=2
170 // v1[0]=10
171 // v1[1]=20
172 // 调整v1的大小为1后:
173 // v1.size()=1
174 // v1[0]=10
175 // 再调整v1的大小为5后:
176 // v1.size()=5
177 // v1[0]=10
178 // v1[1]=0
179 // v1[2]=0
180 // v1[3]=0
181 // v1[4]=0
182 // 操作5:
183 // v1.size()=2
184 // v1[0]=10
185 // v1[1]=20
186 // v1.resize(1, 100)后:
187 // v1.size()=1
188 // v1[0]=10
189 // v1.resize(5, 100)后:
190 // v1.size()=5
191 // v1[0]=10
192 // v1[1]=100
193 // v1[2]=100
194 // v1[3]=100
195 // v1[4]=100
196 //============================