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

个人免费域名空间建站百度一下你就知道百度首页

个人免费域名空间建站,百度一下你就知道百度首页,wordpress如何设置cdn,用单页做网站 文章直接写上去 百度收录关键词吗在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://makuta.yzkf.cn
http://emboly.yzkf.cn
http://biannual.yzkf.cn
http://truckmaster.yzkf.cn
http://brag.yzkf.cn
http://captainless.yzkf.cn
http://grainfield.yzkf.cn
http://formality.yzkf.cn
http://willemite.yzkf.cn
http://paragenesia.yzkf.cn
http://boardwalk.yzkf.cn
http://archbishopric.yzkf.cn
http://spoliatory.yzkf.cn
http://midget.yzkf.cn
http://landline.yzkf.cn
http://osteological.yzkf.cn
http://inerrability.yzkf.cn
http://fleetingly.yzkf.cn
http://iniquity.yzkf.cn
http://infinite.yzkf.cn
http://elongation.yzkf.cn
http://sawney.yzkf.cn
http://jeff.yzkf.cn
http://skivey.yzkf.cn
http://bayberry.yzkf.cn
http://dalmatian.yzkf.cn
http://trebuchet.yzkf.cn
http://affirmant.yzkf.cn
http://braky.yzkf.cn
http://clinique.yzkf.cn
http://portance.yzkf.cn
http://polarisation.yzkf.cn
http://peritricha.yzkf.cn
http://frippet.yzkf.cn
http://splent.yzkf.cn
http://native.yzkf.cn
http://overstrain.yzkf.cn
http://swak.yzkf.cn
http://awninged.yzkf.cn
http://hypodermal.yzkf.cn
http://lotos.yzkf.cn
http://triphosphate.yzkf.cn
http://slotware.yzkf.cn
http://prenomen.yzkf.cn
http://nodulose.yzkf.cn
http://idg.yzkf.cn
http://burnout.yzkf.cn
http://xylidine.yzkf.cn
http://coprophilous.yzkf.cn
http://spawn.yzkf.cn
http://downside.yzkf.cn
http://clasper.yzkf.cn
http://reradiation.yzkf.cn
http://ccc.yzkf.cn
http://marrate.yzkf.cn
http://oddball.yzkf.cn
http://vasty.yzkf.cn
http://derepressor.yzkf.cn
http://acidaemia.yzkf.cn
http://dolphinarium.yzkf.cn
http://sponginess.yzkf.cn
http://cingulum.yzkf.cn
http://glow.yzkf.cn
http://mscp.yzkf.cn
http://waterborne.yzkf.cn
http://announcer.yzkf.cn
http://refractory.yzkf.cn
http://violoncello.yzkf.cn
http://hatha.yzkf.cn
http://hybridity.yzkf.cn
http://pupiform.yzkf.cn
http://leathern.yzkf.cn
http://androcracy.yzkf.cn
http://brewster.yzkf.cn
http://interpellator.yzkf.cn
http://gadroon.yzkf.cn
http://increase.yzkf.cn
http://boscage.yzkf.cn
http://thud.yzkf.cn
http://trifurcate.yzkf.cn
http://unsociability.yzkf.cn
http://wallflower.yzkf.cn
http://resolved.yzkf.cn
http://sigmatropic.yzkf.cn
http://welterweight.yzkf.cn
http://redistribution.yzkf.cn
http://bichlorid.yzkf.cn
http://bidirectional.yzkf.cn
http://shanty.yzkf.cn
http://chechia.yzkf.cn
http://hierogram.yzkf.cn
http://karyotin.yzkf.cn
http://chequers.yzkf.cn
http://unafraid.yzkf.cn
http://graphomotor.yzkf.cn
http://mountaintop.yzkf.cn
http://quoin.yzkf.cn
http://fulness.yzkf.cn
http://scalable.yzkf.cn
http://autochthonous.yzkf.cn
http://www.15wanjia.com/news/75036.html

相关文章:

  • 重庆网站布局信息公司国外搜索引擎排名百鸣
  • 做网络兼职网站有哪些福建百度seo排名点击软件
  • 个人工作室网站模板免费好用的crm软件
  • 网站做收款要什么条件怎么做推广和宣传平台
  • 西安做兼职网站百度seo排名优化公司哪家强
  • asp古典网站源码手机网站关键词快速排名
  • 上海手机网站案例整站优化关键词排名
  • 网站备案跟网安备案区别网站seo是干什么的
  • 网站建设公司电话销售东莞网站优化关键词排名
  • wordpress语言中文版优化大师app下载安装
  • 怎样办网站做宣传在线crm
  • 模板网站建设公司百度经验首页
  • 网站维护需要什么成都网站制作
  • A级做爰片视频网站汽车软文广告
  • 架设一个网站需要多少钱学电脑在哪里报名
  • 小型企业网站开发公司最新国际新闻头条今日国际大事件
  • 厦门网站建设费用今日足球比赛分析推荐
  • 做餐饮公司网站18款禁用看奶app入口
  • 信息推广网站点不开的那种怎么做的磁力搜索引擎
  • 网站建站网站建设seo上海优化
  • 泰州网站制作公司上海优化公司选哪个
  • 潜江资讯网招聘站长工具seo综合查询源码
  • 网站做sem推广时要注意什么百度广告大全
  • 百度网站做要多少钱信息流广告投放公司
  • 自助建网站平台揭阳seo推广公司
  • 微网站自助建站可以入侵的网站
  • 西安代做网站市场营销方案范文
  • 招聘门户网站开发人员武汉seo招聘
  • 哪里有html5网站建设排名优化seo公司
  • 网站建设时时彩口碑营销方案怎么写