您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關C++11新特性std::tuple的用方,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1. 引入頭文件
#include <tuple>
2. std::tuple初始化
std::tuple<int, std::string, float> t1(10, "Test", 3.14);
這里要注意,不是所有的C++ 11編譯器都支持copy-list-initialization的方式。如下代碼所示。
std::tuple<int, int> foo_tuple() { return {1, -1}; // Error until N4387 return std::tuple<int, int>{1, -1}; // Always works return std::make_tuple(1, -1); // Always works }
3. 打印std::tuple
打印std::tuple可以將它的元素逐個打印出來,不過非常繁瑣,我們可以通過如下通用的打印函數,幫助我們一次性的將tuple的所有要素打印出來。
#include <iostream> #include <tuple> #include <string> // helper function to print a tuple of any size template<class Tuple, std::size_t N> struct TuplePrinter { static void print(const Tuple& t) { TuplePrinter<Tuple, N-1>::print(t); std::cout << ", " << std::get<N-1>(t); } }; template<class Tuple> struct TuplePrinter<Tuple, 1> { static void print(const Tuple& t) { std::cout << std::get<0>(t); } }; template<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0> void print(const std::tuple<Args...>& t) { std::cout << "()\n"; } template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0> void print(const std::tuple<Args...>& t) { std::cout << "("; TuplePrinter<decltype(t), sizeof...(Args)>::print(t); std::cout << ")\n"; } // end helper function int main() { std::tuple<int, std::string, float> t1(10, "Test", 3.14); print(t1); }
輸出:
(10, Test, 3.14)
4、合并多個std::tuple
std::tuple_cat函數可以將多個std::tuple合并為一個tuple。
int main() { std::tuple<int, std::string, float> t1(10, "Test", 3.14); int n = 7; auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n)); n = 42; print(t2); }
輸出:
(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)
5. std::tuple的解包(unpack)
std::tie能夠將std::tuple包含的要素解包(unpack)成單個的對象。
#include <iostream> #include <tuple> #include <string> int main() { auto info = std::make_tuple(3.8, 'A', "Lisa Simpson"); double score = 0.0; char grade; std::string name; std::tie(score, grade, name) = info; std::cout << "score:" << score << ", grade:" << grade << ", name:" << name << std::endl; return 0; }
輸出:
score:3.8, grade:A, name:Lisa Simpson
std::tie還支持std::pair對象的解包(unpack)。
#include <iostream> #include <tuple> #include <string> #include <utility> int main() { auto info = std::make_pair(3.8, "Lisa Simpson"); double score = 0.0; std::string name; std::tie(score, name) = info; std::cout << "score:" << score << ", name:" << name << std::endl; return 0; }
輸出:
score:3.8, name:Lisa Simpson
當我們不關注tuple中的某個元素時,可以使用std::ignore忽略該元素。
#include <iostream> #include <tuple> #include <string> #include <utility> int main() { auto info = std::make_pair(3.8, "Lisa Simpson"); double score = 0.0; std::string name; std::tie(score, std::ignore) = info; std::cout << "score:" << score << ", name:" << name << std::endl; return 0; }
輸出:
score:3.8, name:
看完上述內容,你們對C++11新特性std::tuple的用方有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。