1.在迭代器范围内查找元素
#include<iostream>
#include<vector>
using namespace std;
vector<int>::iterator findInt(vector<int>::iterator beg,
vector<int>::iterator end,int ival)
{
while(beg != end)
{
if(*beg == ival)
break;
else
++beg;
return beg;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int ia[]={0,1,2,3,4,5,6};
vector<int> ivec(ia,ia+7);
int ival;
cin >> ival;
vector<int>::iterator iter;
iter = findInt(ivec.begin(),ivec.end(),ival);
if(iter != ivec.end())
cout << "success" << endl;
else
cout << "fail" << endl;
system("pause");
return 0;
}
2.//从标准输入中读入若干string 并将其全部输出
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> svec;
string str;
while(cin >> str)
svec.push_back(str);
for(vector<string>::iterator iter = svec.begin();iter!=svec.end();iter++)
cout << *iter << " ";
cout << endl;
system("pause");
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
list<string> slist;
string str;
while(cin >> str)
slist.push_back(str);
for(list<string>::iterator iter = slist.begin();iter!=slist.end();iter++)
cout << *iter << " ";
cout << endl;
system("pause");
return 0;
}
3.
// 9_18.cpp : 定义控制台应用程序的入口点。
//把list中的数字复制到两个deque,偶数一列,奇数一列
#include "stdafx.h"
#include<iostream>
#include<list>
#include<deque>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
list<int> ilst;
deque<int> evenDq,oddDq;
int ival;
while(cin >> ival)
ilst.push_back(ival);
//复制元素
list<int>::iterator iter = ilst.begin();
for(;iter!=ilst.end();++iter)
{
if(*iter % 2 == 0)
evenDq.push_back(*iter);
else
oddDq.push_back(*iter);
}
//输出对象进行检验
deque<int>::iterator it;
it = evenDq.begin();
while(it != evenDq.end())
{
cout << *it << " ";
it++;
}
cout <<endl;
it = oddDq.begin();
while(it != oddDq.end())
{
cout << *it << " ";
it++;
}
cout <<endl;
system("pause");
return 0;
}
4.
// 9——26.cpp : 定义控制台应用程序的入口点。
//将ia复制到vector和list中,使用单个迭代器参数的erase函数,把vector中的even都删掉,list的odd都删掉
//int ia[]={0,1,2,3,4,5,6,7,8,9,45}
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int ia[]={0,1,2,3,4,5,6,7,8,9,45};
vector<int> ivec(ia,ia+10);
list<int> ilst(ia,ia+10);
//删除vector中的偶数
vector<int>::iterator iter = ivec.begin();
for(;iter!=ivec.end();++iter)
{
if(*iter %2 == 0)
iter = ivec.erase(iter);//iter指向被删除元素后面的元素
--iter;//迭代器回退指向被删除元素的前一元素,如果删除最后一个元素,iter就指向尾端下一元素啦,for循环hui ++,所以要回退
}
//删除list中的奇数
list<int>::iterator lter = ilst.begin();
for(;lter!=ilst.end();++lter)
{
if(*lter %2 != 0)
lter = ilst.erase(lter);//iter指向被删除元素后面的元素
--lter;//迭代器回退指向被删除元素的前一元素,如果删除最后一个元素,iter就指向尾端下一元素啦,for循环hui ++,所以要回退
}
return 0;
}
// 9_30.cpp : 定义控制台应用程序的入口点。
//编写程序研究标准库为vector提供的内存分配策略
#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int>ivec;
cout << "ivec.size:" <<ivec.size() <<" capacity:"<<ivec.capacity() <<endl;
//添加十个元素
for(int ix =0;ix!=10;ix++)
ivec.push_back(ix);
cout << "ivec.size:" <<ivec.size() <<" capacity:"<<ivec.capacity() <<endl;
//将现有容量用完
while(ivec.size()!=ivec.capacity())
ivec.push_back(0);
cout << "ivec.size:" <<ivec.size() <<" capacity:"<<ivec.capacity() <<endl;
//添加一个元素,长度应该加1,容量>=原容量+1
ivec.push_back(0);
cout << "ivec.size:" <<ivec.size() <<" capacity:"<<ivec.capacity() <<endl;
//将容量设为至少100
ivec.reserve(100);
cout << "ivec.size:" <<ivec.size() <<" capacity:"<<ivec.capacity() <<endl;
system("pause");
return 0;
}
// 9_38.cpp : 定义控制台应用程序的入口点。
//查找所有的数字字符 find_first_of
//查找所有的字母find_first_not_of
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str("ab2c3d7R4E6");
string numbers("0123456789");
string letters("abcdefghijklmnopqrstuvwxyz");
string::size_type pos = 0;
while((pos = str.find_first_of(numbers,pos)) != string::npos)
{
cout << pos <<" " << str[pos] << endl;
pos++;
}
pos = 0;
while((pos = str.find_first_of(letters,pos)) != string::npos)
{
cout << pos <<" "<< str[pos] << endl;
pos++;
}
system("pause");
return 0;
}