编写程序将int型的list容器的所有元素复制到两个deque容器中。list容器的元素如果为偶数,则复制到一个deque容器中;如果为奇数,则复制到另一个deque容器里。
#include<iostream>
#include<vector>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<list>
#include<cstring>
#include<string>
#include<deque>
using namespace std;
int main()
{
list<int>ilist;
for(int i=1;i<=10;i++)
ilist.push_back(i);
deque<int> ide2;
deque<int> ide3;
list<int>::iterator iter=ilist.begin();
for(;iter!=ilist.end();++iter)
{
if(*iter%2)ide2.push_back(*iter);
else ide3.push_back(*iter);
}
for(deque<int>::iterator it=ide2.begin();it!=ide2.end();++it)
cout<<*it<<" ";
cout<<endl;
for(deque<int>::iterator it=ide3.begin();it!=ide3.end();++it)cout<<*it<<" ";
return 0;
}
9-20
#include<iostream>
#include<vector>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<list>
#include<cstring>
#include<string>
#include<deque>
using namespace std;
int main()
{
list<int>ilist;
vector<int>ivec;
for(int i=1;i<=10;i++)
{
ilist.push_back(i);
ivec.push_back(i);
}
list<int>::iterator it1=ilist.begin();
vector<int>::iterator it2=ivec.begin();
if(ilist.size()==ivec.size())
{
int a=1;
while(it1!=ilist.end())
{
if(*it1!=*it2){a=0;cout<<"不相等。1。"<<endl;break;}
++it1;
++it2;
}
if(a)cout<<"相等。。。"<<endl;
}
else cout<<"不相等。2。"<<endl;
return 0;
}
标准答案中有这么一句判断,能让程序简练很多(能够让程序少了容器长短判断的一步。。)
if(it1==ilist.end()&&it2==ivec.end())
9-30 这样测试很新颖哦,省的重复输出了
#include<iostream>
#include<vector>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<list>
#include<cstring>
#include<string>
#include<deque>
using namespace std;
int main()
{
vector<int>vec;
int num;
cout<<vec.capacity()<<endl;
for(int i=0;i<129;i++)
{
vec.push_back(i);
if(num!=vec.capacity())cout<<vec.capacity()<<endl;
num=vec.capacity();
}
return 0;
}