問題描述
是否可以在 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 to1
double f
set to1.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
,而不是 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
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 循環體中,您可以輕松地為對象設置別名,但您仍然需要使用 .first
或 std::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模板網!