#ifdef MAIN #define EXTERN #else #define EXTERN extern #endif EXTERN グローバル変数 ... |
int i,n,iz; iz = 1; for (i=0;i<n;++i) { while (10*(i+1) >= n*iz) fprintf(stderr,"%3d%%\n",10*iz++); ... } |
struct Test { enum {Size = 10}; int array[Size]; }; |
#include <string> #include <map> map<string,int,less<string> > m; |
inline bool iskanji(unsigned char c) { return c >= 0x81 && c <= 0x9f || c >= 0xe0 && c <=0xfc; } |
string itos(int i) { stringstream s; s << i; return s.str(); } string ftos(double d) { stringstream s; s << d; return s.str(); } |
// 2点(p,q)-(r,s) を結ぶ直線上に点(i,j)から降ろした垂線との交点(x,y) pair<double,double> GetPerp(double p,double q,double r,double s,double i,double j) { double a = p-r, b = q-s, l = a*a+b*b, d = q*r-p*s, e = i*a+j*b; if (l == 0.) l = 1; return make_pair((b*d+a*e)/l,(b*e-a*d)/l); } |
% cat test.c #include <vector> struct A { vector<int> v; }; int main() { A a,b; a = b; } % g++ test.c Undefined first referenced symbol in file __as__t6vector1ZiRCt6vector1Zi /var/tmp/cca003m31.o ld: fatal: Symbol referencing errors. No output written to a.out |
struct A { vector<int> v; A& operator=(const A& a) {v = a.v; return *this;} }; |
struct Point { double x,y; Point(double x0 = 0,double y0 = 0) : x(x0), y(y0) {} Point operator+(const Point& p)const{return Point(x+p.x,y+p.y);} Point operator-(const Point& p)const{return Point(x-p.x,y-p.y);} int operator*(const Point& p)const {double z = x*p.x + y*p.y; return z > 0 ? 1 : z < 0 ? -1 : 0;} int IsRight(const Point& p)const{return operator*(Point(-p.y,p.x));} }; bool IsCross(const Point& p1,const Point& p2,const Point& p3,const Point& p4) { int t1,t2; Point p5 = p2 - p1, p6 = p4 - p3; t1 = p5.IsRight(p3-p1) * p5.IsRight(p4-p1); if (t1 > 0) return false; t2 = p6.IsRight(p1-p3) * p6.IsRight(p2-p3); return t2 < 0 || (t1 < 0 && t2 == 0); } |
struct point { int x,y; }; // 方法1 bool IsInclude(const vector<point>& line, point pt) { int i,k,whc = 0, n = line.size(); if (n < 3) return false; const point *p1 = &line.back(), *p2 = line.begin(), *p3; bool bRes = false; double d; for (i=0;i<n;++i) { if ((d = p2->x - p1->x) == 0) { if (pt.x == p1->x && ((p1->y <= pt.y && pt.y <= p2->y) || (p1->y >= pt.y && pt.y >= p2->y))) return true; } else if (p2->x == pt.x) { if (pt.y == p1->y) return true; whc = p1->y > pt.y ? p1->x - pt.x : 0; } else if ((p1->x < pt.x && pt.x < p2->x) || (p1->x > pt.x && pt.x > p2->x)) { d = (pt.y - p1->y) * d * d - (p2->y - p1->y) * (pt.x - p1->x) * d; if (d == 0) return true; if (d < 0) bRes = !bRes; whc = 0; } else { if (whc && whc * (p2->x - pt.x) < 0) bRes = !bRes; whc = 0; } p1 = p2++; } if (whc && whc * (m_line.front().x - pt.x) < 0) bRes = !bRes; return bRes; } // 方法2 bool IsInclude2(const vector<point>& line, point pt) { int i, n = line.size(); if (n < 3) return false; double v0,v1,v2, x = pt.x, y = pt.y; vector<point>::const_iterator it = line.begin(); const point *p0 = it, *p1, *p2; bool bRes = false, bBorder; for (i=1;i<n;++i) { if (i%2) { p1 = ++it; bBorder = (v1 = x*(p1->y-p0->y)-y*(p1->x-p0->x) + double(p1->x)*p0->y - double(p1->y)*p0->x) == 0; if (i == 1) continue; } else { p2 = ++it; bBorder = (v0 = x*(p0->y-p2->y) - y*(p0->x-p2->x) + double(p0->x)*p2->y - double(p0->y)*p2->x) == 0; } v2 = x*(p2->y-p1->y) - y*(p2->x-p1->x) + double(p2->x)*p1->y - double(p2->y)*p1->x; if (v0*v1 >= 0 && v1*v2 >= 0 && (!bBorder || i == n-1)) bRes = !bRes; } return bRes; } // 方法3 bool IsInclude3(const vector<point>& line, point pt) { int i, n = line.size(); if (n < 3) return false; const CPoint *p1, *p2 = line.begin(); if (p2->x == pt.x) { for (i=1;i<n;++i) { // 開始点 p2 を探す p1 = p2++; if (p2->x != pt.x) break; if ((p2->y-pt.y)*(pt.y-p1->y) >= 0) return true; } if (i == n) return false; // 全部の点の x 座標が同じ } double d; // 交点の y 座標 bool b,bLeft, bRes = false; // 含んでいるかどうか bLeft = p2->x < pt.x; // p2 は pt の左にある for (i=0;i<n;++i) { p1 = p2++; if (p2 == line.end()) p2 = line.begin(); b = p2->x == pt.x ? bLeft : p2->x < pt.x; if (b != bLeft) { // 線分が pt をはさむ d = p2->x - p1->x; d = (pt.y - p1->y) * d * d - (p2->y - p1->y) * (pt.x - p1->x) * d; if (d == 0) return true; if (d < 0) bRes = !bRes; bLeft = b; } } return bRes; } |
struct point { double x,y; point(double x0=0,double y0=0) : x(x0),y(y0) {} double len(const point& p)const{ double xx=x-p.x,yy=y-p.y; return sqrt(xx*xx + yy*yy);} }; double Area(const vector<point>& p) { int i, n = p.size(); if (n < 3) return 0.; const point& p0 = p.front(); double s,a,b, c = p0.len(p[1]), res = 0.; for (i=2;i<n;++i) { a = c; b = p[i-1].len(p[i]); c = p0.len(p[i]); s = (a+b+c)/2; res += sqrt(s*(s-a)*(s-b)*(s-c)); } return res; } |
// Γ(x) = exp(lngamma(x)) double lngamma(double xx) { static double cof[6]={76.18009173,-86.50532033,24.01409822, -1.231739516,0.120858003e-2,-0.536382e-5}; double x=xx-1.0; double tmp=x+5.5; tmp -= (x+0.5)*log(tmp); double ser=1.0; for (int j=0;j<=5;j++) { x += 1.0; ser += cof[j]/x; } return -tmp+log(2.50662827465*ser); } |