C++ 通用 int 数组和矢量迭代器

c++ general int array and vector iterator

本文关键字:迭代器 数组 通用 int C++      更新时间:2023-10-16

在下面的代码中,我需要定义一个可以迭代vector<int>int[100]的迭代器。如何在这里定义mi

template<class arraytype>
void array_show(arraytype array, size_t arraySize)
{
    // how to define mi????
    for (mi = array; array != m.end(); array++)
        std::cout << " " << *mi << std::endl;
}

尝试以下操作

#include <iostream>
#include <vector>
#include <iterator>
template<class arraytype>
void array_show( const arraytype &array )
{
    for ( const auto &x : array ) std::cout << x << ' ';
    std::cout << std::endl;
}
int main() 
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::vector<int> v( std::begin( a ), std::end( a ) );
    array_show( a );
    std::endl( std::cout );
    array_show( v );
    std::endl( std::cout );
    return 0;
}

程序输出为

1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 

另一种方法是使用迭代器。例如(这里显示了两个函数定义)

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
template<class arraytype>
void array_show( const arraytype &array )
{
    for ( const auto &x : array ) std::cout << x << ' ';
    std::cout << std::endl;
}
template <class InputIterator>
void array_show( InputIterator first, InputIterator last )
{
    typedef typename std::iterator_traits<InputIterator>::value_type value_type;
    std::copy( first, last, 
               std::ostream_iterator<value_type>( std::cout, " ") );
    std::cout << std::endl;     
}
int main() 
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::vector<int> v( std::begin( a ), std::end( a ) );
    array_show( std::begin( a ), std::end( a ) );
    std::endl( std::cout );
    array_show( std::begin( v ), std::end( v ) );
    std::endl( std::cout );

    return 0;
}

程序输出与上述相同

1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 

代替算法 std::copy,你可以自己编写一个循环。例如

template <class InputIterator>
void array_show( InputIterator first, InputIterator last )
{
    for ( ; first != last; ++first )
    {
        std::cout << *first << ' '; 
    }           
    std::cout << std::endl;     
}

定义函数以接受开始和结束迭代器。然后,您可以将函数用于任何形式的可迭代对象

template<typename Iter>
void array_show(Iter begin, Iter end)
{
    // how to define mi????
    for (Iter it = begin; it != end; it++)
        std::cout << " " << *it << std::endl;
}

然后,您可以按如下方式调用函数

int main(int argc, char *argv[]) {
    std::vector<int> vec= { 3, 1, 4, 1, 5, 9, 2, 6 };
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6 };
    array_show(vec.begin(), vec.end());
    array_show(std::begin(arr), std::end(arr));

}

如果要将数组作为引用和向量传递,则应创建两个不同的函数重载来支持这两个重载。这可能看起来像您的意图,因为您的函数签名旨在接受大小和对象。

template<typename Ty, size_t size>
void array_show(Ty(&arr)[size])
{
    for (size_t i = 0; i < size; i++)
        std::cout << " " << arr[i] << std::endl;
}

随后将其称为

int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6 };
array_show(arr);
<</div> div class="answers">

如果您需要按照显示的方式使用它:

template<class arraytype>
void array_show(arraytype array, size_t arraySize)
{
    auto end = &(array[arraySize]);
    for (mi = &(array[0]); array != end; ++array)
        std::cout << " " << *mi << std::endl;
}

正如vector operator[]所定义的,构造&(array[...])适用于vector数组和普通数组。

但最好是:

template <class Iter>
void array_show(Iter begin, Iter end)
{
    for (Iter it = begin, it != end; ++it)
        std::cout << " " << *it << std::endl;
}

然后:

vector<int> vi;
int ai[100];
//fill with data
array_show(vi.begin(). vi.end());
array_show(ai, ai + 100);
下面是

一个示例。重要的部分是使用 arraytype& - 一个引用,这样常规数组就不会衰减到指针 - 这样就可以在array_show内读取它的大小。

template<class arraytype>
void array_show(arraytype& array, size_t arraySize)
{
    // how to define mi????
    for (auto mi = std::begin(array); mi != std::end(array); mi++)
        std::cout << " " << *mi << std::endl;
}