如何初始化用 auto 关键字声明的循环计数器?
How to initialize the loop counter declared with the auto keyword?
这是我的代码:
#include <iostream>
#include <vector>
void cumulative_sum_with_decay(std::vector<double>& v)
{
for (auto i = 2; i < v.size(); i++) {
v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
}
}
void printv(std::vector<double>& v)
{
std::cout << "{";
for (auto i = 0; i < v.size() - 1; i++) {
std::cout << i << ", ";
}
std::cout << v[v.size() - 1] << "}\n";
}
int main()
{
auto v = std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cumulative_sum_with_decay(v);
printv(v);
}
当我尝试编译和 运行 这个程序时,我收到这些警告:
$ clang++ -std=c++11 -Wextra foo.cpp && ./a.out
foo.cpp:6:24: warning: comparison of integers of different signs: 'int' and 'std::__1::vector<double,
std::__1::allocator<double> >::size_type' (aka 'unsigned long') [-Wsign-compare]
for (auto i = 2; i < v.size(); i++) {
~ ^ ~~~~~~~~
foo.cpp:14:24: warning: comparison of integers of different signs: 'int' and 'unsigned long'
[-Wsign-compare]
for (auto i = 0; i < v.size() - 1; i++) {
~ ^ ~~~~~~~~~~~~
2 warnings generated.
{0, 1, 2, 3, 4, 5, 6, 7, 8, 8.68781}
如何初始化这些用 auto
声明的循环计数器,使代码安全且没有警告?
请注意,虽然我这里有一个小向量,但我正在尝试学习如何使用 auto
编写安全代码,即使向量太大以至于 i
中的值可能超过整数范围。
auto
声明变量的类型是从初始值设定项推导出来的。给定 2
或 0
它将是 int
.
您可以使用显式类型的初始值设定项指定类型。例如
for (auto i = static_cast<decltype(v.size())>(2); i < v.size(); i++) {
auto i=2
意味着 a 是 int
类型 & vector.size()
是 size_t 这就是为什么 -Wsign-compare 来了。你可以使用
for (std::size_t i = 0, max = vec.size(); i != max; ++i)
或
for (auto it = vec.begin(), end = vec.end(); it != end; ++it)
您可以使用 'decltype(v.size())' 来获得正确的类型。
for (decltype(v.size()) i = 2; i < v.size(); i++)
如果你关心精确匹配类型,你可以为此编写一个帮助程序(live example):
// Concepts would help here.
template<typename Cont, typename T>
auto as_size_type(const Cont& cont, T init) {
return static_cast<decltype(std::size(cont))>(init);
}
用法:
for (auto i = as_size_type(v, 2); i < v.size(); i++) {
v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
}
我使用 std::size
来处理数组与具有 ::size_type
的 类 之间的差异,但如果 std::size
不可用。同样,自动 return 类型推导可以使用 decltype
等代替 C++11.
这不是您问题的答案,但它解决了您使用迭代器的问题。
void cumulative_sum_with_decay(std::vector<double>& v)
{
assert(v.size()>2);
for (auto it = v.begin()+2; it!=v.end(); it++) {
*it = 0.167 * *(it-2) + 0.333 * *(it-1) + 0.5 * *it;
}
}
void printv(std::vector<double>& v)
{
assert(v.size()>0);
std::cout << "{";
for (auto it = v.begin(); it != v.end() - 1; it++) {
std::cout << *it << ", ";
}
std::cout << v.back() << "}\n";
}
int main()
{
auto v = std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cumulative_sum_with_decay(v);
printv(v);
}
这是我的代码:
#include <iostream>
#include <vector>
void cumulative_sum_with_decay(std::vector<double>& v)
{
for (auto i = 2; i < v.size(); i++) {
v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
}
}
void printv(std::vector<double>& v)
{
std::cout << "{";
for (auto i = 0; i < v.size() - 1; i++) {
std::cout << i << ", ";
}
std::cout << v[v.size() - 1] << "}\n";
}
int main()
{
auto v = std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cumulative_sum_with_decay(v);
printv(v);
}
当我尝试编译和 运行 这个程序时,我收到这些警告:
$ clang++ -std=c++11 -Wextra foo.cpp && ./a.out
foo.cpp:6:24: warning: comparison of integers of different signs: 'int' and 'std::__1::vector<double,
std::__1::allocator<double> >::size_type' (aka 'unsigned long') [-Wsign-compare]
for (auto i = 2; i < v.size(); i++) {
~ ^ ~~~~~~~~
foo.cpp:14:24: warning: comparison of integers of different signs: 'int' and 'unsigned long'
[-Wsign-compare]
for (auto i = 0; i < v.size() - 1; i++) {
~ ^ ~~~~~~~~~~~~
2 warnings generated.
{0, 1, 2, 3, 4, 5, 6, 7, 8, 8.68781}
如何初始化这些用 auto
声明的循环计数器,使代码安全且没有警告?
请注意,虽然我这里有一个小向量,但我正在尝试学习如何使用 auto
编写安全代码,即使向量太大以至于 i
中的值可能超过整数范围。
auto
声明变量的类型是从初始值设定项推导出来的。给定 2
或 0
它将是 int
.
您可以使用显式类型的初始值设定项指定类型。例如
for (auto i = static_cast<decltype(v.size())>(2); i < v.size(); i++) {
auto i=2
意味着 a 是 int
类型 & vector.size()
是 size_t 这就是为什么 -Wsign-compare 来了。你可以使用
for (std::size_t i = 0, max = vec.size(); i != max; ++i)
或
for (auto it = vec.begin(), end = vec.end(); it != end; ++it)
您可以使用 'decltype(v.size())' 来获得正确的类型。
for (decltype(v.size()) i = 2; i < v.size(); i++)
如果你关心精确匹配类型,你可以为此编写一个帮助程序(live example):
// Concepts would help here.
template<typename Cont, typename T>
auto as_size_type(const Cont& cont, T init) {
return static_cast<decltype(std::size(cont))>(init);
}
用法:
for (auto i = as_size_type(v, 2); i < v.size(); i++) {
v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
}
我使用 std::size
来处理数组与具有 ::size_type
的 类 之间的差异,但如果 std::size
不可用。同样,自动 return 类型推导可以使用 decltype
等代替 C++11.
这不是您问题的答案,但它解决了您使用迭代器的问题。
void cumulative_sum_with_decay(std::vector<double>& v)
{
assert(v.size()>2);
for (auto it = v.begin()+2; it!=v.end(); it++) {
*it = 0.167 * *(it-2) + 0.333 * *(it-1) + 0.5 * *it;
}
}
void printv(std::vector<double>& v)
{
assert(v.size()>0);
std::cout << "{";
for (auto it = v.begin(); it != v.end() - 1; it++) {
std::cout << *it << ", ";
}
std::cout << v.back() << "}\n";
}
int main()
{
auto v = std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cumulative_sum_with_decay(v);
printv(v);
}