是否可以在 for 循环中声明两个不同类型的变量

时间:2023-03-22
本文介绍了是否可以在 for 循环中声明两个不同类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 C++ 中 for 循环的初始化体中声明两个不同类型的变量?

Is it possible to declare two variables of different types in the initialization body of a for loop in C++?

例如:

for(int i=0,j=0 ...

定义两个整数.我可以在初始化体中定义一个 int 和一个 char 吗?这将如何完成?

defines two integers. Can I define an int and a char in the initialization body? How would this be done?

推荐答案

C++17:是的!您应该使用 结构化绑定声明.自 gcc-7 和 clang-4.0(clang live example)以来,gcc 和 clang 已支持该语法.这允许我们像这样解包一个元组:

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:

for (auto [i, f, s] = std::tuple{1, 1.0, std::string{"ab"}}; i < N; ++i, f += 1.5) {
    // ...
}

以上会给你:

  • int i 设置为 1
  • double f 设置为 1.0
  • std::string s 设置为 "ab"
  • int i set to 1
  • double f set to 1.0
  • std::string s set to "ab"

对于这种声明,请确保#include .

Make sure to #include <tuple> for this kind of declaration.

如果你想命名一个类型,你可以在 tuple 中指定确切的类型,就像我在 std::string 中所做的那样.例如:

You can specify the exact types inside the tuple by typing them all out as I have with the std::string, if you want to name a type. For example:

auto [vec, i32] = std::tuple{std::vector<int>{3, 4, 5}, std::int32_t{12}}

它的一个具体应用是迭代地图,获取键和值,

A specific application of this is iterating over a map, getting the key and value,

std::unordered_map<K, V> m = { /*...*/ };
for (auto& [key, value] : m) {
   // ...
}

查看现场示例此处

C++14:您可以通过添加基于类型的 std::get 来执行与 C++11(如下)相同的操作.因此,在下面的示例中,您可以使用 std::get(t),而不是 std::get<0>(t).

C++14: You can do the same as C++11 (below) with the addition of type-based std::get. So instead of std::get<0>(t) in the below example, you can have std::get<int>(t).

C++11:std::make_pair 允许您这样做,以及 std::make_tuple 用于两个以上的对象.

C++11: std::make_pair allows you to do this, as well as std::make_tuple for more than two objects.

for (auto p = std::make_pair(5, std::string("Hello World")); p.first < 10; ++p.first) {
    std::cout << p.second << std::endl;
}

std::make_pair 将返回 std::pair 中的两个参数.元素可以通过 .first.second 访问.

std::make_pair will return the two arguments in a std::pair. The elements can be accessed with .first and .second.

对于两个以上的对象,您需要使用 std::tuple

For more than two objects, you'll need to use a std::tuple

for (auto t = std::make_tuple(0, std::string("Hello world"), std::vector<int>{});
        std::get<0>(t) < 10;
        ++std::get<0>(t)) {
    std::cout << std::get<1>(t) << std::endl; // cout Hello world
    std::get<2>(t).push_back(std::get<0>(t)); // add counter value to the vector
}

std::make_tuple 是一个可变参数模板,它将构造一个包含任意数量参数的元组(当然有一些技术限制).可以使用 std::get(tuple_object)

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 with std::get<INDEX>(tuple_object)

在 for 循环体中,您可以轻松地为对象设置别名,但您仍然需要使用 .firststd::get 作为 for 循环条件和更新表达式

Within the for loop bodies you can easily alias the objects, though you still need to use .first or std::get for the for loop condition and update expression

for (auto t = std::make_tuple(0, std::string("Hello world"), std::vector<int>{});
        std::get<0>(t) < 10;
        ++std::get<0>(t)) {
    auto& i = std::get<0>(t);
    auto& s = std::get<1>(t);
    auto& v = std::get<2>(t);
    std::cout << s << std::endl; // cout Hello world
    v.push_back(i); // add counter value to the vector
}


C++98 和 C++03 您可以明确命名 std::pair 的类型.虽然没有标准方法可以将其概括为两种以上类型:


C++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:

for (std::pair<int, std::string> p(5, "Hello World"); p.first < 10; ++p.first) {
    std::cout << p.second << std::endl;
}

这篇关于是否可以在 for 循环中声明两个不同类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Visual Studio 中的 Cygwin 下一篇:“for"循环中的后增量和预增量产生相同的输

相关文章

最新文章