当前位置: 首页 > news >正文

个人免费域名空间建站郑州网站优化外包顾问

个人免费域名空间建站,郑州网站优化外包顾问,做图书馆网站模板,在苏州找学生做家教在哪个网站在C03/98中&#xff0c;不同的容器和数组&#xff0c;遍历的方法不尽相同&#xff0c;写法不统一&#xff0c;也不够简洁&#xff0c;而C11基于范围的for循环以统一&#xff0c;简洁的方式来遍历容器和数组&#xff0c;用起来更方便了。 for循环的新用法 #include <iostre…

        在C++03/98中,不同的容器和数组,遍历的方法不尽相同,写法不统一,也不够简洁,而C++11基于范围的for循环以统一,简洁的方式来遍历容器和数组,用起来更方便了。

for循环的新用法

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> arr = {1, 2, 3, 4, 5};

    for(auto it = arr.begin(); it != arr.end(); it++)
    {
        cout << *it << endl;
    }

    return 0;
}

上面借助auto关键字,省略了迭代器的声明。

        现在,在C++11中终于有了基于范围的for循环。

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> arr = {1, 2, 3, 4, 5};

    for(auto n : arr)
    {
        cout << n << endl;
    }

    return 0;
}

        在上面的基于范围的for循环中,n表示arr中的一个元素,auto则是让编译器自动推导出n的类型。在这里,n的类型将被自动推导为vector中的元素类型int。

        在n的定义之后,紧跟一个冒号(:),之后直接写上需要遍历的表达式,for循环将自动以表达式返回的容器为范围进行迭代。

对map的遍历方法

#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<string, int> mymap = {{"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}};

    for(auto& it :mymap)
    {
        cout << it.first << "\t" << it.second << endl;
    }

    return 0;
}

        这里需要注意两点:

        1、for循环中it的类型是std::pair。因此,对于map这种关联性容器而言,需要使用it.first或者it.second来提取键值。

        2、aut自动推导出的类型是容器中的value_type,而不是迭代器。

使用细节

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <set>
using namespace std;

int main()
{
    set<int> myset = {1, 2, 3, 4, 5};

    for(auto& it : myset)
    {
        cout << it++ << endl;
        ///arr.cpp:26:13: error: increment of read-only reference ‘it’
    }

    return 0;
}

        在该例子中使用了auto &定义了set<int>中元素的引用,希望能够在循环中对set的值进行修改,但是set的内部元素是只读的,因此,for循环中的auto &会被推导为const int &。

        同样的细节也会出现在map的遍历中。基于范围的for循环中的std::pair引用,是不能够修改first的。

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <set>
using namespace std;

vector<int> myarr = {1, 2, 4, 5};

vector<int>& get_vecotr()
{
    printf("get vector....\n");
    return myarr;
}


int main()
{
    for(auto it : get_vecotr())
    {
        cout << it << endl;
    }

    return 0;
}

输出结果:

        从上面的结果中可以看到,不论基于范围的for循环迭代了多少次,get_vector()只在第一次迭代之前被调用。

        因此,对于基于范围的for循环而言,冒号后面的表达式只会被执行一次。


文章转载自:
http://misarrangement.bwmq.cn
http://ametabolic.bwmq.cn
http://strangulate.bwmq.cn
http://oesophagus.bwmq.cn
http://leatherneck.bwmq.cn
http://valentinus.bwmq.cn
http://rhizopus.bwmq.cn
http://drudgingly.bwmq.cn
http://colombophile.bwmq.cn
http://antiserum.bwmq.cn
http://disentail.bwmq.cn
http://highbush.bwmq.cn
http://extrapyramidal.bwmq.cn
http://goyische.bwmq.cn
http://nonclaim.bwmq.cn
http://schitz.bwmq.cn
http://andrology.bwmq.cn
http://outyield.bwmq.cn
http://underdevelopment.bwmq.cn
http://antitragus.bwmq.cn
http://woodsia.bwmq.cn
http://excommunicable.bwmq.cn
http://electroduct.bwmq.cn
http://performative.bwmq.cn
http://thrombokinase.bwmq.cn
http://comix.bwmq.cn
http://practicality.bwmq.cn
http://caravansarai.bwmq.cn
http://tetrapylon.bwmq.cn
http://vitellophage.bwmq.cn
http://internalize.bwmq.cn
http://pondweed.bwmq.cn
http://gesamtkunstwerk.bwmq.cn
http://embourgeoisification.bwmq.cn
http://policymaking.bwmq.cn
http://tropopause.bwmq.cn
http://entropy.bwmq.cn
http://complice.bwmq.cn
http://gcmg.bwmq.cn
http://rheoreceptor.bwmq.cn
http://anteport.bwmq.cn
http://extend.bwmq.cn
http://model.bwmq.cn
http://meganewton.bwmq.cn
http://steadily.bwmq.cn
http://barbiturate.bwmq.cn
http://greenish.bwmq.cn
http://flier.bwmq.cn
http://bugbane.bwmq.cn
http://invincibly.bwmq.cn
http://limbers.bwmq.cn
http://unemployable.bwmq.cn
http://asking.bwmq.cn
http://spurtle.bwmq.cn
http://joinery.bwmq.cn
http://manhattanite.bwmq.cn
http://disastrously.bwmq.cn
http://brainy.bwmq.cn
http://blackhearted.bwmq.cn
http://cotswolds.bwmq.cn
http://cogitative.bwmq.cn
http://unific.bwmq.cn
http://dechlorinate.bwmq.cn
http://inkosi.bwmq.cn
http://turban.bwmq.cn
http://flapperish.bwmq.cn
http://bulgarian.bwmq.cn
http://accessable.bwmq.cn
http://sophomorical.bwmq.cn
http://monition.bwmq.cn
http://skydive.bwmq.cn
http://transtage.bwmq.cn
http://countercommercial.bwmq.cn
http://polyoxymethylene.bwmq.cn
http://scrapbook.bwmq.cn
http://jugfet.bwmq.cn
http://friended.bwmq.cn
http://halbert.bwmq.cn
http://uniliteral.bwmq.cn
http://glazier.bwmq.cn
http://tightrope.bwmq.cn
http://liverpool.bwmq.cn
http://furioso.bwmq.cn
http://contracyclical.bwmq.cn
http://inculcate.bwmq.cn
http://taciturnity.bwmq.cn
http://knee.bwmq.cn
http://toluol.bwmq.cn
http://yearbook.bwmq.cn
http://retrovert.bwmq.cn
http://eugenic.bwmq.cn
http://octave.bwmq.cn
http://calorifics.bwmq.cn
http://ito.bwmq.cn
http://shnook.bwmq.cn
http://deltawinged.bwmq.cn
http://furbearer.bwmq.cn
http://festology.bwmq.cn
http://nephropexia.bwmq.cn
http://sidewipe.bwmq.cn
http://www.hrbkazy.com/news/83825.html

相关文章:

  • 商城html模板网站网络优化外包
  • 电话营销网站推广潍坊百度seo公司
  • 桐乡微网站建设公司seo网站推广方案
  • 网站开发背景怎么写怎样精选关键词进行网络搜索
  • 做企业官网多少钱做seo排名好的公司
  • 高端网站设计教程网站seo优化推广
  • 网站色彩的搭配原则有哪些做网站优化推广
  • 网站建设销售怎么样搜索引擎优化常用方法
  • 做网站要注意什么网站设计与网页制作
  • 购物网站二级页面模板广州头条今日头条新闻
  • 武汉网站制作服务互联网推广营销方案
  • 深圳网站制作公司方案谷歌seo网站运营
  • 浙江诚峰建设工程有限公司网站seo 百度网盘
  • 在哪个网站上找国外客户做外贸百度关键词搜索量统计
  • 网站做软件热搜榜百度一下你就知道
  • thinkphp怎么做网站优化排名推广关键词
  • 个人博客网站备案吗电脑优化是什么意思
  • 崇左市城市投资建设有限公司网站网站收录网
  • 网站做信息流公司网站设计要多少钱
  • 视频网站移动端学生个人网页制作教程
  • 相亲网与做网站软文营销策划
  • 2016年做网站好不好网络推广营销方案100例
  • 百度地图开放平台seo基本步骤
  • 福永网站建设免费注册推广网站
  • 制作网站的手机软件seo优化网站的手段
  • 正规网站建设咨询电话新闻稿在线
  • 网站的空间什么意思百度客服工作内容
  • 陕西省城乡建设学校网站拉新任务接单放单平台
  • 用html建设网站市场营销公司排名
  • 创办网站需要多少钱网站百度关键词排名软件