是否可以在for循环中声明两个不同类型的变量?
是否可以在 C++ 的 for 循环的初始化主体中声明两个不同类型的变量?
例如:
for(int i=0,j=0 ...
定义两个整数。我可以在初始化主体中定义一个 int
和一个 char
吗?这将如何完成?
Is it possible to declare two variables of different types in the initialization body of a for loop in C++?
For example:
for(int i=0,j=0 ...
defines two integers. Can I define an int
and a char
in the initialization body? How would this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不 - 但从技术上讲,有一个解决方法(除非被迫,否则我实际上不会使用它):
No - but technically there is a work-around (not that i'd actually use it unless forced to):
不可能,但您可以这样做:
或者,使用附加括号显式限制
f
和i
的范围:Not possible, but you can do:
Or, explicitly limit the scope of
f
andi
using additional brackets:C++17:是的!您应该使用结构化绑定声明。自 gcc-7 和 clang-4.0 起,gcc 和 clang 就支持该语法(clang 实例)。这允许我们像这样解包一个元组:
上面的代码将为您提供:
int i
set to1
double f
set to1.0
std::string s
设置为"ab"
确保为此类声明
#include
。如果您想命名一个类型,您可以通过像我使用
std::string
那样将它们全部输入来指定元组内的确切类型。例如:它的一个具体应用是迭代地图,获取键和值,
请参见此处
C++14:您可以执行与 C++11(如下)相同的操作,并添加基于类型的
std::get
。因此,您可以使用std::get(t)
,而不是下面示例中的std::get<0>(t)
。C++11:
std::make_pair
允许您执行此操作,以及std::make_tuple
对于两个以上的对象。std::make_pair
将返回std::pair
中的两个参数。可以使用.first
和.second
访问这些元素。对于两个以上的对象,您需要使用
std::tuple
std::make_tuple
是一个可变参数模板,它将构造一个包含任意数量参数的元组(当然有一些技术限制)。可以使用std::get(tuple_object)
通过索引访问元素在 for 循环体内,您可以轻松地为对象添加别名,但仍然需要使用
.first 或
std::get
用于 for 循环条件和更新表达式C++98 和 C++03 您可以显式命名
std 的类型::配对
。不过,没有标准方法可以将其概括为两种以上类型:C++17: Yes! You should use a structured binding declaration. The syntax has been supported in gcc and clang since gcc-7 and clang-4.0 (clang live example). This allows us to unpack a tuple like so:
The above will give you:
int i
set to1
double f
set to1.0
std::string s
set to"ab"
Make sure to
#include <tuple>
for this kind of declaration.You can specify the exact types inside the
tuple
by typing them all out as I have with thestd::string
, if you want to name a type. For example:A specific application of this is iterating over a map, getting the key and value,
See a live example here
C++14: You can do the same as C++11 (below) with the addition of type-based
std::get
. So instead ofstd::get<0>(t)
in the below example, you can havestd::get<int>(t)
.C++11:
std::make_pair
allows you to do this, as well asstd::make_tuple
for more than two objects.std::make_pair
will return the two arguments in astd::pair
. The elements can be accessed with.first
and.second
.For more than two objects, you'll need to use a
std::tuple
std::make_tuple
is a variadic template that will construct a tuple of any number of arguments (with some technical limitations of course). The elements can be accessed by index withstd::get<INDEX>(tuple_object)
Within the for loop bodies you can easily alias the objects, though you still need to use
.first
orstd::get
for the for loop condition and update expressionC++98 and C++03 You can explicitly name the types of a
std::pair
. There is no standard way to generalize this to more than two types though:你不能在初始化中声明多个类型,但是你可以分配给多个类型EG,
只需在它们自己的作用域中声明它们即可。
You can't declare multiple types in the initialization, but you can assign to multiple types E.G.
Just declare them in their own scope.
我认为最好的方法是xian的回答。
但是...
# 嵌套for循环
这种方法很脏,但可以在所有版本中解决。
所以,我经常在宏函数中使用它。
其他 1.
它还可以用于
声明局部变量
和初始化全局变量
。附加 2.
很好的例子:具有宏功能。
(如果无法使用最佳方法,因为它是一个for循环宏)
# If-语句技巧
如果你想要初始化为
0
或nullptr
,你可以使用这个技巧。但由于阅读困难,我不推荐这样做。
这看起来像是错误。
I think best approach is xian's answer.
but...
# Nested for loop
This approach is dirty, but can solve at all version.
so, I often use it in macro functions.
Additional 1.
It can also be used to
declare local variables
andinitialize global variables
.Additional 2.
Good example : with macro function.
(If best approach can't be used because it is a for-loop-macro)
# If-statement trick
If you want initialize to
0
ornullptr
, you can use this trick.but I don't recommend this because of hard reading.
and it seems like bug.
请参阅“有没有办法在 for 循环中定义两种类型的变量?”是涉及嵌套多个 for 循环的另一种方式。与 Georg 的“结构技巧”相比,另一种方法的优点是它(1)允许您混合使用静态和非静态局部变量,(2)它允许您拥有不可复制的变量。缺点是它的可读性较差,而且效率可能较低。
See "Is there a way to define variables of two types in for loop?" for another way involving nesting multiple for loops. The advantage of the other way over Georg's "struct trick" is that it (1) allows you to have a mixture of static and non-static local variables and (2) it allows you to have non-copyable variables. The downside is that it is far less readable and may be less efficient.
您也可以在 C++ 中使用如下所示。
Also you could use like below in C++.
定义宏:
请记住,您的变量范围也不会以这种方式位于 for 循环内。
Define a macro:
Just remember, your variable scopes will not be within the for loop this way either.