板凳——————————————————c++(103)

#include
#include
#include

template
std::ostream& operator<<(std::ostream& os, const std::vector& vec)
{
for (auto& el : vec)
{
os << el << ’ ';
}
return os;
}

int main()
{
std::vectorstd::string vec = {
“Hello”, “from”, “GCC”, VERSION, “!”
};
std::cout << vec << std::endl;
}

https://en.cppreference.com/w/cpp/utility/pair/tuple_element
#include
#include
#include

namespace detail {

template std::size_t
struct index_tag { explicit constexpr index_tag() = default; };

template <class T, class U>
constexpr T get_val_dispatch(std::pair<T, U> const& pair, index_tag<0>)
{
return pair.first;
}

template <class T, class U>
constexpr U get_val_dispatch(std::pair<T, U> const& pair, index_tag<1>)
{
return pair.second;
}

} // namespace detail

template <std::size_t N, class T, class U>
auto constexpr get_val(std::pair<T, U> const& pair)
-> typename std::tuple_element<N, std::pair<T, U>>::type
{
return detail::get_val_dispatch(pair, detail::index_tag{});
}

int main()
{
auto var = std::make_pair(1, std::string{“one”});

std::cout << get_val<0>(var) << " = " << get_val<1>(var);

}

//The c++ standard library 2nd p68
#include
#include
#include
#include
#include
#include

template<typename T1, typename T2>
std::ostream& operator << (std::ostream& strm,
const std::pair<T1, T2> & p){
return strm << “[” << p.first << “.”
<< p.second << “]”;
}

typedef std::pair<int, float> IntFloatPair;

class Foo{
public:
Foo(std::tuple<int, float>) {
std::cout << “Foo::Foo(tuple)” << std::endl;
}
template<typename… Args>
Foo(Args… args) {
std::cout << “Foo::Foo(args…)” << std::endl;
}

};

int main(){
IntFloatPair p(42, 3.14);
std::cout << std::get<0>§<< " ";
std::cout << std::get<1>§<< " ";
std::cout << std::endl;
std::cout << std::tuple_size::value << std::endl;
// std::cout << std::tuple_element<0,IntFloatPair>::type << std::endl;

std::tuple<std::string, int, int, std::complex<double>> t;
std::tuple<int, float, std::string> t1(41, 6.3, "nico");

std::cout << std::get<0>(t1) << " ";
std::cout << std::get<1>(t1) << " ";
std::cout << std::get<2>(t1) << " ";
std::cout << std::endl;

auto t2 = std::make_tuple(22, 44, "nico");

std::get<1>(t1) = std::get<1>(t2);

if(t1 < t2){
	t1 = t2;
}

std::tuple<int, float> t3 (1, 2.22);
std::pair<int, Foo> p1(42, t3);
std::pair<int, Foo> p2(std::piecewise_construct, std::make_tuple(42), t3);
//p66
int i = 0;
auto p3 = std::make_pair(std::ref(i), std::ref(i)); 
++p3.first;
++p3.second;
std::cout << "i: " << i << std::endl;
//67
std::pair<char, char> p4 = std::make_pair('x', 'y');
char c;
//解包时,我们如果只想解某个位置的值时,可以用std::ignore占位符来表示不解某个位置的值
std::tie(std::ignore, c) = p4;
//std::tie(std::ignore, std::ignore, y) = tp; //只解第三个值了
//p71
std::string s;
auto x = std::make_tuple(s);
std::get<0>(x) = "my value";

auto y = std::make_tuple(ref(s));
std::get<0>(y) = "my value";

std::tuple<int, float, std::string> t5(77, 1.1, "more light");
int i5;
float f5;
std::string s5;
std::make_tuple(std::ref(i5), std::ref(f5), std::ref(s5)) = t5;
std::tie(i5, f5, s5) = t5;

std::vector<std::tuple<int, float>> v { std::make_tuple(1, 1.0),
                                        std::make_tuple(2, 2.0)};

// std::tuple<int, int, int> foo() {
// return std::make_tuple(1, 2, 3);
// }

//https://www.cnblogs.com/depend-wind/articles/10154730.html 

}

//Constructs a tuple that is a concatenation of all tuples in args.

//The behavior is undefined if any type in std::decay_t… is not a specialization of std::tuple. However, an implementation may choose to support types (such as std::array and std::pair) that follow the tuple-like protocol.
//https://en.cppreference.com/w/cpp/utility/tuple/tuple_cat

#include
#include
#include

// 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(t);
}
};

template
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);
int n = 7;
auto t2 = std::tuple_cat(t1, std::make_tuple(“Foo”, “bar”), t1, std::tie(n));
n = 42;
print(t2);
}

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)

//https://en.cppreference.com/w/cpp/utility/make_from_tuple
#if 0
Notes

The tuple need not be std::tuple, and instead may be anything that supports std::get and std::tuple_size; in particular, std::array and std::pair may be used.

Due to guaranteed copy elision, T need not be movable.
Possible implementation

namespace detail {
template <class T, class Tuple, std::size_t… I>
constexpr T make_from_tuple_impl( Tuple&& t, std::index_sequence<I…> )
{
return T(std::get(std::forward(t))…);
}
} // namespace detail

template <class T, class Tuple>
constexpr T make_from_tuple( Tuple&& t )
{
return detail::make_from_tuple_impl(std::forward(t),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t>>{});
}

#endif

#include
#include

struct Foo {
Foo(int first, float second, int third) {
std::cout << first << ", " << second << ", " << third << “\n”;
}
};

int main()
{
auto tuple = std::make_tuple(42, 3.14f, 0);
std::make_from_tuple(std::move(tuple));
}

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
42, 3.14, 0

//https://en.cppreference.com/w/cpp/utility/apply
#if 0
Notes

The tuple need not be std::tuple, and instead may be anything that supports std::get and std::tuple_size; in particular, std::array and std::pair may be used.
Possible implementation

namespace detail {
template <class F, class Tuple, std::size_t… I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I…>)
{
// This implementation is valid since C++20 (via P1065R2)
// In C++17, a constexpr counterpart of std::invoke is actually needed here
return std::invoke(std::forward(f), std::get(std::forward(t))…);
}
} // namespace detail

template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
return detail::apply_impl(
std::forward(f), std::forward(t),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t>>{});
}
#endif

#include
#include
#include

int add(int first, int second) { return first + second; }

template
T add_generic(T first, T second) { return first + second; }

auto add_lambda = [](auto first, auto second) { return first + second; };

template<typename… Ts>
std::ostream& operator<<(std::ostream& os, std::tuple<Ts…> const& theTuple)
{
std::apply
(
[&os](Ts const&… tupleArgs)
{
os << ‘[’;
std::size_t n{0};
((os << tupleArgs << (++n != sizeof…(Ts) ? ", " : “”)), …);
os << ‘]’;
}, theTuple
);
return os;
}

int main()
{
// OK
std::cout << std::apply(add, std::pair(1, 2)) << ‘\n’;

// Error: can't deduce the function type
// std::cout << std::apply(add_generic, std::make_pair(2.0f, 3.0f)) << '\n'; 

// OK
std::cout << std::apply(add_lambda, std::pair(2.0f, 3.0f)) << '\n'; 

// advanced example
std::tuple myTuple(25, "Hello", 9.31f, 'c');
std::cout << myTuple << '\n';

}

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
3
5
[25, Hello, 9.31, c]

//https://en.cppreference.com/w/cpp/utility/tuple/make_tuple
#include
#include
#include

std::tuple<int, int> f() // this function returns multiple values
{
int x = 5;
return std::make_tuple(x, 7); // return {x,7}; in C++17
}

int main()
{
// heterogeneous tuple construction
int n = 1;
auto t = std::make_tuple(10, “Test”, 3.14, std::ref(n), n);
n = 7;
std::cout << "The value of t is " << “(”
<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
<< std::get<4>(t) << “)\n”;

// function returning multiple values
int a, b;
std::tie(a, b) = f();
std::cout << a << " " << b << "\n";

}

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
The value of t is (10, Test, 3.14, 7, 1)
5 7

#include
#include
int minus(int a, int b){
return a-b;
}
int main()
{
auto fifty_minus = std::bind_front(minus, 50);
std::cout << fifty_minus (3);
std::cout << std::endl;
}
wannian07@wannian07-PC:~$ g++ -std=c++2a -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
47

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值