首页 > 编程语言 >C++for循环新用法

C++for循环新用法

时间:2023-02-28 20:47:43浏览次数:34  
标签:arc cout int auto C++ 用法 range 循环 include

1、拷贝range的元素时,使用for(auto x : range).

2、修改range的元素时,使用for(auto & x : range).

3、只读range的元素时,使用for(const auto & x : range).

#include <iostream>
#include <vector>
using namespace std;
int main() {
    char arc[] = "http://c.biancheng.net/cplus/11/";
    //for循环遍历普通数组
    for (char ch : arc) {
        cout << ch;
    }
    cout << '!' << endl;
    vector<char>myvector(arc, arc + 23);
    //for循环遍历 vector 容器
    for (auto ch : myvector) {
        cout << ch;
    }
    cout << '!';
    return 0;
}
#include <iostream>
using namespace std;
int main() {
    for (int num : {1, 2, 3, 4, 5}) {
        cout << num << " ";
    }
    return 0;
}

标签:arc,cout,int,auto,C++,用法,range,循环,include
From: https://www.cnblogs.com/linhongyu0090/p/17165893.html

相关文章

  • C++刷题笔记
    初始化string数组stringnumbers[12]={{"1"},{"2"},{"10"},{"11"},{"23"},{"25"},{"31"},{"36"},{"37"},{"102"},{"325"},{"438"}};填充for(in......
  • C++substr()函数
    C++中substr函数有三种用法,如下所示:假设strings(“student12”);stringx=s.substr()//默认时的长度为从开始位置到尾stringy=s.substr(5)......
  • C++string大小写转换
    #include<iostream>#include<string>#include<algorithm>usingnamespacestd;intmain(){stringstr="ancdANDG";cout<<"转换前的字符串:"<<str......
  • C++里的memset
    memset函数是内存赋值函数,用来给某一块内存空间进行赋值的;包含在<string.h>头文件中,可以用它对一片内存空间逐字节进行初始化;原型为:void*memset(void*s,intv,......
  • 控制台安装虚拟环境常见指令和用法
    查看conda信息(版本,安装位置等等)condainfo创建一个新的虚拟环境condacreate-nyour_env_namecondacreate-nyour_env_namepython=3.9.2(带python版本的创建)激......
  • C++
                   ......
  • 1238. 循环码排列 (Medium)
    问题描述1238.循环码排列(Medium)给你两个整数n和start。你的任务是返回任意(0,1,2,,...,2^n-1)的排列p,并且满足:p[0]=startp[i]和p[i+1]的二进制表示形......
  • c++11 智能指针 之 shared_ptr
    使用://newarray_PRTA(unsignedchar,fileBuffer,filesize+1);//newpoint_PRT(StructA)mStructA;mStructA=make_shared<StructA>(111);shared_ptr不用手动去释......
  • c++11 assert 静态断言
    断言,是指在对某一件事情或者事物下的一种主观性非常强的言论:如果事实与断言不符,则会认为错误,C++里面的assert也是起这样一个作用。1.什么是assertassert宏的原型定义在<a......
  • OPENSSL RSA 与JAVA C++ RSA 生成 公钥 私钥
    客户端和服务端通信一般可能会采用非对称加密,这样服务端私钥加密,客服端解密,客户端加密,服务端解密。服务端采用JAVA,这个很方便,客户端因为是多平台,采用OPENSSL可能会遇到的问......