微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在动态分配数组中使用迭代器的问题

如何解决在动态分配数组中使用迭代器的问题

我有一个类型为k的变量int,用于设置动态分配的int数组的长度:

int *Numbers = new int[k];

但是由于这个原因,我无法遍历数组,但出现错误

"no matching begin function was found required for this range-based for statement"

我也无法使用size()获得数组的长度;

这是完整的代码

#include <iostream>
using namespace std;
int main()
{
    int b,k;
    cin >> b >> k;
    int *Numbers = new int[k];
    for (int i : Numbers) {// (There is a error)
    }
    for (int i = 0; i < size(Numbers); i++) {

    }
}

解决方法

最好使用std::vector而不是std::array。 (就像提到的@tadman。)

以下是您使用std::vector的代码:

#include <iostream>
#include <vector>

int main()
{
    int b,k;

    std::cin >> b >> k;

    std::vector<int> Numbers(b,k); // Fills the vector "Numbers" with nth number of elements with each element as a copy of val.
    for (int i : Numbers) 
        std::cout << i << std::endl;
        
    for (int i = 0; i < Numbers.size(); i++) 
        std::cout << Numbers[i] << std::endl;
        
    
     return 0;
}

说我要10 elements,并加上数字5。

输出:

10
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5

也不要考虑使用using namespace std;

,

简单推荐的解决方案是使用std::vector,但是,如果您确实想要动态分配的数组并对其使用类似迭代器的功能,则可以使用iterator_range中的boost library,这样您就可以为其创建一个迭代器范围,从而使其可用于基于范围的循环以及std::size之类的函数中。

Live demo

#include <iostream>
#include<boost/range.hpp>

int main()
{
    int k = 5;
    int *Numbers = new int[k]{1,4,5,7,8};
    
    auto arr = boost::make_iterator_range(Numbers,Numbers + k); 

    for (int i : arr) {  //range based loop
        std::cout << i << " ";
    }
    
    std::cout << std::endl << "Size: " << arr.size();  //print size
    //or std::size(arr);
}

输出:

1 4 5 7 8 
Size: 5
,

基于范围的for循环适用于数组,但不适用于指针。实际问题是数组实际上是一个指针,而不是数组。请尝试使用简单数组。

,

使用指针存在很多问题。解决您问题的简单方法是使用向量

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int b,k;
    cin >> b >> k;
    vector<int> Numbers(k);
    for (int i : Numbers) {
         cout << i << endl;
    }
    for (int i = 0; i < Numbers.size(); i++) {
         cout << Numbers[i] << endl;
    }
}
,

C数组没有默认的迭代器,因此当您使用如下语句时,没有用于遍历数组的begin()和end()函数:

for (int i : Numbers)

您可以检查range-for reference

range_expression-表示合适序列的任何表达式(为数组或对象定义了开始和结束成员函数或自由函数,请参见下文)或大括号初始列表。

,

好的,因此,由于动态数组没有默认的迭代器,因此请不要使用for-each循环,而应考虑使用常规的for循环。 另外,请注意size函数对数组(或动态数组)不起作用,您需要记住它的大小,因为不可能仅从指针获取大小。因此,此代码将起作用:

#include <iostream>
using namespace std;
int main()
{
    int b,k;
    cin >> b >> k;
    int *Numbers = new int[k];
    const int SIZE = k;
    for (int i = 0; i < SIZE; i++) {
        cout << i << ' ';
    }
} 
,

如果要遍历数组,则需要使用*Numbers来解除对*的引用,因为*Numbers是指向整数的指针,该整数指向数组的第一个元素。例如:

#include <iostream>

using namespace std;

int main()
{
    int k = 10;
    int *numbers = new int[k];
     
    //filling the array 
    for(int i = 0 ; i < k ; ++i) {
        *(numbers + i) = i ;
    }
    
    //output array element
    for(int i = 0 ; i < k ; ++i) {
        cout << numbers + i << " is the address of "<<*(numbers + i) << endl;
    }

    return 0;
}

输出为:

0x6f1750 is the address of 0
0x6f1754 is the address of 1
0x6f1758 is the address of 2
0x6f175c is the address of 3
0x6f1760 is the address of 4
0x6f1764 is the address of 5
0x6f1768 is the address of 6
0x6f176c is the address of 7
0x6f1770 is the address of 8
0x6f1774 is the address of 9

不幸的是,您无法使用*Numbers获得数组的大小,因为它不是数组而是指针。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?