板凳——————————————————c++(101)

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

void printX(){
}
//侯捷老师C++ 11标准 Variadic Templates 1
template<typename T, typename… Type>
void printX(const T& firstArg, const Type&… args){
std::cout << firstArg << std::endl;
printX(args…);
}
//侯捷老师C++ 11标准 Variadic Templates 2
void printF(const char *s){
while(*s){
if(s == ‘%’&&(++s) != ‘%’)
throw std::runtime_error(“invalid format string: missing arguments”);
std::cout << *s++;
}
}

template<typename T, typename… Args>
void printF(const char *s, T value, Args… args){
while(*s){
if(*s == ‘%’ && *(++s) != ‘%’){
std::cout << value;
printF(++s, args…);
return;
}
std::cout << *s++;
}
throw std::logic_error(“extra arguments provided to printf”);
}
//侯捷老师C++ 11标准 Variadic Templates 3 2020年10月21日 星期三 22时07分31秒
struct _Iter_less_iter{
template<typename _Iterator1,
typename _Iterator2>
bool
operator()(_Iterator1 __it1,
_Iterator2 __it2)const{
return *__it1 < * __it2;
}
};

inline _Iter_less_iter
__iter_less_iter(){
return _Iter_less_iter();
}

template<typename _ForwardIterator,
typename _Compare>
_ForwardIterator
__max_element(_ForwardIterator __first,
_ForwardIterator __last,
_Compare __comp){
if(__first == __last)return __first;
_ForwardIterator __result = __first;
while(++__first != __last)
if(__comp(__result, __first))
__result = __first;
return __result;
}

template
inline _ForwardIterator
max_element(_ForwardIterator __first,
_ForwardIterator __last){
return __max_element(__first, __last, __iter_less_iter());
}

template
inline _Tp
max(std::initializer_list<_Tp> _l){
return * max_element(_l.begin(), _l.end());
}

//侯捷老师C++ 11标准 Variadic Templates 4
int maximum(int n){
return n;
}

template<typename…Args>
int maximum(int n, Args…args){
return std::max(n, maximum(args…));
}

//侯捷老师C++ 11标准 Variadic Templates 5
template <int IDX, int MAX, typename…Args>
struct PRINT_TUPLE{
static void print(std::ostream& os, const std::tuple<Args…>& t){
os << std::get(t) << (IDX+1 == MAX?"":",");
PRINT_TUPLE<IDX+1, MAX, Args…>::print(os,t);
}
};

template <typename… Args>
std::ostream& operator << (std::ostream& os, const std::tuple<Args…> & t){
os << “[”;
PRINT_TUPLE<0, sizeof…(Args), Args…>::print(os, t);
return os << “]”;
}

template <int MAX, typename…Args>
struct PRINT_TUPLE<MAX, MAX, Args…> {
static void print(std::ostream& os, const std::tuple<Args…>& t){

}

};

//递归调用处理的都是参数,所以使用函数模板;递归继承处理的都是类型,所以使用类模板。
//侯捷老师C++ 11标准 Variadic Templates 6
//https://www.cnblogs.com/flysong/articles/10047700.html
template<typename… Values> class tup;
template<> class tup<> { };

template<typename Head, typename… Tail>
class tup<Head, Tail…>
{
typedef tup<Tail…> composited;
protected:
composited m_tail;
Head m_head;
public:
tup() { }
tup(Head v, Tail… vtail)
: m_tail(vtail…), m_head(v) { }

Head head() { return m_head; }                
composited& tail() { return m_tail; } 
};
// 这里需要用引用,不然修改值时因为改的是拷贝版本,原始版本不会被改变

/*
template<typename… Values> class tuple;
template<> class tuple<> { };

template<typename Head, typename… Tail>
class tuple<Head, Tail…>
:private tuple<Tail…>{
typedef tuple<Tail…> inherited;

public:
tuple() { }
tuple(Head v, Tail… vtail)
: m_head(v), inherited(vtail…){ }

typename Head::type head() { return m_head; }                
inherited& tail() { return *this; } 

protected:
Head m_head;
};*/

int main(){
std::cout << __cplusplus << std::endl;

auto i = 42;
double f();

// auto d = f();

static auto vat = 0.19;

std::vector<std::string> v1;
auto pos = v1.begin();

auto l = [](int x)->bool{
   return x < 10;
};

//p15
int values[] {1, 2, 3};
std::vector<int> v2 {2, 3, 5, 7, 11, 13, 17};
std::vector<std::string> cities {
	"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"
};
std::complex<double> c {4.0, 3.0};

//p18
int array[] = {1, 2, 3, 4, 5};
long sum = 0;

for(int i : {2, 3, 5, 7, 9, 13, 17, 19}){
	std::cout << i << " " ;
}
std::cout << std::endl;

for(int x : array){
	sum += x;
}

for(auto elem : {sum, sum*2, sum*4}){
	std::cout << elem << std::endl;
}
std::cout << std::endl;
//p26
printX(7.5, "hello", std::bitset<16>(377), 42);

int *pi = new int;
printF("%d%s%p%f\n", //printF 与 printf 错误:调用重载的‘printf(const char*&)’有歧义
 15,
 "This is Ace.",
  pi, 
  3.141592653);
  
  std::cout << max({57, 48, 60, 100, 20, 18}) << std::endl;
  
  std::cout << maximum(57, 48, 60, 100, 20, 18) << std::endl;
  
  std::cout << std::make_tuple(7.5, std::string("hello"), std::bitset<16>(377), 42) << std::endl;
  // 需要实现的打印效果 [7.5,hello,0000000101111001,42]
  //https://www.cnblogs.com/flysong/articles/10047700.html
    std::string a = "nico"; 
    std::cout << sizeof("nico") << std::endl; //5
    std::cout << sizeof(a) << std::endl;      //32
     
    float b = 6.3;
    std::cout << sizeof(6.3) << std::endl;    //8
    std::cout << sizeof(b) << std::endl;      //4
     
    std::cout << sizeof(41) << std::endl;     //4
     
    tup<int, float, std::string> t(41, 6.3, "nico");
    std::cout << sizeof(t) << std::endl;      //56
    std::cout << t.head() << std::endl;   //41
    std::cout << t.tail().head() << std::endl;//6.3
    std::cout << t.tail().tail().head() << std::endl; //nico  
     
    tup<std::string> t1("nico");
    tup<float, std::string> t2(6.3, "nico");
    std::cout << sizeof({tup<>();}) << std::endl;        //1 
    std::cout << sizeof(t1) << std::endl;     //40
    std::cout << sizeof(t2) << std::endl;     //48
    return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -o c11 c11.cpp
wannian07@wannian07-PC:~$ ./c11
201402
2 3 5 7 9 13 17 19
15
30
60

7.5
hello
0000000101111001
42

15This is Ace.0x13461603.14159

100

100

[7.5,hello,0000000101111001,42]

5
32
8
4
4
56
41
6.3
nico
1
40
48

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值