加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么for循环不能定义两个范围的变量?

发布时间:2020-12-16 06:46:23 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is it possible to declare two variables of different types in a for loop?????????????????????????????????????6个 我很困惑,“’iter’未在此范围内声明”错误. #include vectorusing std::vector;int main(){ vectorint vec{1,2,3,4,5,6
参见英文答案 > Is it possible to declare two variables of different types in a for loop?????????????????????????????????????6个
我很困惑,“’iter’未在此范围内声明”错误.

#include <vector>

using std::vector;

int main()
{
    vector<int> vec{1,2,3,4,5,6};
    for(std::size_t i,vector<int>::iterator iter=vec.begin();iter!=vec.end();++i,++iter)
    {
        //do something
    }
}

解决方法

就像你能做的那样

int a = 10,b = 20;

for循环的第一部分做同样的事情.既然你做不到

int a = 10,double b = 20;

同样的事情适用于for循环.

从技术上讲,你可以有两种不同的类型,因为你可以在同一行上声明一个类型和指向同一类型的指针

int i = 0,*b = nullptr;

这也适用于for循环.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读