3.4 迭代器介绍
#include<iostream>
using namespace std;
int main()
{
string s("some string");
if(s.begin() != s.end()){
auto it = s.begin();
*it = toupper(*it);
}
cout<<s<<endl;
for(auto it =s.begin(); it!=s.end() && !isspace(*it);++it)
*it = toupper(*it);
cout<<s<<endl;
for(auto it = s.cbegin(); it!=s.cend(); ++it)
cout<<*it;
string text;
while(cin>>text)
for(auto it = text.begin(); it!=text.end(); ++it)
cout<<*it;
return 0;
}
使用迭代器运算
二分查找
#include<iostream>
#include<vector>
using namespace std;
int main()
{
cout<<"input increase number: ";
vector<int> iVec(0);
int iNum = 0;
while(cin>>iNum && iVec.size()<6)
iVec.push_back(iNum);
auto low = iVec.begin(), high = iVec.end();
auto mid = iVec.begin() + (high -low)/2;
int sought = 0;
cout<<"input you want find number: ";
cin>>sought;
while(mid != high && *mid != sought )
{
if(sought < *mid)
high = mid; //忽略后半部分
else
low = mid + 1;
mid = low + (high - low)/2;
}
cout<<*mid<<endl;
return 0;
}