C++ 通用 int 数组和矢量迭代器
c++ general int array and vector iterator
在下面的代码中,我需要定义一个可以迭代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;
}
相关文章:
- 迭代器库中的 std::size() 不适用于传递给函数的 C 样式数组
- 自定义 STL 兼容迭代器,用于迭代 2D 数组类的列
- 获取 c++ 中具有恒定长度的数组的迭代器
- 为自定义数组实现迭代器
- 原始数组和 std::array 在 clang++ 和 VC++ 上的不同迭代器行为
- C++程序在迭代到数组的最后一个元素时崩溃
- 有没有办法创建一个花哨的迭代器和相应的新数组,以便检查每个索引的索引值的条件?
- C 迭代器范围使用指针到数组
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 是否可以在 C 数组中使用迭代器?
- 迭代器和数组
- STL 迭代器和数组的关系
- 使用迭代器数组读取稀疏信息
- 为什么迭代2D数组的行主要比列主要快
- c++ 11迭代锯齿数组
- 一元函数,接受迭代器元组并产生基本类型
- 迭代c++数组
- 迭代器元组.operator++的实现
- 如何在C和C++中迭代字符串数组
- 用step迭代1D数组和2D数组