C++学习笔记1
for(vector<student-info>::const-iterator iter=students.begin();iter!=student.end();iter++)
{
cout<<(*iter).name<<endl;
}
每个容器都有一个关联的iterator
container-type:const-iterator 只读
iterator
vector:快速随机访问,类似数组
list:适合快速插入和删除,类似链表
list<student> extract_fails(list<student>& students)
{
list<student> fail;
list<student>::iterator iter=students.begin();
while(iter!=students.end())
{
if(fgrade(*iter))
{
fail.push_back(*iter);
iter=students.erase(iter);
}
else
++iter;
}
return fail;
}
list 不能用sort排序 自带了sort排序算法,用法如下:
list<student> students;
students.sort(compare);
copy函数
copy(buttom.begin(),bottom.end(),back.inserter(ret));
找一个页面中的链接
vector<string> find_urls(const string &s)
{
vector<string> ret;
typedef string::const-iterator iter;
iter b=s.begin(),e=s.end();
while(b!=e){
b=url_beg(b,e);//发现一个letters
if(b!=e){
iter after=url.end(b.e);//获得对应的url
ret.push_back(string(b.after));//记住url
b=after;
}
}
return ret;
}
accumulate(v.begin(),v.end(),0.0);//累加函数,属于<numeric>库
理解使用算法和迭代器以及iterators一个首要的重点:算法只作用于容器中的容器,而非容器本身。
待续......