久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否可以在 for 循環中聲明兩個不同類型的變量

Is it possible to declare two variables of different types in a for loop?(是否可以在 for 循環中聲明兩個不同類型的變量?)
本文介紹了是否可以在 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 久久精品国产亚洲 | 久久久久久久久蜜桃 | 国产免费观看久久黄av片涩av | 成人国产精品入口免费视频 | 久草在线青青草 | 伊人在线视频 | 欧美日韩一区二区三区四区 | 中文字幕加勒比 | 91亚洲国产成人久久精品网站 | 蜜桃臀av一区二区三区 | 欧美一区视频 | 毛片一级片 | 国产伦精品一区二区三区四区视频 | 欧美日韩看片 | 激情久久av一区av二区av三区 | 国产亚洲成av人在线观看导航 | 日韩三级免费观看 | 久久在线免费 | av在线播放网站 | 久久国产综合 | 色免费视频 | 国产男女猛烈无遮掩视频免费网站 | 欧美一级黄色免费看 | 91精品久久久久久久 | 日韩2020狼一二三 | 久久久久国产 | 国产99久久精品一区二区300 | 特一级黄色毛片 | 中文字幕精品一区 | 草草视频在线免费观看 | 中文在线一区 | 亚洲美女视频 | 精品免费国产 | 亚洲午夜久久久 | 成人a免费| 天啪| 性视频一区 | 日韩一区二区三区四区五区六区 | 欧美不卡视频 | a在线免费观看视频 | 亚洲日韩欧美一区二区在线 |