C++一般int数组和向量迭代器
问题描述:
在下面的代码中,我需要定义一个迭代器,它可以迭代vector<int>
和int[100]
。如何在这里定义mi
?C++一般int数组和向量迭代器
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;
}
答
下面是一个例子。重要的部分是使用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;
}
答
定义你的函数来接受开始和结束迭代器。然后,您可以使用您的功能对任何形式iterables
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);
答
如果你需要使用它,你就有了办法:
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);
答
尝试使用以下
#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 ::的副本,你可以自己写一个循环。例如
template <class InputIterator>
void array_show(InputIterator first, InputIterator last)
{
for (; first != last; ++first)
{
std::cout << *first << ' ';
}
std::cout << std::endl;
}
答
刚写简单的功能与内建阵列上循环,并通过那里指针到std ::矢量数据
std::vector<int> a;
foo(a.data(), a.size());
要么使用'auto',或在一个迭代器对,其类型就可以推断出。 – Quentin
不需要使用迭代器。看到我的答案。 – senfen