sizeof

How can a template function 'know' the size of the array given as template argument?

拥有回忆 提交于 2019-11-29 12:01:42
In the C++ code below, the templated Check function gives an output that is not what I would like: it's 1 instead of 3. I suspect that K is mapped to int* , not to int[3] (is that a type?). I would like it to give me the same output than the second (non templated) function, to which I explicitly give the size of the array... Short of using macros, is there a way to write a Check function that accepts a single argument but still knows the size of the array? #include <iostream> using namespace std; int data[] = {1,2,3}; template <class K> void Check(K data) { cout << "Deduced size: " << sizeof

do{...} while(0)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 11:23:59
前段时间看公司项目源码时,发现一个很有意思的地方,就是在很多宏函数定义里会出现do{...} while(0)。 #define ASSERT_ARRAY_EQ(expect, actual) do { / ASSERT_EQ(sizeof(expect), sizeof(actual)); / for( size_t i = 0; i < sizeof(expect)/sizeof(int); i++) / { / ASSERT_EQ(expect[i], actual[i]); / } / } while(0) 开始的时候百思不得其解,写不写这个循环似乎都只会跑一次,那是不是多此一举呢。后来仔细想了想,这么做在实际的工程项目中的确是非常有用的。 如果不使用do{...} while(0),在非嵌套使用的情况下,使用这个宏是不会出现什么问题的。 /* ...........code............ */ ASSERT_ARRAY_EQ(expected, actual); /* ...........code............ */ 上面这段代码里中间那一行会被宏函数直接替换。 然而,考虑下面这种情况: if(...) ASSERT_ARRAY_EQ(expected, actual); else ... 如果没有do{...} while(0)

C++: getting the row size of a multidimensional array passed to a function

二次信任 提交于 2019-11-29 10:43:18
I'm trying to write a function that will print out the contents of a multidimensional array. I know the size of the columns, but not the size of the rows. EDIT: Since I didn't make this clear, the arrays passed to this function are NOT dynamically allocated. The sizes are known at compile time. I am testing it using a 3x2 array. Here is the function as it stands: void printArrays(int array1[][2], int array2[][2]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << "\narray1[" << i << "][" << j << "] = " << setfill('0') << setw(2) << array1[i][j] << "\tarray2[" << i << "][" <<

Maximum size of size_t

别来无恙 提交于 2019-11-29 10:38:54
问题 I know in C return type of sizeof operator is size_t being unsigned integer type defined in <stdint.h> . Which means max size of it should be 65535 as stated in C99 standard 7.18.3: limit of size_t SIZE_MAX 65535 However in gcc-4.8.2 header file stdint.h has defined its size much greater than 65535 contradicting to which is stated in C99 standard as below shown, /* Limit of `size_t' type. */ # if __WORDSIZE == 64 # define SIZE_MAX (18446744073709551615UL) # else # define SIZE_MAX (4294967295U

指针详解

戏子无情 提交于 2019-11-29 09:57:45
前言:复杂类型说明 要了解指针,多多少少会出现一些比较复杂的类型,所以我先介绍一下如何完全理解一个复杂类型,要理解复杂类型其实很简单,一个类型里会出现很多运算符,他们也像普通的表达式一样,有优先级,其优先级和运算优先级一样,所以我总结了一下其原则:从变量名处起,根据运算符优先级结合,一步一步分析.下面让我们先从简单的类型开始慢慢分析吧: int p; //这是一个普通的整型变量 int *p; //首先从P 处开始,先与*结合,所以说明P 是一个指针,然后再与int 结合,说明指针所指向的内容的类型为int 型.所以P是一个返回整型数据的指针 int p[3]; //首先从P 处开始,先与[]结合,说明P 是一个数组,然后与int 结合,说明数组里的元素是整型的,所以P 是一个由整型数据组成的数组 int *p[3]; //首先从P 处开始,先与[]结合,因为其优先级比*高,所以P 是一个数组,然后再与*结合,说明数组里的元素是指针类型,然后再与int 结合,说明指针所指向的内容的类型是整型的,所以P 是一个由返回整型数据的指针所组成的数组 int (*p)[3]; //首先从P 处开始,先与*结合,说明P 是一个指针然后再与[]结合(与"()"这步可以忽略,只是为了改变优先级),说明指针所指向的内容是一个数组,然后再与int 结合,说明数组里的元素是整型的.所以P

C++Primer 5th Chap4 Expressions

邮差的信 提交于 2019-11-29 08:27:41
左值和右值:左值:用的是对象的身份(内存中的位置),右值:用的是对象的值(内容) 解引用与递增(递减)运算符连用:   *ivec++:取ivec当前值并向后移动一个元素,等价于*(ivec++),本来++的优先级高于* 条件运算符嵌套:(注意在语句两端加上括号,条件运算符优先级极低)   A?B:C?D:E等价于A?B:(C?D:E) 位运算符(建议仅将位运算用于无符号类型,避免因符号位问题造成错误)   用法: 运算符 功能 用法 ~ 位求反 ~exp << 左移 exp1<<exp2 >> 右移 exp1>>exp2 & 位与 exp1&exp2 ^ 位异或 exp1^exp2 | 位或 exp1|exp2          位运算的结果一旦超出位的范围,超出部分的位将会被舍弃    <<操作符满足左结合律 sizeof运算符 :返回一条表达式或类型名所占的字节数,返回值类型为size_t,满足右结合律   sizeof(data.revenue);//获取类对象data的revenue成员对应类型的大小   sizeof(arr);//获取数组arr的类型大小,是其所有元素类型大小之和(利用sizeof(arr)/sizeof(*arr)可以获取arr的元素数量 命名的强制类型转换 :cast-name<type>(expression),cast-name有4种

void 指针的使用规则

六眼飞鱼酱① 提交于 2019-11-29 07:47:48
1、void 指针可以指向任意类型的数据 就是说可以用任意类型的指针对 void 指针赋值,如果要将 void 指针 p 赋给其他类型的指针,则需要强制类型转换,就本例而言:a=(int *)p。因为"无类型"可以包容"有类型",而"有类型"则不能包容"无类型"。 int * a; void * p; p = a; 2、void 指针算术运行 在 ANSI C 标准中,不允许对 void 指针进行一些算术运算如 p++ 或 p+=1 等,因为既然 void 是无类型,那么每次算术运算我们就不知道该操作几个字节,例如 char 型操作 sizeof(char) 字节,而 int 则要操作 sizeof(int) 字节。而在 GNU 中则允许,因为在默认情况下,GNU 认为 void * 和 char * 一样,既然是确定的,当然可以进行一些算术操作,在这里sizeof(*p)==sizeof(char) int * a; void * p; p = a; ( int * ) p ++ ; //错误 1、由于(类型) 与 ++ 优先级相同,从右向左结合,所以会先计算p++,而p仍为void*不能进行++运算 ( ( int * ) p ) ++ ; //正确 1、由于()优先级比++高,所以会先处理(int*)p,将void*p强制转换成int* 作为左值,然后在进行++运算 int

Educational Codeforces Round 72 (Rated for Div. 2)

不想你离开。 提交于 2019-11-29 07:31:58
四题就可以当大爷了,一共才六道题。 A、你有一个角色,a点力量b点智力,你现在有c点经验值,1点经验值可以把力量或智力增加1 你现在需要用掉所有的经验值,问你有多少种使用经验值的方案,使得最后力量大于智力 首先如果力量一开始小于智力,先消耗经验值增加力量直到力量大于智力 然后考虑方程组: (a+x)>(b+y) (x+y)=c x和y分别是增加的力量和智力 稍微改动一下: (a+x)>=(b+y+1) 这样可以求的最小的x满足上述方程组 那么得到2x>=c+b-a+1 注意一下(c+b-a+1)不一定是偶数,所以实际上x=((c+b-a+1)+1)/2 即向上取整。 最后解出y=c-x,总方案数就是[0,y]这个区间中的数字个数,每一个y都可以满足条件 似乎做完了,但是有个坑点需要特判: 如果(b+c)<a,说明不管怎么加一定满足条件,此时应当输出c+1 代码: #include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define schar(a) scanf("%c",&a) #define

Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

断了今生、忘了曾经 提交于 2019-11-29 07:09:07
二进制专场,还被FST一题。 A、签到,题意是,给定a,b,c f(0)=a,f(1)=b,f(n)=f(n-1)^f(n-2) 求f(n) 稍微化简就看得出f(x)是按照a,b,a^b这样在循环出现 因为a^b^a=b,a^b^b=a 代码: #include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define schar(a) scanf("%c",&a) #define pr(a) printf("%lld",a) #define fo(i,a,b) for(int i=a;i<b;++i) #define re(i,a,b) for(int i=a;i<=b;++i) #define rfo(i,a,b) for(int i=a;i>b;--i) #define rre(i,a,b) for(int i=a;i>=b;--i) #define prn() printf("\n") #define prs() printf(" ") #define mkp make_pair

No Smoking, Please Gym - 101518H

偶尔善良 提交于 2019-11-29 06:47:33
No Smoking, Please Gym - 101518H 最小割~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 1000110; 4 const int maxe = 4000010; 5 const int inf = 0x3f3f3f3f; 6 struct Edge{ 7 int u, v, nex; 8 int cap, flow; 9 Edge(int u=0, int v=0, int nex=0, int cap=0, int flow=0): 10 u(u), v(v), nex(nex), cap(cap), flow(flow){} 11 }e[maxe<<1]; 12 int head[maxv]; 13 int cnt; 14 void init(){ 15 memset(head, -1, sizeof(head)); 16 cnt = 0; 17 } 18 19 void add(int u, int v, int cap){ 20 e[cnt] = Edge(u, v, head[u], cap, 0); 21 head[u] = cnt++; 22 e[cnt] = Edge(v, u, head[v], cap, 0); 23 head[v] =