C-Array迭代器C ++合规性问题 [英] C-Array iterator C++ compliance problem
问题描述
大家好,
我写了两个函数来使一个C-Style数组玩得很好用
std :: algorithms如图所示:
>
模板< typename T>
T begin(T& t)
{
返回t;
}
模板< typename T>
T end(T& t)
{
返回(t + sizeof(t)/ sizeof(t [0]));
}
使用std ::算法很简单。因此......
const char * c_style_array [] = {" one",two,three};
std :: for_each(
begin(c_style_array),
end(c_style_array),
...做点什么);
生活很美好,
但是......
显然,我的编译器不符合标准。在转到更符合标准的
编译器时,我的代码坏了。
如何重新编写我的函数以使我的代码再次运行?这个
违规是通过代码库非常多的。
谢谢,
M
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
使用
std :: algorithms如图所示:
模板< typename T>
T begin(T& t)
{
返回t;
}
模板< typename T>
T end(T& t)
{
返回(t + sizeof(t)/ sizeof(t [0]));
}
为什么? C风格阵列衰减到指针,指针是迭代器,至少在语义上是
,所以它们在算法上运行得很好。你
只是不需要使用''begin''或''end''。使用''c_style_array''和
''c_style_array + size''。
[...]
V
mmacrobert写道:如何重新编写我的函数以使我的代码再次运行?通过代码库,这种不合规是非常多的。
模板< typename T,unsigned N>
T * begin(T(& array)[N]){return array + 0; }
模板< typename T,unsigned N>
T * end(T(& array)[N]){return array + N; }
[见 http:/ /www.gotw.ca/resources/clcm.htm 了解有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
mmacrobert写道:大家好,
我写了2个函数来制作一个C风格的数组播放很好用
std :: algorithms如图所示:
模板< typename T>
T begin(T& t)
{
返回t;
}
模板< typename T>
T end(T& t)
{
返回(t + sizeof(t)/ sizeof( t [0]));
}
使用std :: algorithm非常简单。因此......
const char * c_style_array [] = {" one",two"" 3"};
std :: for_each(
begin(c_style_array),
结束(c_style_array),
......做点什么);
生活很美好,
但是。 ..
显然,我的编译器不符合标准。在转向更兼容的编译器时,我的代码坏了。
如何重新编写我的函数以使我的代码再次运行?这个
违规通过代码库非常多。
谢谢,
M
查看实现boost :: array:
http:// boost.org/doc/html/array.html
干杯! --M
[见 http:// www .gotw.ca / resources / clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
Hi Everyone,
I wrote 2 functions to make a C-Style array play nice with
std::algorithms as shown:
template <typename T>
T begin(T& t)
{
return t;
}
template <typename T>
T end(T& t)
{
return (t + sizeof(t)/sizeof(t[0]));
}
Usage with a std::algorithm was easy. Thus...
const char* c_style_array[] = {"one","two","three"};
std::for_each(
begin(c_style_array),
end(c_style_array),
...do something);
Life was beautiful,
BUT...
Apparently, my compiler was not standards compliant. On moving to a
more compliant compiler my code broke.
How can I re-write my functions to make my code work again? This
non-compliance is pretty prolific through the codebase.
Thanks,
M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
mmacrobert wrote:I wrote 2 functions to make a C-Style array play nice with
std::algorithms as shown:
template <typename T>
T begin(T& t)
{
return t;
}
template <typename T>
T end(T& t)
{
return (t + sizeof(t)/sizeof(t[0]));
}
WHY? "C-Style arrays" decay to pointers, and pointers are iterators,
at least semantically, so they work just fine with algorithms. You
just don''t need to use ''begin'' or ''end''. Use ''c_style_array'' and
''c_style_array + size''.
[...]
V
mmacrobert wrote:How can I re-write my functions to make my code work again? This
non-compliance is pretty prolific through the codebase.
template < typename T, unsigned N >
T *begin ( T (&array)[N] ) { return array + 0; }
template < typename T, unsigned N >
T *end ( T (&array)[N] ) { return array + N; }
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
mmacrobert wrote:Hi Everyone,
I wrote 2 functions to make a C-Style array play nice with
std::algorithms as shown:
template <typename T>
T begin(T& t)
{
return t;
}
template <typename T>
T end(T& t)
{
return (t + sizeof(t)/sizeof(t[0]));
}
Usage with a std::algorithm was easy. Thus...
const char* c_style_array[] = {"one","two","three"};
std::for_each(
begin(c_style_array),
end(c_style_array),
...do something);
Life was beautiful,
BUT...
Apparently, my compiler was not standards compliant. On moving to a
more compliant compiler my code broke.
How can I re-write my functions to make my code work again? This
non-compliance is pretty prolific through the codebase.
Thanks,
M
Check out the implementation of boost::array:
http://boost.org/doc/html/array.html
Cheers! --M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于C-Array迭代器C ++合规性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!